declaring test dependencies explicitly
See original GitHub issueDo you want to request a feature or report a bug? Feature
What is the current behavior?
The only obvious way to declare that x.test.js
depends on (is related to) x.js
is importing/requiring ./x
in the test.
Only with this import, Jest will run x.test.js
when
- started with
--findRelatedTests x.js
or - started with
--watch
/--onlyChanged
andx.js
has changed
What is the expected behavior? Some tests have dependencies that are not visible in the import tree. Examples:
x.integration.test.js
spawns anode x.cli.js
subprocess. This case usually has a simple workaround - inx.integration.test.js
:import './x.cli.js'; // implicit dependency via spawned node process
It’s kind of ugly and needs a comment to explain why this import is needed.yaml-parser.test.js
has a test resource fileexample.yml
:
This is harder to deal with -import parseYaml from './yaml-parser.js'; import { resolve } from 'path'; test('yaml parsed correctly', () => { // parseYaml does some sort of fs.readFile('example.yml') expect(parseYaml(resolve(__dirname, 'example.yml'))).toMatchSnapshot(); });
import './example.yml';
does not work because the runtime doesn’t know what to do with the file.
Solutions / Options
- is the side effect import workaround an acceptable way to do this or should Jest provide a more explicit way to declare dependencies?
- Would it be in the scope of Jest to provide a way to declare non-module dependencies? (second example)
- how would it look?
jest.dependsOn('./x.js')
?
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Declaring dependencies - Gradle User Manual
You can declare a dependency on the TestKit API of the current version of Gradle by using the DependencyHandler.gradleTestKit() method. This is useful...
Read more >Unit Testing Dependencies: The Complete Guide
Explicit dependencies — Dependencies that you pass as arguments when you instantiate the class under test or call its methods.
Read more >Managing test dependencies - 1.67.0 - Boost C++ Libraries
Declaring a test case dependency. If an ordering dependency is required between the test units within the same suite, it has to be...
Read more >Multi-project test dependencies with gradle - Stack Overflow
It creates two dependencies in IDEA. One points to test sources another to compiled classes. And it is crucial in which order these...
Read more >Dependency Management in Gradle - Baeldung
As we saw before, we can declare the external dependencies of our source code and tests inside the dependencies block. Similarly, the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Oh, just tried it out, that actually works 🎉 I didn’t think of that before, thanks for the tip! For future reference: It appears that you also need to add the extension to
moduleFileExtensions
.However, it only seems to affect
--onlyChanged
and--findRelatedTests
, in--watch
and--watchAll
mode Jest does not watch the external resource. Maybe I’ll try to “fix” that then and submit a PR so the hiddenrequire
workaround works properly.Edit (2018-05-06): Actually, watching does work as well, it only didn’t work because I tried it with a hidden file (dotfile), which Jest does not watch.
I like the idea. Maybe we could use a docblock though, instead of a real API, similar to how we allow users to select test environment?