npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

given2

v2.1.7

Published

Lazy variable evaluation for Jasmine, Mocha, Jest specs, inspired by Rspec's let

Downloads

28,223

Readme

⚠️Currently given2 supports only jasmine, mocha and jest.

Basically the given helper will register a beforeEach and a afterEach hook that will create a memoized get accessor with the given name. The value will be cached across multiple test suits in the same example but not across examples.

Note that given variables is lazy-evaluated: data in the variables are not calculated until they are accessed for the first time.

Installation

You can install given2 using npm or yarn

npm install given2
yarn add given2

Global namespace >= 2.1.2

given2 also can be imported into global namespace by simply requiring given2/setup.

import 'given2/setup';

Or configure your testing framework.

// Config example for jest
{
  "setupTestFrameworkScriptFile": "given2/setup"
}

Аfter that you can use given in your spec files without importing

Usage

To use given you just need to require or import the given2 module in your spec files

import given from 'given2'

describe('Example', () => {
  given('foo', () => 'bar');

  it('foo should be "bar"', () => {
    expect(given.foo).toBe('bar');
  })
})

More examples

The given2 variables are evaluated only once and are cached within a single test suite, and reset the cache after each suite.

let count = 0;

describe('given', () => {
  given('count', () => count += 1);

  // The values cached in same examples
  it('memoizes the value', () => {
    expect(given.count).toBe(1);
    expect(given.count).toBe(1);
  });

  // But do not cached across examples
  it('is not cached across examples', () => {
    expect(given.count).toBe(2);
  });
});

The values must be functions otherwise you will get an error.

describe('given', () => {
  it('should throw error', () => {
    // Such use will cause an error
    expect(() => given('value', 123)).toThrow();
  });
});

When you try to use the variable given, recursively given2 tells you about it.

describe('given', () => {
  // Such use will cause an error
  given('one', () => given.one);

  it('should throw error', () => {
    expect(() => given.one).toThrow();
  });
});

If you want the variable values not to be cached, use @ prefix. All variables that begin with this prefix will not be cached.

describe('given', () => {
  given('@random', () => Math.random());

  it('should not cache', () => {
    const cached = given.random;

    // values not cached
    expect(given.random).not.toBe(cached);
    expect(given.random).not.toBe(cached);
  });
});

Also you can get the values of the variables immediately, right after the declaration, with the prefix !

let counter = 1;

describe('given', () => {
  given('!next', () => counter += 1);

  it('should be 2', () => {
    expect(counter).toBe(2);
  });
});