7.0.1 • Published 9 months ago

mocha-expect-snapshot v7.0.1

Weekly downloads
-
License
-
Repository
github
Last release
9 months ago

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

7.0.1

9 months ago

7.0.0

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

6.1.0

2 years ago

6.0.0

2 years ago

6.2.0

2 years ago

3.2.0

2 years ago

4.0.0

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago