question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

toMatchInlineSnapshot fails if prettier's native module deps are mocked through jest.mock

See original GitHub issue

🐛 Bug Report

toMatchInlineSnapshot fails if path module is mocked

jest.mock('path', () => ({}));

test('foo', () => {
  expect({}).toMatchInlineSnapshot();
});

results in

● Test suite failed to run

    TypeError: path.resolve is not a function

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS High Sierra 10.13.5
    CPU: x64 Intel(R) Core(TM) i7-6567U CPU @ 3.30GHz
  Binaries:
    Node: 8.9.4 - ~/.nvm/versions/node/v8.9.4/bin/node
    Yarn: 1.7.0 - /usr/local/bin/yarn
    npm: 5.6.0 - ~/.nvm/versions/node/v8.9.4/bin/npm
  npmPackages:
    jest: ^23.4.0 => 23.4.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
tryggvigycommented, Jul 24, 2018

Thanks for explaining @aaronabramov! Also, unfortunately yes I can reproduce this on master.

Here is the test I added to e2e/__tests__/to_match_inline_snapshot.test.js and reproduces the bug on latest master.

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);
});
1reaction
tryggvigycommented, Jul 24, 2018

I’m trying to dig into this. I’ve got a test that reproduces the bug in e2e/__tests__/to_match_inline_snapshot.test.js.

The error is coming from the compiled node_modules/prettier/third-party.js and originally from this line in cosmiconfig dependency. Thats the code thats trying to run when TypeError: path.resolve is not a function happens.


node_modules/prettier/prettier-bin.js is right now using these native modules

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 jest.mock will overrider any of the above. I’m unsure how to actually fix this issue (looking at https://github.com/facebook/jest/pull/6687 for inspiration) but I’ll keep digging. I’d appreciate any input from folks with more context.

update jest.mock only causes failure on some of them

mock path -- TypeError: path.resolve is not a function
mock fs -- TypeError: fs.statSync is not a function
mock os -- TypeError: os.homedir is not a function
mock assert -- TypeError: assert is not a function
mock utils -- passes
mock readline -- passes
mock stream -- passes
mock module -- passes
Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Jest
Node.js core modules, like fs , are not mocked by default. ... For example, with the following configuration jest will fail if there...
Read more >
Jest: Error when trying to import Native Modules
I found the solution to this. I had been worried that perhaps the mocks weren't being tracked through all the import statement.
Read more >
jest-mock | Yarn - Package Manager
Creates a new module mocker that generates mocks as if they were created in an environment with the given global object.
Read more >
De-mystifying Jest Snapshot Test Mocks | by Albert Brand
I hope you now have a clear understanding of what's happening when a module is mocked with Jest and that you're able to...
Read more >
Snapshot Testing - Jest
Snapshot tests are a useful tool when you want to make sure your UI doesn?t change unexpectedly.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found