Skip to content

Commit

Permalink
fix: matchInlineSnapshot when prettier dependencies are mocked (#6776)
Browse files Browse the repository at this point in the history
## Summary
This PR fixes #6702 by seizing to local require prettier in jest-jasmine2. Instead `require(config.prettierPath)` is used. This prevents the issue where mocking native modules like `path` and `fs` that prettier depends on would cause `toMatchInlineSnapshot` to fail.

## Test plan
Run the new unit test introduced in this PR.
  • Loading branch information
tryggvigy authored and thymikee committed Jul 30, 2018
1 parent 0f525c5 commit fb2a6ac
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- `[jest-circus]` Fix retryTimes so errors are reset before re-running ([#6762](https://github.com/facebook/jest/pull/6762))
- `[docs]` Update `expect.objectContaining()` description ([#6754](https://github.com/facebook/jest/pull/6754))
- `[babel-jest]` Make `getCacheKey()` take into account `createTransformer` options ([#6699](https://github.com/facebook/jest/pull/6699))
- `[jest-jasmine2]` Use prettier through `require` instead of `localRequire`. Fixes `matchInlineSnapshot` where prettier dependencies like `path` and `fs` are mocked with `jest.mock`. ([#6776](https://github.com/facebook/jest/pull/6776))
- `[docs]` Fix contributors link ([#6711](https://github.com/facebook/jest/pull/6711))
- `[website]` Fix website versions page to link to correct language ([#6734](https://github.com/facebook/jest/pull/6734))

Expand Down
17 changes: 17 additions & 0 deletions e2e/__tests__/to_match_inline_snapshot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,20 @@ test('supports async tests', () => {
expect(status).toBe(0);
expect(fileAfter).toMatchSnapshot();
});

// issue: https://github.com/facebook/jest/issues/6702
test('handles mocking native modules prettier relies on', () => {
const filename = 'mockFail.test.js';
const test = `
jest.mock('path', () => ({}));
jest.mock('fs', () => ({}));
test('inline snapshots', () => {
expect({}).toMatchInlineSnapshot();
});
`;

writeFiles(TESTS_DIR, {[filename]: test});
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
expect(status).toBe(0);
});
3 changes: 2 additions & 1 deletion packages/jest-jasmine2/src/setup_jest_globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export default ({
expand,
getBabelTraverse: () => require('babel-traverse').default,
getPrettier: () =>
config.prettierPath ? localRequire(config.prettierPath) : null,
// $FlowFixMe dynamic require
config.prettierPath ? require(config.prettierPath) : null,
updateSnapshot,
});
setState({snapshotState, testPath});
Expand Down

0 comments on commit fb2a6ac

Please sign in to comment.