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

givens

v1.3.9

Published

Easy test setup without side effects

Downloads

57,855

Readme

Easy test setup without side effects.

For use with jest, mocha, and jasmine. Behavior based on rspec, syntax inspired by given2


Why?

If you or your team have ruby and rspec experience, and are moving to a javascript based library, this will make the experience of writing javascript tests very similar to that of writing rspec tests.

Even if you don't come from a ruby background, givens can help you write maintainable tests quickly, while avoiding many of the side effects traditional JavaScript testing often has, such as:

  • testing different methods on the same object in multiple tests can result in cross-contamination.
  • tests can depend on order; and break when reordered.
  • Running only some tests in a single file (like via jest’s .skip or .only) can make tests behave unpredictably.

Givens, when used correctly solves all of these, dries up your tests, and might even make them more readable, while still letting you use beforeEach and afterEach.


What does it do?

when you call getGiven(), givens calls afterEach with a callback that clears the cache. Because this afterEach hook is declared first, it will run after every other afterEach in every scope.

when you call given(key, callback), givens calls the following lifecycle functions:

  • beforeAll, with a hook which pushes callback onto a stack of functions for key and makes sure there is an accessor present for key, which simply calls the topmost function on the stack and returns the result.
  • afterAll, with a hook which pops callback back off the stack of functions for key, and deletes the accessor if the stack is now empty.

Getting Started

Prerequisites

This package should work fine for any javascript or typescript project that uses jest or mocha tests. Non Node environments are untested, and may not work.

Installing

first, install from npm:

npm install --save-dev givens

or

yarn add --dev givens

global installation

import 'givens/setup';

or add to testing framework config, for example in jest:

{
  setupFilesAfterEnv: [ // this CANNOT be setupFiles
    'givens/setup.js',
  ],
}

then you can use the given keyword without importing.

local installation

in the test file, use the following import:

import getGiven from 'givens';
const given = getGiven();

or in typescript

import getGiven from 'givens';
interface myVars {
  var1: number; // the keys you intend to use
  var2: string; // and their types
}
const given = getGiven<myVars>();

Usage

When you call given(myKey, callback), givens stores the callback function you give it. When you go to retrieve the key (via given.myKey) givens will execute the most recent callback you have given for the key, and cache the value. Additionally, if you call given() inside a describe, the callback will revert to the previous one given for myKey after executing all tests in the describe, allowing you to override a given value for a set of tests. The cache is cleared after every test.

describe('basic overriding behavior', () => {
  given('var1', () => 'initial value');
  given('var2', () => {
    return 'the value is: ' + given.var1;
  });

  it('has initial value', () => {
    expect(given.var2).toEqual('the value is: initial value');
  });

  describe('with new value', () => {
    given('var1', () => 'overridden value');

    it('has overridden value', () => {
      expect(given.var2).toEqual('the value is: overridden value');
    });
  });

  it('has initial value again', () => {
    expect(given.var2).toEqual('the value is: initial value');
  });
});

For more examples of usage for your particular setup, read through the integration tests. They are examples of how to use the library as intended (and in some cases how it's not supposed to be used).