Skip to content

Commit fb2a6ac

Browse files
tryggvigythymikee
authored andcommitted
fix: matchInlineSnapshot when prettier dependencies are mocked (#6776)
## 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.
1 parent 0f525c5 commit fb2a6ac

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- `[jest-circus]` Fix retryTimes so errors are reset before re-running ([#6762](https://github.com/facebook/jest/pull/6762))
1212
- `[docs]` Update `expect.objectContaining()` description ([#6754](https://github.com/facebook/jest/pull/6754))
1313
- `[babel-jest]` Make `getCacheKey()` take into account `createTransformer` options ([#6699](https://github.com/facebook/jest/pull/6699))
14+
- `[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))
1415
- `[docs]` Fix contributors link ([#6711](https://github.com/facebook/jest/pull/6711))
1516
- `[website]` Fix website versions page to link to correct language ([#6734](https://github.com/facebook/jest/pull/6734))
1617

e2e/__tests__/to_match_inline_snapshot.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,20 @@ test('supports async tests', () => {
163163
expect(status).toBe(0);
164164
expect(fileAfter).toMatchSnapshot();
165165
});
166+
167+
// issue: https://github.com/facebook/jest/issues/6702
168+
test('handles mocking native modules prettier relies on', () => {
169+
const filename = 'mockFail.test.js';
170+
const test = `
171+
jest.mock('path', () => ({}));
172+
jest.mock('fs', () => ({}));
173+
test('inline snapshots', () => {
174+
expect({}).toMatchInlineSnapshot();
175+
});
176+
`;
177+
178+
writeFiles(TESTS_DIR, {[filename]: test});
179+
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
180+
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
181+
expect(status).toBe(0);
182+
});

packages/jest-jasmine2/src/setup_jest_globals.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ export default ({
102102
expand,
103103
getBabelTraverse: () => require('babel-traverse').default,
104104
getPrettier: () =>
105-
config.prettierPath ? localRequire(config.prettierPath) : null,
105+
// $FlowFixMe dynamic require
106+
config.prettierPath ? require(config.prettierPath) : null,
106107
updateSnapshot,
107108
});
108109
setState({snapshotState, testPath});

0 commit comments

Comments
 (0)