provides snapshot testing like jest
- ✅ 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
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",
}
`;
# set envoriment
UPDATE_SNAPSHOT=1 mocha ...
# or add command line argument
mocha ... --update
npx mocha ... --reporter mocha-chai-jest-snapshot/reporters/spec
const chai = require("chai");
const { jestSnapshotPlugin } = require("mocha-chai-jest-snapshot");
chai.use(
jestSnapshotPlugin({
rootDir: "../",
snapshotResolver: "<rootDir>/jest/snapshotResolver",
snapshotSerializers: ["jest-serializer-vue"],
})
);
{
"jest": {
"rootDir": "../",
"snapshotResolver": "<rootDir>/jest/snapshotResolver",
"snapshotSerializers": ["jest-serializer-vue"]
}
}
module.exports = {
rootDir: "../",
snapshotResolver: "<rootDir>/jest/snapshotResolver",
snapshotSerializers: ["jest-serializer-vue"],
};
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);