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-snapshot
or
yarn add --dev jest-file-snapshot
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(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.