1.1.6 • Published 2 months ago
mocha-chai-jest-snapshot v1.1.6
🍵mocha-chai-jest-snapshot📷
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);