-
-
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
[bug] duplicate manual mock found in separate directories #2070
Comments
+1 |
In my case, after clear the |
Here's the offending code: But it looks like the behavior might be explicitly wanted as there is a test confirming it: Which was added in: I wonder why we are using /cc @flarnie |
This means that import { MyWhatever } from 'models/MyWhatever/schema';
import { MyOtherWhatever } from 'models/MyOtherWhatever/schema'; and use manual mocks at the same time. Jest will currently see them both as mocking Though the workaround is trivial (s/schema/MyWhateverSchema/), it feels like a bug to rename and restructure non-test code to make jest happy 🐞 . |
Yes this does suck indeed. The manual mocking system is really not good and I'm happy to accept PRs that will improve the situation, assuming we can make sure we don't break all of FB (but I can take care of that :) ) |
Cool. I might find some time tomorrow to cook up a patch, but no promises though 😅 |
@cpojer is there a particular reason for this behaviour? Could it have anything to do with the fact that fb uses unique filenames for modules? I don't see otherwise how not allowing two mocks with the same name makes any sense... |
Yes, mocks are "global" as well. This is a terrible design that we have to live with, unfortunately. At FB we have 4000+ mock files in the wrong location (and often there isn't even a proper location). It is likely we will fix this early next half, so this should improve in Jest. I'm happy to support PRs that improve the behavior in Jest for open source – if we can retain the old behavior for Jest at FB for now. |
@cpojer how about a flag? Would you accept a pr that enables/disables this with a flag? |
Yeah, it should be a config option. But I'm not just talking about the warning, I'm also talking about the feature in general. |
@cpojer right - which parts of jest does this touch? |
The resolution code is called from jest-runtime: https://github.com/facebook/jest/blob/master/packages/jest-runtime/src/index.js and is somewhere in jest-resolve and jest-resolve-dependencies. |
@cpojer thanks for the pointers 👍 |
@cpojer what about some global override, like At least, we can enjoy non-unique filenames, and it will not break anything in FB. Of course, it's a temporary solution. I mean, this is in some export JEST_USE_BASENAME_FOR_CACHING="true" (or some file with env)
or this one, without env file modifications:
What you think? It's a sort of hack or it's ok? 😉 |
@cpojer if FB code has the restriction on the uniqueness of the names, using the full path as a key for the map of mocks shouldn't bring problems. Am I right or there is something I'm not seeing? |
The two solutions I see are:
|
it would be nice to see @cpojer answer on it |
Hey everyone, sorry for the delay, I'm pretty backed up with a ton of stuff right now. I think I'm fine if you guys decide to do whatever breaking change in Jest that is necessary to improve this system. Manual mocking is really messed up and doesn't work well. One thing we kind of want to do is limit the scope of "haste modules" (internal FB module system) using a config option, like One thing to be figured out then, is this: If all manual mocks are local, like
So to summarize:
What do you think? |
In order:
HURRAY 😄 🎉
I'm not sure I understand the needs with haste. "haste_modules:" ["/path_1/a", "/path_2/c"] only I'd say, targets could easily be specific files and entire directories, even with single/double
I'd maintain the current behavior:
|
My thoughts: haste modules:I think ,
node_modules@EnoahNetzach what if someone have same names for app module and module from node_modules? To make it work without haste... hm, I think it can be described like:
(of course, Really, mocking node_modules module is a rare case by my experience, so... may be rareness may compensate ugliness in particular case of mocking node_modules ? Anyone mocking modules inside of node_modules often? what about to think furtheras I noticed, for react-native projects, we often have to mock this means, we have:
what I want to say: it would be very nice to have ability to auto mock modules on some paths. It can be implemented around well-known data structure array-of-jest-globs in config, and filtering modules upon it. I will describe that step by step Given this config entry
and this code at
and this test code for screen at
We come to this situation, in context of
... You can answer, how it will play with simply saying,
auto mocking... wait!may be just introduce special value for for example, with this config entry:
jest will auto mock all application modules, and leave actual versions for modules from what do you think about app level modules automocking, @cpojer ? I find it very efficient for my particular case. |
I fully agree with We personally don't use automocking that much, so I can't say what's better, my wild guess is that the The |
may be just throw, if someone has same module name in e.g.
in this case, nah... this is more ugly than |
What I meant is: what if you have an The naming collision is usually handled in node by preferring the nearest, but I don't know how this works in haste. I'm bringing this up because projects like Create React App are using it, or will do in the near future. |
Let's keep this focused on changing how haste works (whitelist/blacklist rather than on by default). I do think I'd prefer to maintain that |
I should be able to work on this no sooner than the next weekend. |
I can work on PR this sunday, I'm kinda free @cpojer just to recap - create |
I think these are some larger changes on the way to get this resolved but I think we need both the singular globalMocks option (could be a string or array of strings) and the hasteModules option which would be an array of paths of haste modules. Most of this code lives in jest-haste-map and jest-resolve. I'm not 100% sure what the right solution will look like yet.
…________________________________
From: Max Sysoev <notifications@github.com>
Sent: Friday, December 9, 2016 8:18:44 AM
To: facebook/jest
Cc: Christoph Pojer; Mention
Subject: Re: [facebook/jest] [bug] duplicate manual mock found in separate directories (#2070)
I can work on PR this sunday, I'm kinda free
@cpojer<https://github.com/cpojer> just to recap - create globalMocks config entry with default value of <rootDir>/__mocks__. This option regulates use of node-haste within jest by specifying path? Or it will be array of paths?
-
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#2070 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AAA0KAMFc34iKqBDLHZzgaGHqyc3WkAzks5rGQ7kgaJpZM4Kt2DW>.
|
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.
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. |
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). |
here's what I came up with 💩
|
To ask the stupid question... Would using the whole path instead of just the filename not solve this? |
this is what worked for me :) |
When this bug will be fixed? I have |
We have found that in spite of the warnings, our actual tests succeed and thus the individual manual |
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
The text was updated successfully, but these errors were encountered: