mocha-expect-snapshot
> `expect` extension to use Jest's `toMatchSnapshot` in Mocha
Last updated 3 years ago by mshima .
Repository · Bugs · Original npm · Tarball · package.json
$ npm install mocha-expect-snapshot 
SYNC missed versions from official npm registry.

mocha-expect-snapshot

Mocha plugin to use expect with jest-snapshot and derived projects like jest-image-snapshot.

Usage

Configure as a mocha module.

{
  "require": "mocha-expect-snapshot"
}

Implement test.

const { expect } = require('expect');

describe('foo', function () {
  it('matches the snapshot', function () {
    expect({ foo: 'bar' }).toMatchSnapshot();

    expect({ foo: 'bar' }).toMatchInlineSnapshot(`
Object {
  "foo": "bar",
}
`);
  });
});

Typed expect

import { jestExpect as expect } from 'mocha-expect-snapshot';

describe('foo', function () {
  it('matches the snapshot', function () {
    expect({ foo: 'bar' }).toMatchSnapshot();

    expect({ foo: 'bar' }).toMatchInlineSnapshot(`
Object {
  "foo": "bar",
}
`);
  });
});

Update snapshots by passing --updateSnapshot option to mocha (not compatible with parallel mode).

mocha --updateSnapshot

Or by passing UPDATE_SNAPSHOT environment variable with value all or new.

UPDATE_SNAPSHOT=all mocha

jest-image-snapshot

Install.

npm install jest-image-snapshot --save-dev

Configure as a mocha module.

{
  "require": ["mocha-expect-snapshot/image"]
}

For both:

{
  "require": ["mocha-expect-snapshot/combined"]
}

Create tests.

const { expect } = require('expect');

it('test image', () => {
  const image = ...
  expect(image).toMatchImageSnapshot();
});

Custom configuration

File specific configuration must be set at top-level before, SnapshotState is created once per file:

const { expect } = require('expect');

describe('foo', function () {
  before(function () {
    // https://github.com/facebook/jest/blob/817d8b6aca845dd4fcfd7f8316293e69f3a116c5/packages/jest-snapshot/src/State.ts#L25-L30
    this.snapshotStateOptions = { updateSnapshot: 'new' };
    this.snapshotFile = '/foo';
  });

  it('matches the snapshot', function () {
    expect({ foo: 'bar' }).toMatchSnapshot();
  });

  it('matches the inline snapshot', function () {
    expect({ foo: 'bar' }).toMatchInlineSnapshot(`
Object {
  "foo": "bar",
}
`);
  });
});

Global configuration must be implemented with a custom [hook plugin](https://mochajs.org/#defining-a-root-hook-plugin]

// test/hooks.js
const {
  setSnapshotResolver,
  getSnapshotResolver,
  setSnapshotResolverOptions,
  getSnapshotResolverOptions,
  setSnapshotStateOptions,
  getSnapshotStateOptions,
  mochaHooks,
} = require('mocha-expect-snapshot');

// set your custom config

module.exports = {
  mochaHooks,
};
{
  "require": "./test/hooks.js"
}

This module is a reimplementation of expect-mocha-snapshot.

License

MIT

Current Tags

  • 7.0.1                                ...           latest (a year ago)

16 Versions

  • 7.0.1                                ...           a year ago
  • 7.0.0                                ...           2 years ago
  • 6.2.0                                ...           2 years ago
  • 6.1.0                                ...           2 years ago
  • 6.0.0                                ...           2 years ago
  • 5.0.1                                ...           2 years ago
  • 5.0.0                                ...           2 years ago
  • 4.0.0                                ...           3 years ago
  • 3.2.0                                ...           3 years ago
  • 3.1.0                                ...           3 years ago
  • 3.0.0                                ...           3 years ago
  • 2.1.0                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.1.1                                ...           3 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Total 14
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (10)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE