-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
toMatchInlineSnapshot fails if prettier's native module deps are mocked through jest.mock #6702
Comments
I wonder if this might be fixed by #6687? @aaronabramov wdyt? EDIT: Hmm, no. I tried applying that patch locally, and it still fails π |
no, this seems like a separate issue, but comes from the same bug. |
I'm trying to dig into this. I've got a test that reproduces the bug in The error is coming from the compiled
var path = _interopDefault(require('path'));
var os = _interopDefault(require('os'));
var assert = _interopDefault(require('assert'));
var fs = _interopDefault(require('fs'));
var util = _interopDefault(require('util'));
var events = _interopDefault(require('events'));
var thirdParty = require('./third-party');
var thirdParty__default = thirdParty['default'];
var readline = _interopDefault(require('readline'));
// and these two through ./third-party
var stream = _interopDefault(require('stream'));
var module$1 = _interopDefault(require('module')); I'm assuming update jest.mock only causes failure on some of them
|
path
module is mocked
@tryggvigy hey! the issue comes from the fact that we have two environments we execute Jest code in:
because most of our framework code is in the outer environment we're able to mock native/whatever modules from inside the VM and not affect the framework code. unfortunately inline snapshots bring a lot of logic that is executed in the inner scope (inside the VM) and interacting with the environment makes it possible to break framework code. the right fix for this is to take everything framework related to the outer scope and pass a pure/stateless function to the VM |
@tryggvigy actually! since you found that the issue comes from inside prettier my PR might have fixed it (cause i did take prettier to the outer scope) |
Thanks for explaining @aaronabramov! Also, unfortunately yes I can reproduce this on master. Here is the test I added to test('fails', () => {
const filename = 'mockFail.test.js';
const test = `
jest.mock('path', () => ({}));
test('hæhæ!', () => {
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);
}); |
I noticed that this is not an issue in jest-circus. |
ugh.. that might be my fault :) |
Yeah, thats it :) Test passes now, I'll make a PR |
Closing the issue. It's fixed once #6776 is merged |
## 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.
@tryggvigy thanks for digging into it! :) |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
π Bug Report
toMatchInlineSnapshot fails if
path
module is mockedresults in
Run
npx envinfo --preset jest
Paste the results here:
The text was updated successfully, but these errors were encountered: