Skip to content
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

Cannot jest.mock('module/sub/directory/file') #226

Closed
namuol opened this issue Jan 24, 2015 · 10 comments
Closed

Cannot jest.mock('module/sub/directory/file') #226

namuol opened this issue Jan 24, 2015 · 10 comments

Comments

@namuol
Copy link

namuol commented Jan 24, 2015

I'm requiring invariant in my tested code like so:

var invariant = require('react/lib/invariant');

In my tests, 'req/lib/invariant' does not get mocked, even when I explicitly call jest.mock('react/lib/invariant')

To get around this unexpected behavior, I require('./invariant'), which looks like this:

module.exports = require('react/lib/invariant');

Is there any way to require('sub/modules')?

@ivantsov
Copy link

I have the same problem. Is there any progress?

@aaronabramov
Copy link
Contributor

is it still an issue?
i just ran

var invariant = require('fbjs/lib/invariant');

describe('invariant', function() {
  it('is mocked', function() {
    expect(invariant._isMockFunction).toBe(true);
  });
});

and it worked

@ivantsov
Copy link

I'm going to have a look at the thing today in the evening. Nevertheless, I think the problem has gone because jest was rewritten, particularly a mocking part.

@ivantsov
Copy link

Hi @DmitriiAbramov, I've created a repo where you can reproduce the problem: https://github.com/ivantsov/jest-mock-nested-folders. Just run npm i & npm test.

There is a __mocks__ folder with sdk folder and foo.js file inside but Jest fails with error Cannot find module 'sdk/foo' from 'sample.js'.

Hope it helps.

@aaronabramov
Copy link
Contributor

@ivantsov i think the issue here is that you don't have the sdk module.

here is what i tried in your repo:

mkdir -p node_modules/sdk && echo "module.exports = function() { return 1; }" >> node_modules/sdk/foo.js

and the test passed

@ivantsov
Copy link

But if I just create sdk.js inside __mocks__ it'll work as expected, without actually having sdk module. This is why I think it's unexpected behavior...

@aaronabramov
Copy link
Contributor

that is true.. __mocks__/sdk.js worked, but __mocks__/sdk/foo.js didn't.
@cpojer any thoughts on that?

@vvo
Copy link
Contributor

vvo commented Jun 30, 2016

Yep, seems like any attempt to mock any require('module/submodule') mapped to mocks/module/submodule is failing on my side.

In the meantime I use jest.mock + require.requireActual

@cpojer
Copy link
Member

cpojer commented Aug 31, 2016

Yeah the solution here is to use jest.mock. We are unlikely to extend manual mocks to have sub folders for now but I might revisit this decision in the future.

@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants