Comments (17)
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 😄
from jest.
I believe that the fix for #20 in 56c4f55 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;
};
from jest.
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.
from jest.
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.
from jest.
@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.
from jest.
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).
from jest.
@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.
from jest.
Any intention on addressing this? been open for a while
from jest.
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
from jest.
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.
from jest.
I am still experiencing this issue.
from jest.
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
#66 (comment).
from jest.
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.
from jest.
I have the exact same problem described by @AlcockP. Removing testPathDirs resolved the problem and everything is mocked properly.
from jest.
+1 on the problem with testPathDirs
. Replacing dontMock()
with require.requireActual('path')
seems to be a viable workaround.
from jest.
This should not be an issue any more with jest 0.7+
.
from jest.
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.
from jest.
Related Issues (20)
- [Bug]: ReferenceError: CSS is not defined HOT 1
- [Feature]: Option to support `Error#cause` in snapshots for Jest29 HOT 4
- [Bug]: Using `projects` option does not "copy the root-level configuration options to each individual child configuration during the test run"
- [Feature]: The ability of `toHaveProperty` to parse dots in property name is so evil. Please remove it. HOT 1
- [Bug]: Error while running watch
- OpenJS Onboarding: Document Core Infrastructure By Filling Out Google Form HOT 1
- [Feature]: Support `import.meta.resolve` HOT 3
- [Feature]: Global / system wide config ?
- OpenJS Onboarding: Publish Security Policy
- OpenJS Onboarding: Create & Publish Project Charter
- [Bug]: `advanceTimersByTimeAsync` has inconsistent behavior regarding `setTimeout(x, 0)` HOT 2
- Why do my unit tests slow down after 26 upgrading to the latest version of jest HOT 1
- [Bug]: Reporter interface has onTestCaseStart, but that hook is never called
- [Docs]: Difference between runner and testRunner? HOT 3
- [Bug]: Jest with ECMAScript modules importing a package that is itself an ECMAscript module fails HOT 2
- [Bug]: Validation warning for option "reporters" when using jest-junit to generate junit report HOT 1
- [Bug]: Jest asserts truthy when comparing undefined with Map
- [Feature]: Tagging test block failures with 'step' description
- [Feature]: outputFile support for listTests option HOT 1
- [Bug]: `structuredClone` under jest fails `instanceof` check HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jest.