Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

jest-wrap

airbnb37.8kMIT1.7.0

Fluent pluggable interface for easily wrapping describe and it blocks in Jest tests.

jest, test, javascript, js, chai, before, after, beforeEach, afterEach, describe, it, wrap, around, around_filter, mock, stub

readme

jest-wrap Version Badge

Build Status dependency status dev dependency status License Downloads

npm badge

Fluent pluggable interface for easily wrapping describe, it, and test blocks in Jest tests.

Example

var wrap = require('jest-wrap');
var expect = require('chai').expect;

var mockWindow = {
    location: {
        href: 'test/url'
    }
};
wrap().withGlobal('window', () => mockWindow).describe('mocked window', function () {
    it('is mocked', function () {
        expect(window).to.equal(mockWindow);
    });

    it('has the right URL', function () {
        expect(window.location.href).to.equal('test/url');
    });
});

var obj = { a: 1 };
wrap().withOverrides(() => obj, () => ({ a: 2, b: 3 })).describe('overridden object keys', function () {
    it('has "b"', function () {
        expect(obj.b).to.equal(3);
    });

    it('has overridden "a"', function () {
        expect(obj.a).to.equal(2);
    });
});

wrap().withOverride(() => obj, 'a', () => 4).skip().describe('this test is skipped', function () {
    it('also supports .only()!', function () {
        expect(true).to.equal(false); // skipped
    });
});

Plugins

A jest-wrap plugin is a named function that returns a JestWrapper instance or a descriptor object.

  • A plugin’s function name must begin with the string “with”.
  • Plugins can be globally registered, or .used ad-hoc.
    • .use requires a plugin function as its first argument; further arguments are passed through to the plugin.
    • .extend requires a non-empty description string, and a descriptor object which may contain a value that is a function, or an array of functions, whose keys correspond to any or all of the supported jest methods.
  • Globally registered plugins, .use calls, and .extend calls can be chained, stored, and reused - each link in the chain creates a new instance of a JestWrapper.

  • A descriptor object may contain any or all of these 5 keys:

    • a description string, for use in “describe” and/or “it” (this is required when returning an object)
    • beforeEach: a function, or array of functions, for use in a jest beforeEach function
    • afterEach: a function, or array of functions, for use in a jest afterEach function
    • beforeAll: a function, or array of functions, for use in a jest beforeAll function
    • afterAll: a function, or array of functions, for use in a jest afterAll function

The most common approach will be for a plugin function to return this.extend(description, descriptor).

A plugin function must have a name that starts with “with”, and will be invoked with a receiver (”this” value) of a JestWrapper instance.

To register a plugin, call the register function on jest-wrap with the plugin function. This should not be done in a reusable module.

module.exports = function withFoo(any, args, you, want) {
    return this.extend('with some foo stuff', {
        beforeEach: function () {
            // setup ran before each test
        },
        afterEach: [
            function () {
                // teardown ran after each test
            },
            function () {
                // more teardown
            }
        ],
        beforeAll: function () {
            // setup ran once before all tests
        },
        afterAll: function () {
            // teardown ran once after all tests
        }
    });
};

Usage

var wrap = require('jest-wrap');
wrap.register(require('jest-wrap-with-foo'));

wrap().withFoo().describe…

skip/only

Although jest has describe.skip, describe.only, it.skip, it.only, test.skip, and test.only, it is not possible to implement these in jest-wrap without using ES5 property accessors. Since this project supports ES3, we decided to use .skip().describe etc rather than forfeit the ability to have skip/only.

Tests

Simply clone the repo, npm install, and run npm test

changelog

1.7.0 / 2020-05-12

  • [New] adds support for Jest ^26

1.6.0 / 2020-01-22

  • [New] adds support for Jest ^25

1.5.0 / 2019-02-11

  • [New] adds support for Jest ^24

1.4.0 / 2018-06-08

  • [New] adds support for Jest ^23

1.3.1 / 2018-05-01

  • [Fix] ensure that skip works inside plugins, and plugins with no changes but the mode work
  • [Deps] update semver
  • [Dev Deps] update eslint, istanbul-lib-coverage, nsp, tape, eslint-plugin-import

1.3.0 / 2018-01-04

  • [New] adds support for Jest ^22
  • [Deps] update function.prototype.name, is-primitive, object-inspect
  • [Dev Deps] update eslint, eslint-config-airbnb-base, eslint-plugin-import, nsp, rimraf
  • Move repo to airbnb

1.2.0 / 2017-05-12

  • [New] add jest v21 support
  • [Deps] update function-bind, function.prototype.name, object-inspect, semver
  • [Dev Deps] update eslint, @ljharb/eslint-config, istanbul-lib-coverage, nsp, tape
  • [Tests] make a matrix of jests
  • [Tests] only test major node versions; include v8
  • [Tests] use nvm install-latest-npm to ensure newer npms don’t break on older nodes

1.1.0 / 2017-05-12

  • [New] Add jest 20 support (#9)
  • [Docs] Correct links/badges in the README (#8)
  • [Tests] on node v7.10
  • [Tests] Correct jest18/19 in package.json (#5)

1.0.2 / 2017-04-11

  • [Fix] Fix descriptions when using multiple wrappers (#4)
  • [Fix] Stop reversing the afterEach hooks (#4)
  • [Fix] Update global beforeAll/afterAll hooks (#4)
  • [Fix] Remove .specify() (#4)
  • [Deps] update object-inspect
  • [Dev Deps] update eslint, istanbul-lib-coverage
  • [Tests] up to node v7.9
  • [Tests] Update core.js tests to work around bug in jest (#4)

1.0.1 / 2017-03-16

  • [Fix] avoid exponentially adding outer wrappers (#3)
  • [Deps] lock isarray down to v1 only (v2 has a silly deprecation warning)
  • [Dev Deps] update eslint, nsp, jest, rimraf
  • [Tests] up to node v7.7, v6.10, v4.8

1.0.0 / 2017-02-18

  • Initial release.