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.

declaring test dependencies explicitly

See original GitHub issue

Do 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 and x.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 a node x.cli.js subprocess. This case usually has a simple workaround - in x.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 file example.yml:
    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();
    });
    
    This is harder to deal with - 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:closed
  • Created 5 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jeysalcommented, May 6, 2018

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 hidden require 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.

1reaction
SimenBcommented, Apr 14, 2018

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?

Read more comments on GitHub >

github_iconTop 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 >

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