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

Package detail

mocha-wrap

airbnb3.4kMITdeprecated2.1.2

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

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

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

readme

mocha-wrap Version Badge

Build Status dependency status dev dependency status License Downloads

npm badge

Fluent pluggable interface for easily wrapping describe, context, it, and specify blocks in Mocha tests.

Example

var wrap = require('mocha-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 mocha-wrap plugin is a named function that returns a MochaWrapper 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 mocha 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 MochaWrapper.

  • 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 mocha beforeEach function
    • afterEach: a function, or array of functions, for use in a mocha afterEach function
    • before: a function, or array of functions, for use in a mocha before function
    • after: a function, or array of functions, for use in a mocha after 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 MochaWrapper instance.

To register a plugin, call the register function on mocha-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
            }
        ],
        before: function () {
            // setup ran once before all tests
        },
        after: function () {
            // teardown ran once after all tests
        }
    });
};

Usage

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

wrap().withFoo().describe…

skip/only

Although mocha has describe.skip, describe.only, context.skip, context.only, it.skip, it.only, specify.skip, and specify.only, it is not possible to implement these in mocha-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

2.1.2 / 2018-05-01

  • [Fix] ensure that skip works inside plugins, and plugins with no changes but the mode work
  • [Deps] update function-bind, function.prototype.name, object-inspect
  • [Dev Deps] update eslint, @ljharb/eslint-config, tape, rimraf, istanbul-lib-coverage
  • [Tests] add --no-save to testing install commands
  • [Tests] up to node v10.0, v9.11, v8.11, v7.10, v6.14, v4.9; use nvm install-latest-npm

2.1.1 / 2017-03-14

  • [Fix] ensure wrappers are only applied once when .use is called multiple times.

2.1.0 / 2016-10-04

  • [New] add support for specify as an alias to it
  • [Dev Deps] add eslint, @ljharb/eslint-config, istanbul, istanbul-lib-coverage, istanbul-merge, safe-publish-latest, tape
  • [Tests] up to node v6.6, v4.5; improve test matrix
  • [Tests] ensure coverage runs on mocha 3
  • [Docs] remove browser support matrix

2.0.5 / 2016-08-01

  • [Patch] [Tests] update peerDependencies for mocha v3; remove parallelshell since it does not reliably report failures.
  • [Tests] on node v6.3, v5.12
  • [Dev Deps] update eslint, @ljharb/eslint-config, tape, rimraf

2.0.4 / 2016-05-18

  • [Fix] withOverride/withOverrides should work on something with only a getter.
  • [Deps] update object-inspect
  • [Dev Deps] update eslint, @ljharb/eslint-config, istanbul-merge
  • [Tests] up to node v6.1

2.0.3 / 2016-03-22

  • [Fix] withOverrides: allow functions to be used as both the object to override, and the object of overrides.
  • [Tests] on node v5.9

2.0.2 / 2016-03-15

  • [Fix] make sure withOverrides re-calls the thunks on every iteration

2.0.1 / 2016-03-14

  • Something broke with the v2.0.0 publish

2.0.0 / 2016-03-14

  • [Breaking] withGlobal/withOverride/withOverrides now require a thunk for both targets and overrides (#2)
  • [Breaking] [New] Add .only()/.skip(), drop mocha < 1.4.1 support (#6, #1)
  • [Fix] ensure multiple nested before/after hooks are called in the right order (#3)
  • [Fix] ensure “no transformations” throws properly
  • [Tests] on node v5.8, v4.4
  • [Tests] parallelize tape tests and mocha tests
  • [Tests] ensure coverage dir is clean before running merge; parallelize coverage runs
  • [Tests] Echo a success message on npm run cover:check

1.1.0 / 2016-03-11

  • [New] add .context() (#4)
  • [New] add withOverride plugin (#5)
  • [Deps] update is-callable
  • [Dev Deps] update tape, eslint
  • [Dev Deps] move merge-coverage script into istanbul-merge package
  • [Tests] move tape tests to their own directory

1.0.1 / 2016-03-04

  • Improve describe wrapper message.

1.0.0 / 2016-03-03

  • Initial release.