question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:7

github_iconTop GitHub Comments

1reaction
SimenBcommented, May 4, 2020

Logic is in jest-resolver, jest-haste-map and jest-runtime if you want to dabble.

1reaction
SimenBcommented, Apr 27, 2020

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Jest
The bail config option can be used here to have Jest stop running tests after n failures. ... The directory where Jest should...
Read more >
Using Mock to test package builds - Fedora Project Wiki
First, install the mock package, which is available from Fedora Package Collection. Default configuration. When using mock, you can use the " ...
Read more >
Build local unit tests - Android Developers
To add a mock object to your local unit test using Mockito, follow this programming model: Include the Mockito library dependency in your...
Read more >
test ach numbers
Instead, use the following test bank account numbers to create an ACH ... If you are setting up automatic payments for your phone...
Read more >
Test Runner: CLI and Configuration - Modern Web
When the files option is specified on the top-level config, a default test group is created default . You can run only this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found