jest-file-snapshot

jest-file-snapshot

Jest matcher to write snapshots to a separate file instead of the default snapshot file used by Jest. Writing a snapshot to a separate file means you have proper syntax highlighting in the output file, and better readability without those pesky escape characters.

Installation

npm install --save-dev jest-file-snapshot

or

yarn add --dev jest-file-snapshot

Usage

Extend Jest's expect:

import { toMatchFile } from 'jest-file-snapshot';

expect.extend({ toMatchFile });

Then use it in your tests:

it("matches content of file on disk", () => {
  expect(content).toMatchFile();
});

The matcher takes two optional arguments:

  • filepath: path to the file whose content should be matched, e.g. expect(content).toMatchFile(path.join(__dirname, 'output.md'))
  • options: additional options object for the matcher, with following properties:

You should also exclude the output files from Jest's watcherso that updating the snapshot doesn't re-run the tests again.

For example, by default toMatchFileuses a folder named __file_snapshots__which you can exclude by adding the following under the jestkey in package.json:

"watchPathIgnorePatterns": [
  "__file_snapshots__"
]

Credits

Repository

https+https://github.com/satya164/jest-file-snapshot


上一篇:quicktype
下一篇:typewriter

相关推荐

官方社区

扫码加入 JavaScript 社区