Skip to content

Commit

Permalink
Merge pull request #247 from tricoder42/patch-1
Browse files Browse the repository at this point in the history
Add note about snapshot testing
  • Loading branch information
tschaub authored Sep 11, 2018
2 parents f54a322 + a90b7ac commit 5e68c29
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,21 @@ Mock file access is controlled based on file mode where `process.getuid()` and `

Tested on Linux, OSX, and Windows using Node 0.10 through 10.x. Check the tickets for a list of [known issues](https://github.com/tschaub/mock-fs/issues).

### Using with Jest Snapshot Testing

`.toMatchSnapshot` in [Jest](https://jestjs.io/docs/en/snapshot-testing) uses `fs` to load existing snapshots.
If `mockFs` is active, Jest isn't able to load existing snapshots. In such case it accepts all snapshots
without diffing the old ones, which breaks the concept of snapshot testing.

Calling `mock.restore()` in `afterEach` is too late and it's necessary to call it before snapshot matching:

```js
const actual = testedFunction()
mock.restore()
expect(actual).toMatchSnapshot()
```

Note: it's safe to call `mock.restore` multiple times, so it can still be called in `afterEach` and then manually
in test cases which use snapshot testing.

[![Current Status](https://secure.travis-ci.org/tschaub/mock-fs.png?branch=master)](https://travis-ci.org/tschaub/mock-fs)

0 comments on commit 5e68c29

Please sign in to comment.