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
Explicitly setting modulePathIgnorePatterns to the default value breaks dontMock method #66
Comments
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 '../src/service'
describe 'service', ->
describe 'build', ->
it 'should call the service builder', ->
serviceBuilder = require '../src/service-builder'
Service = require '../src/service'
I'm not sure whether these problems are related to your one, just thought I'd post about it here 😄 |
I believe that the fix for #20 in 56c4f55 is incomplete as
Here
|
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 In my case |
For me, simply setting unmockedModulePathPatterns to the react suggested one:
seems to break |
@messfromspace are you using coffeescript by chance for your other files? It seems like haste is not loading coffeescript files properly and therefore returning |
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
^^ 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 |
Any intention on addressing this? been open for a while |
Looks like there's an open issue with Jest where due to the .haste-cache, certain configurations and usages of dontMock() cause broken automocking behavior (jestjs/jest#66). This bit me and I ended up having to wipe the .haste_cache completely. I'm opting not to use automocking and just declare mocks explicitly; hopefully that will help.
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. |
Get the RouteParser test working (had to move mock to test file because of jestjs/jest#66).
Add an express mock for routeToRouter tests (had to put mock to test file because of jestjs/jest#66).
I am still experiencing this issue. |
Not that I know of
|
I'm seeing the same issue if I specify a 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 |
This should not be an issue any more with |
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. |
Steps to repo:
npm install jquery underscore jest-cli
npm init
and accept all defaults except setting the test command to jest__tests__
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 rerunnpm test
. You'll get: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 (dbd9eb2). 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 callingdontMock()
to break mocking. These tests both pass:Showing that calling
dontMock()
causes all future requires to be unmocked. As well as:Showing that without
dontMock()
, everything still works as expected. It's the combination of settingmodulePathIgnorePatterns
, callingdontMock()
, 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.)
The text was updated successfully, but these errors were encountered: