jest-community / vscode-jest

The optimal flow for Jest based testing in VS Code
MIT License
2.84k stars 294 forks source link

[BUG] Entire testing file triggered prior rather than individual test when running tests using it.each(...) #1144

Closed YasiTL closed 5 months ago

YasiTL commented 7 months ago

Describe the bug When attempting to run any test using a test.each() via the sidebar or the green play button by a test, the entire testing file runs instead of that specific test. Immediately forcing a stop will then expand all the tests, including each of the each cases, and you can then individually run the tests.

To Reproduce Steps to reproduce the behavior:

  1. Open a testing file that contains one or more tests that use it.each(...)
  2. Open the vscode-jest sidebar. Notice that tests that use it.each(...) are displayed as grey, open circles. (Image below)
  3. Run the test that uses it.each(). Notice that the entire test file runs, instead of the expected behavior of only the test using it.each(...) running, and expanding out the generated test cases. (Related GIF below)
  4. Notice that only when tests have prematurely failed/been exited out of, can one see all the test conditions that were supposed to be available for tests containing it.each().
  5. This entire process be immediately repro-ed again by making another edit in the test file.

Note: A sample repo will help us identify the bug much faster. 🙏

Expected behavior After edits of a test file, users should be able to run tests that use it.each(...) without having to run the entire test file. Tests that use it.each(...) should be picked up by the extension and labeled, and run individually.

Screenshots image All tests run instead of just the each Environment (please complete the following information):

Prerequisite


The fastest (and the most fun) way to resolve the issue is to submit a pull request yourself. If you are interested, please check out the contribution guide, we look forward to seeing your PR...

connectdotz commented 6 months ago

@YasiTL, this might sound strange, but this is actually the expected behavior. Let me explain why this happens and how people typically mitigate this issue:

Why can't I run it.each() test directly?

Why, after editing the file, are the it.each() tests back to the original state mentioned above?

Best Practice

A simple mitigation strategy is to enclose your it.each() within a describe() block with a static name. This ensures that if it.each() names remain unresolved and the extension fallbacks to the parent block, only the it.each() tests within that describe() block are executed.

I hope this explanation clarifies your concerns. If you have further questions, please feel free to ask.