mocha-chai-jest-snapshot
TypeScript icon, indicating that this package has built-in type declarations

1.1.6 • Public • Published

🍵mocha-chai-jest-snapshot📷

npm MIT

NPM

provides snapshot testing like jest

Features

  • ✅ Simple to use
  • ✅ Output the same snapshot file as jest
  • ✅ Output the same snapshot summary as jest
  • ✅ Detect obsolete snapshots
  • ✅ Support custom serializer
  • ✅ Support typescript

Usage

npm i -D mocha-chai-jest-snapshot

then add it to the test setup:

// e.g. setup.js (mocha --file setup.js)
const chai = require("chai");
const { jestSnapshotPlugin } = require("mocha-chai-jest-snapshot");

chai.use(jestSnapshotPlugin());

enjoy.

const { expect } = require("chai");
it("foo", function () {
  expect({ foo: "bar" }).toMatchSnapshot();
});
> chai-snapshot@1.0.0 test /mnt/x/chai-snapshot
> npx mocha --file setup.js -r ts-node/register test/**/*.ts


  ✓ foo

Snapshot Summary
 › 1 snapshot written from 1 test suite.

  1 passing (9ms)
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`foo 1`] = `
Object {
  "foo": "bar",
}
`;

update snapshots

# set envoriment
UPDATE_SNAPSHOT=1 mocha ...
# or add command line argument
mocha ... --update

report at the end

npx mocha ... --reporter mocha-chai-jest-snapshot/reporters/spec

with options

jest options (setup)

const chai = require("chai");
const { jestSnapshotPlugin } = require("mocha-chai-jest-snapshot");

chai.use(
  jestSnapshotPlugin({
    rootDir: "../",
    snapshotResolver: "<rootDir>/jest/snapshotResolver",
    snapshotSerializers: ["jest-serializer-vue"],
  })
);

jest options (package.json)

{
  "jest": {
    "rootDir": "../",
    "snapshotResolver": "<rootDir>/jest/snapshotResolver",
    "snapshotSerializers": ["jest-serializer-vue"]
  }
}

jest options (jest.config.js)

module.exports = {
  rootDir: "../",
  snapshotResolver: "<rootDir>/jest/snapshotResolver",
  snapshotSerializers: ["jest-serializer-vue"],
};

custom serializer

const chai = require("chai");
const {
  jestSnapshotPlugin,
  addSerializer,
} = require("mocha-chai-jest-snapshot");
const customSerializer = require("...");

chai.use(jestSnapshotPlugin());
addSerializer(customSerializer);

or

const { expect } = require("chai");
const customSerializer = require("...");

expect.addSnapshotSerializer(customSerializer);

Package Sidebar

Install

npm i mocha-chai-jest-snapshot

Weekly Downloads

17,163

Version

1.1.6

License

MIT

Unpacked Size

53.7 kB

Total Files

85

Last publish

Collaborators

  • mochiya98