Open paularmstrong opened 8 years ago
+1
Any update on this?
Also still looking for a fix so that my test files can be immported in a regular fashion, not by using the more dirty way that was suggested above.
jest.mock('models/index', () => require('models/index/_mocks_/index'));
Our directory structure looks the same as @dkundel 's referenced here https://github.com/facebook/jest/issues/2070#issuecomment-301332202 with models and components in their own namespaces with index.js
being the default export.
Would prefer to not silence warnings or throw all mocks into a flat directory. Some of our mocks are deeply nested, and the workaround suggested would likely look like
jest.mock('pages/index/components/Component', () => require('pages/index/components/Component/_mocks_/index'));
in our structure.
Any word?
@karomancer I've submitted PR #6037 which will allow you to use a configuration to remove the warnings. As of yet, it hasn't been merged; I'm waiting for a response from the contributors.
This issue is super frustrating, but I think I've found a nice workaround that results in a cleaner naming convention.
In package.json
{
"jest": {
"setupFiles": [
"<rootDir>/test.mocks.ts"
]
}
}
/* test.mocks.ts */
// modules mocked before every test
// use `jest.unmock(...)` to undo for any single test case
const mockedModules = [
"./path/to/module1/index.ts",
"./path/to/module2/index.ts",
];
mockedModules.forEach((path) => {
const mockPath = path.replace(/\.ts$/g, ".mock.ts");
jest.mock(path, () => require(mockPath));
});
This will allow you to mock anything.ts
by creating a sibling anything.mock.ts
and adding the path to the original in the top-level test.mocks
's mockedModules
array.
Why this issue tagged as enhancement?
This seems to work for me. in jest.config.js:
module.exports = {
// ...
modulePathIgnorePatterns: ["<rootDir>/.*/__mocks__"]
};
I'm not sure of the the scope or impact of this change, because I have a small project.
@amccloud thanks! This solved my problem! Details below.
I have a module helpers
in my project root directory. The helper is exporting function parseNodeFromString
.
I've created in some other module local file helpers
. Then I mocked it for one of my tests. And all of the tests using the function parseNodeFromString
started to fail with the following error:
FAIL src/some_dir/bla/tests/SomeClass.test.js
● Test suite failed to run
TypeError: (0 , _helpers.parseNodeFromString) is not a function
What about this issue? Seem like @amccloud solution is correct.
modulePathIgnorePatterns: ["<rootDir>/.*/__mocks__"]
This solution works although it made my top level mocks for node modules fail. So I changed it to not ignore my root mock folder with: "modulePathIgnorePatterns": ["<rootDir>/src/react/.*/__mocks__"],
. Still it's quite weird that mocks are not just unique based on the full path from the root. It's pretty common to have: users/helper.js
& posts/helper.js
. The warning does take quite some space, and I don't want to completely hide actual warnings.
So what is the current status of PR? Is there any proper solution or just some hacks?
In my case, the mock module was copied to Dist dir with every build.
Looks like this is a problem with typescript being unable to exclude paths/patterns that belong to deeper dir structure. Its still a problem with "typescript@^3.4.5".
To fix this I started cleaning my Dist dir with "rimraf dist" before every test run.
"test:unit": "npm run clean && stencil test --spec --snapshot",
I know its a hack, but works.
Hey i solved this here is what happened, and maybe it can help you duplicate this.
three solutions or scenarios:
1, I was editing my app on my text editor twice meaning, I was running pod install / update and react-native run-ios from two different windows. and I got this error, I tried searching for the duplicate files in Xcode and on my app but i couldn't find any. so I simply deleted the app from the simulator and re-ran react-native run-ios and it worked, It turned out that two node_modules had been duplicated as such: node_modules and node_modules0 in my scr file.
2, sometimes when I press random keys on my mac the node_modules are duplicated in for example the SRC folder so this also be the case, so it makes sense to have a look at your folders for any node_modules duplications.
3, I could'nt get my app to start until I launched and terminated a different app on the same simulator and then I rebooted the app with the duplication which then fired up without any errors.
Still nothing? Cant use modulePathIgnorePatterns in CRA
3 years and 10 months
Alternatively, it would be nice if we could force Jest to throw an error when this issue comes up, rather than just warning. Warnings are easily ignored; on my project, I'd prefer that an error be thrown, so the dev who introduced it has to deal with it.
3 years and 10 months
And one of the messages 3 years ago was "I might find some time tomorrow to cook up a patch, but no promises though"... :-P
Heh, I've left many such comments myself.
Is there no workaround (without changing your implementation). index
exports are really useful and being able to mock them throughout...
here's what I came up with 💩
// jest.setup.js
jest.mock('src/my/cool/Scope', () => require('src/my/cool/Scope/__mocks__/Scope.index'))
To ask the stupid question... Would using the whole path instead of just the filename not solve this?
"modulePathIgnorePatterns": ["<rootDir>/src/react/.*/__mocks__"],
.Still it's quite weird that mocks are not just unique based on the full path from the root.
this is what worked for me :)
When this bug will be fixed? I have __mocks__
folders mocking the same module differently in different folders that conflict with each other. The first one that is resolved when test run is executed is used for all mocking fo that module.
We have found that in spite of the warnings, our actual tests succeed and thus the individual manual index
mocks are actually being used. What then does the warnings mean?
A flag to turn off the warning would be good enough for us. Or its outright removal. There's no added value from this warning. Having two identically named files is impossible on a standard file system. My concern is that if we keep investing in manual mocks we're going to get flooded by these warnings. They make the DX poor and make our project look clunky and broken.
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.
Valid
Do you want to request a feature or report a bug? Bug
What is the current behavior?
Given a file tree:
I use the modules outside of the
modules
directory by importing them by directory name:I'd like to be able to mock
module1
andmodule2
. However, if I createsrc/app/modules/module1/__mocks__/index.js
andsrc/app/modules/module2/__mocks__/index.js
, I'm given theduplicate manual mock found
error fromjest-haste-map
.If, however, I try to create
src/app/modules/__mocks__/{module1.js,module2.js}
, the mocked files are not used.If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal repository on GitHub that we can
npm install
andnpm test
.See above behavior.
What is the expected behavior?
I would expect either approach to work, given that the first case uses different paths and the second case uses the pathname of the module.
Run Jest again with
--debug
and provide the full configuration it prints. Please mention your node and npm version and operating system.node v6.2.0 npm v3.8.9 OS X 10.11.6