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.
npm install --save-dev jest-file-snapshotor
yarn add --dev jest-file-snapshotExtend 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(filepath);
});The matcher takes one argument, which is the path to the file whose content should be matched.
You should also exclude the output files from Jest's wacher so that updating the snapshot doesn't re-run the tests again.