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

Using 'dontMock()' on a npm-module disables mocking for all npm-modules. #20

Closed
jbaiter opened this issue May 15, 2014 · 3 comments
Closed

Comments

@jbaiter
Copy link

jbaiter commented May 15, 2014

Consider this test:

describe('SomeTest', function() {
  it("requires underscore (unmocked) and jQuery (mocked)", function() {
    jest.dontMock('underscore');
    var _ = require('underscore'),
        jQuery = require('jquery');
    expect(_.mock).toBeUndefined();
    expect(jQuery.mock).toBeDefined();
  })
});

It fails because jQuery.mock is undefined.

The issue can easily reproduced by slightly modifying the included examples/tutorial/__test__/fetchCurrentUser-test.js, dropping in a single dontMock('underscore') call:

// fetchCurrentUser-test.js, l.4-11
it('calls into $.ajax with the correct params', function() {
    jest.dontMock('underscore');
    var $ = require('jquery');
    var fetchCurrentUser = require('../fetchCurrentUser');

    // Call into the function we want to test
    function dummyCallback() {}
    fetchCurrentUser(dummyCallback);

The test will now fail since jQuery is no longer mocked.

The root cause for this is that HasteModuleLoader._getNormalizedModuleID() seems to be returning user:: for any module in node_modules, making dontMock effectively disable auto-mocking for all npm-installed modules.

@jeffmo
Copy link
Contributor

jeffmo commented May 15, 2014

Ok, I think this should be fixed by 56c4f55
(I was able to confirm that it fixes your repro-case -- feel free to re-open if you're seeing any other related issues)

@jeffmo jeffmo closed this as completed May 15, 2014
@jeffmo
Copy link
Contributor

jeffmo commented May 15, 2014

Thanks a ton for the report and narrowing the issue down to HasteModuleLoader._getNormalizedModuleID() by the way -- that was really helpful!

@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

2 participants