-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
moduleNameMapper - mocks do not work for imported modules of mapped modules using "moduleNameMapper" #4262
Comments
I think "it works" but it's broken maybe? Example 1: it worksModule with something to mock// src/moduleWithSomethingToMock.js
import moduleToMock from '../../rootAlias/helpers/moduleToMock'; Testimport moduleWithSomethingToMock from './moduleWithSomethingToMock';
// Here I'm using the alias
jest.mock('rootAlias/helpers/moduleToMock'); Example 2: doesn't workModule with something to mock// src/moduleWithSomethingToMock.js
// Here I'm using the alias
import moduleToMock from 'rootAlias/helpers/moduleToMock'; Testimport moduleWithSomethingToMock from './moduleWithSomethingToMock';
// Using or not the alias, it doesn't mock the module
jest.mock('rootAlias/helpers/moduleToMock'); |
Does it happen on |
It does 😿 |
If someone can find it interesting: in my case the problem was that the order of entries in |
I had experienced similar issues with jest v20.0.4 upgrading to v22.2.2 solved that issue. |
We're hitting this issue as well - upgrading to 22.2.2 or downgrading to 20.0.4 didn't work. Some details:
Unfortunately, that's as far as I got. I attempted a fix but since I've only started working with this framework every change I made broke a bunch of tests... Hopefully this helps someone find the true fix. I'm also happy to help put in a PR with some direction on what the fix should be. |
First and foremost, thanks to @timtrinidad for the workaround (passing a factory to Second, expanding on what @timtrinidad found, I'm on 22.4.0 of
So, there's a couple problems here. First, at step 4, it's determined whether or not we're dealing with a manual mock. In the use case that we're discussing here, we're not. But However, we have a new problem. The first line of |
I took a walk and tried something when I got back. I changed Rolled the two lines I changed back and working on a test. Hopefully I'll have a PR shortly. |
Having trouble running the tests. Reached out on discord. Once I can sort that out, I can submit the PR. |
I can't get the tests to run on my machine and haven't had any luck getting help with them. I was able to run them with my changes on Travis and my change breaks other things. Without being able to run them locally, it'll take way more time than is worth it to get a fix that doesn't break the existing tests. So, I'm shelving this for the time being. |
Does anyone have any new information about this, or know whether it's going to be fixed before the next release? The same problem occurs when using a custom resolver. We use aliases/nonrelative paths almost exclusively so this is a big issue for us adopting jest. As of
I've tried to reproduce it in isolation but haven't been able to yet, using both relative & aliased/rooted paths for the original import. So it's doesn't seem like it's quite as simple as how @alex-mironov initially observed it, or maybe it manifests differently for when using a custom resolver? Additional info: After a bit of tracing I think this is a result of circular dependencies, which while probably not good are valid in ES6 modules. If I can repro in isolation will file a specific issue. |
Update: Figured out the cause here. I'm not actually sure if this is a bug. I have a module that depends on another module which fails when loaded in the testing environment, because of globals that aren't defined. Automocking tries to load all the modules, and fails when it gets to a dependent module that fails while loading. So for example
The test code has
I would have expected that
This makes sense, in a way, but I guess a future enhancement might be to use an AST to parse the public API of mocked modules instead of trying to load them. Having to actually load a module to mock it seems likely to fail in some scenarios. It's possible I just don't understand how this feature is expected to be used. Another enhancement would be to provide output that the test failed during the module resolution/mocking process-- it's not at all clear that's what's happening; when looking at the stack trace, it just looks like the module didn't get mocked, but it's actually failing while trying to load dependencies of the module during mocking. In any event the problem I'm having doesn't seem to be related to this bug any more, so it's possible this has been fixed already. |
Hello everyone, I just ran into this issue today. Is there anything I can do to help push this issue forward? |
got the same issue |
That's doesn't really seem feasible - to actually support all ways of
That makes a lot of sense to me, would you mind creating a PR for that? Maybe provide some hint that you might not be able to rely on automocking and that you have to provide a mock factory inline? |
You're completely correct of course - I write almost exclusively ES201?/TypeScript these days and when I wrote that, in my mind, a module's public API would always be well-defined ;)
I can take a shot for sure when I get a little free time. |
I've made a minimal example repo where the ModuleNameMapper does not resolve correctly for imports. https://github.com/Kruptein/jest-test-stuff Running 'npm run test' results in: Additionally this output shows that something does not seem to resolve correctly:
|
I have to confirm that currently Jest has serious issues with aliases defined in a webpack config:
This issue is 1.5 years old. Will it be ever fixed? |
I can confirm I have the same problems stated by @pribilinskiy. |
Jest does not support webpack aliases - you need to use That said, I dug into this a tiny bit, and I'm not sure why it fail 😅 However, I've put together a failing integration test based on @Kruptein's excellent reproduction (removing typescript and logging), so maybe it will lower the barrier for people wanting to send a PR to fix this 🙂 Please see #7668 |
We import a shared json file with aliases in webpack.config.js and jest.config.js |
I can try to tackle this one. |
@grosto that would be awesome 👍 |
I was facing this issue with {
"jest": "23.5.0",
"ts-jest": "23.1.3"
} I just update both dependencies to 24.7.1 and 24.0.2 and solved |
I'm having this issue with UPDATE: |
is there any solution for this issue |
I have the same issue, want to use
|
Possible solution for some of you:If it's any help to anyone, I just got mine working with this: In my file that is importing the module: import { TransactionEmail } from 'mail/transactional-email' Then in my jest config: moduleNameMapper: {
// Force mock import.
"mail/transactional-email": "<rootDir>/src/mail/__mocks__/transactional-email.ts",
// Normal module aliases.
"^mail/(.*)": "<rootDir>/src/mail/$1",
"^api/(.*)": "<rootDir>/src/$1"
}, And then you don't call For me, I am on latest versions of jest/ts-jest but when I call To force jest to import the mock module I just mapped the module import path directly to the mocked module. Not exactly a great solution, but I wanted to share regardless if anyone is in a pinch. To be honest, the docs for module mocking are really confusing in my opinion I think a big part of the issue here might just be outdated or difficult to understand documentation. My understanding here was that if you say to mock I hope this helps someone else! 🍻 |
Could you reopen the issue? I use the same workaround as @thom801 for now: ../src/mocks/store.js:
.../test/jest/jest.config.js:
|
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. |
I use jest with webpack. Webpack is configured to use alias for some imports:
some-lib
is git submodule added to the project.part of Jest config from
package.json
:When I'm trying to mock imported module in jest it doesn't work
as a result
utilFunc
is unmocked.I'm using the latest version of jest.
The text was updated successfully, but these errors were encountered: