Explicitly setting modulePathIgnorePatterns to the default value breaks dontMock method
Steps to repo:
- Create a new folder
- Run
npm install jquery underscore jest-cli - Run
npm initand accept all defaults except setting the test command to jest - Place this following test into
__tests__
describe('SomeTest', function() {
it("requires underscore (unmocked) and jQuery (mocked)", function() {
jest.dontMock('underscore');
var _ = require('underscore');
var $ = require('jquery');
expect(_.mock).toBeUndefined();
expect($.mock).toBeDefined();
})
});
If you run npm test it will pass, as you'd expect. Now edit the generated package.json to add "jest": {"modulePathIgnorePatterns": ["/node_modules/"]}, and rerun npm test. You'll get:
cody@possum jest-test $ npm test
> [email protected] test /Users/cody/Coding/jest-test
> jest
Found 1 matching tests...
FAIL __tests__/test.js (0.058s)
● SomeTest › it requires underscore (unmocked) and jQuery (mocked)
- Expected undefined to be defined.
at Spec.<anonymous> (/Users/cody/Coding/jest-test/__tests__/test.js:8:24)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
1 tests failed, 0 tests passed (1 total)
Run time: 0.761s
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0
In other words, setting modulePathIgnorePatterns to ["/node_modules/"] causes don'tMock() method to break mocking. This is true despite the fact that, according to the docs this is the default pattern.
I have confirmed this on the current version of jest on NPM, as well as the current HEAD commit (dbd9eb2eb2de1d04fb9510dc653bdfc063715ead). I have confirmed that setting modulePathIgnorePatterns to something like `["/nonsense_path/"] makes everything work.
In addition, note that the issue is that setting modulePathIgnorePatterns is causing calling dontMock() to break mocking. These tests both pass:
describe('SomeTest', function() {
it("requires underscore (unmocked) and jQuery (mocked)", function() {
jest.dontMock('underscore');
var _ = require('underscore');
var $ = require('jquery');
expect(_.mock).toBeUndefined();
expect($.mock).toBeUndefined();
})
});
Showing that calling dontMock() causes all future requires to be unmocked. As well as:
describe('SomeTest', function() {
it("requires underscore (unmocked) and jQuery (mocked)", function() {
var _ = require('underscore');
var $ = require('jquery');
expect(_.mock).toBeDefined();
expect($.mock).toBeDefined();
})
});
Showing that without dontMock(), everything still works as expected. It's the combination of setting modulePathIgnorePatterns, calling dontMock(), and then trying to require something that should be mocked that's broken.
(I originally submitted the bug as #64, which I've closed because it turns out I was completely wrong about the actual issue.)
I seem to be getting a problem similar to what you described with the following config:
"jest": {
"testDirectoryName": "test",
"scriptPreprocessor": "test/.preprocessor.js",
"testFileExtensions": [
"coffee",
"js"
],
"moduleFileExtensions": [
"coffee",
"js"
]
}
Calling jest.dontMock() seems to disable mocking on all require's, for example:
jest.dontMock '../src/service'
describe 'service', ->
describe 'build', ->
it 'should call the service builder', ->
serviceBuilder = require '../src/service-builder'
Service = require '../src/service'
serviceBuilder is my actual service builder instead of a mock, so I have to avoid using jest.dontMock and use require.requireActual instead.
I'm not sure whether these problems are related to your one, just thought I'd post about it here :smile:
I believe that the fix for #20 in 56c4f5569ef771531f5dae35ac78f991e6ac9385 is incomplete as _getNormalizedModuleID will still return user:: for modules excluded from the resource map. While it does try to lookup the moduleResource:
if (realAbsPath === null) {
var moduleResource = this._getResource('JS', moduleName);
if (moduleResource) {
realAbsPath = moduleResource.path;
}
}
Here _getResource returns undefined. I believe this is because it only consults the Haste resourceMap, from which the file we're after has been explicitly excluded based on modulePathIgnorePatterns (which defaults to empty not "/node_modules/" as stated in the docs.)
Loader.prototype._getResource = function(resourceType, resourceName) {
var resource = this._resourceMap.getResource(resourceType, resourceName);
// TODO: Fix this properly in node-haste, not here :(
if (resource === undefined && resourceType === 'JS' && /\//.test(resourceName)
&& !/\.js$/.test(resourceName)) {
resource = this._resourceMap.getResource(
resourceType,
resourceName + '.js'
);
}
return resource;
};
I have this issue with every module and not only with npm modules. So if I write something like this:
describe "Whatever", ->
it "does something", ->
jest.dontMock "../path/to/a"
a = require "../path/to/a"
b = require "../path/to/b"
expect(a.mock).toBeUndefined()
expect(b.mock).toBeDefined()
Test fails because b doesn't have mock property. So I need to use something like jest.mock "../path/to/b" or a = require.requireActual "../path/to/a" without dontMock to make it work as expected.
In my case dontMock is broken and disables mocks for every module.
Sorry for my bad English.
For me, simply setting unmockedModulePathPatterns to the react suggested one:
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react"
],
seems to break jest.dontMock completely as well, in that nothing else auto mocks after that one dontMock. This seems like a pretty severe issue, I'm surprised many other haven't been hitting it.
@messfromspace are you using coffeescript by chance for your other files? It seems like haste is not loading coffeescript files properly and therefore returning user:: as the module id for anything coffee... so when you dontmock one, it is the same as not mocking the others.
Just would like to note that the original bug (Explicitly setting modulePathIgnorePatterns to the default value breaks dontMock method) is likely a documentation issue. The real default for this is just an empty array. Ignoring the actual node_modules folder will completely break mocking (because it needs to load into haste to mock it).
@pselden In my experience, the bug interacts with testPathDirs as well.
testPathDirs
A list of paths to directories that Jest should use to search for tests in.
There are times where you only want Jest to search in a single
sub-directory (such as cases where you have a src/ directory in your repo),
but not the rest of the repo.
^^ That sounds like it should only affect the discovery of test files.
It's an additionally painful ergonomics issue because the two settings look like ripe targets for improving Jest's poor startup time. And yes - excluding node_modules from testPathDirs does seem to make it run faster, but you've got this latent bug where the vaunted auto-mocking goes bonkers.
Any intention on addressing this? been open for a while
I did some digging,
Setting modulePathIgnorePatterns causes Loader.prototype._getNormalizedModuleID in HasteModuleLoader.js to always return "user::" as the module id. This isn't a problem until you use dontMock and add an entry in HasteModuleLoader's this._explicitShouldMock. This entry looks like "user::": false.
When you require a module after using modulePathIgnorePatterns and using dontMock, the loader when it calls _shouldMock, always returns false because it calls _getNormalizedModuleID, which always returns "user::".
I think when using modulePathIgnorePatterns something goes wrong generating the resource map. Not sure though
Edit: Mocking started working again when I added "testFileExtensions": ["js", "jsx"], to my config
Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.
I am still experiencing this issue.
Not that I know of On Sep 9, 2015 9:09 PM, "Peter Jihoon Kim" [email protected] wrote:
This is still a problem
— Reply to this email directly or view it on GitHub https://github.com/facebook/jest/issues/66#issuecomment-139111112.
I'm seeing the same issue if I specify a testPathDirs ... even if it is "testPathDirs": ["__tests__"]
After removing that from my config, everything works fine.
I have the exact same problem described by @AlcockP. Removing testPathDirs resolved the problem and everything is mocked properly.
+1 on the problem with testPathDirs. Replacing dontMock() with require.requireActual('path') seems to be a viable workaround.
This should not be an issue any more with jest 0.7+.
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.