1. mocha-chai-jest-snapshot
provides snapshot testing like jest
mocha-chai-jest-snapshot
Package: mocha-chai-jest-snapshot
Created by: mochiya98
Last modified: Sat, 11 Jun 2022 13:56:23 GMT
Version: 1.1.4
License: MIT
Downloads: 54,427
Repository: https://github.com/mochiya98/mocha-chai-jest-snapshot

Install

npm install mocha-chai-jest-snapshot
yarn add mocha-chai-jest-snapshot

🍵mocha-chai-jest-snapshot📷

Travis (.org) 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();
});
> [email protected] 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);

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin