Config option for tests to only use adjacent/ascending __mocks__ directories
See original GitHub issue🚀 Feature Proposal
I think it’d be nice to have an opt-in configuration option (i.e. onlyUseAscendingMocks
or whatever), whereby an individual test will only find __mocks__
directories that are adjacent or ascending the directory tree relative to that test. By default, jest will find all __mocks__
directories and apply them to all tests.
Motivation
Especially in the case of monorepos where tests are started from the project root to test all packages, a team that supports one package could receive false positives by a mock that was implemented by a team that supports another package. Even if they were aware of the implementation, they would then have to unmock that package in all of their tests… or the team wishing to implement it would have to manually mock that package in all of theirs.
Example
Current Behavior
my-project
|-- __mocks__
|-- packages
| |-- package-a
| | |-- __mocks__
| | | |-- react-redux.js
| | |-- src
| | | | index.js
| | | + index.spec.js (will find __mocks__ folder in self, root, and package-b)
| |-- package-b
| | |-- __mocks__
| | | |-- axios.js
| | |-- src
| | | | index.js
| | | + index.spec.js (will find __mocks__ folder in self, root, and package-a)
+-- package.json
Proposed Behavior (with opt-in config option)
my-project
|-- __mocks__
|-- packages
| |-- package-a
| | |-- __mocks__
| | | |-- react-redux.js
| | |-- src
| | | | index.js
| | | + index.spec.js (will only find __mocks__ folder in self, and root)
| |-- package-b
| | |-- __mocks__
| | | |-- axios.js
| | |-- src
| | | | index.js
| | | + index.spec.js (will only find __mocks__ folder in self, and root)
+-- package.json
Pitch
Why does this feature belong in the Jest core platform?
Developer experience would be greatly improved, especially for large-scale projects and/or monorepos by decoupling packages, where code ownership by workspace may be assigned. Also, the likelihood for false positives could be mitigated or at least decreased.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:7
Top GitHub Comments
Logic is in
jest-resolver
,jest-haste-map
andjest-runtime
if you want to dabble.I like this idea! I don’t use
__mocks__
though, so cannot really comment on whether it’s a good idea or not 😅 @jeysal @thymikee @cpojer thoughts?