Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
YasiTL opened this issue Apr 25, 2024 · 1 comment
Labels
expected-behavior this is the expected behavior (no fix needed)

Comments

@YasiTL
Copy link

YasiTL commented Apr 25, 2024

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):

  • vscode-jest version:[v6.2.4]
  • node -v: [v20.10.0]
  • npm -v or yarn --version: [npm 10.2.3]
  • jest or react-scripts (if you haven’t ejected) version: [jest 29.4.3]
  • your vscode-jest settings:
    • jest.jestCommandLine? [npx jest]
    • jest.runMode? [on-demand]
    • jest.outputConfig? [none]
    • anything else that you think might be relevant? [none]
  • Operating system: [Ubuntu 22.04.3 LTS]

Prerequisite

  • are you able to run jest from the command line? [yes]
  • where do you run jest CLI from? [root directory of the project]
  • how do you run your tests from the command line? npx jest int or npx jest unit

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
Copy link
Collaborator

@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?

  • Jest runs individual tests based on their "name" — the description you assign to each test. For it.each(), the descriptions often contain dynamic content (like "if $input expects $output") to differentiate tests. Before running the tests, these dynamic names are unresolved. If you pass such an unresolved name to Jest, it won't recognize it, and the test won't run. To prevent this, the extension fallbacks to the test's parent block when faced with unresolved names. In your case, it seems the parent block is the file itself; thus, all tests in that file are executed.
  • After execution, the names in it.each() are resolved, allowing individual tests to be run as you've observed.

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

  • When a test file is edited, the extension, which performs only static lexical analysis, cannot determine if the changes affect the dynamically named test names. Therefore, they revert to the "unresolved" state and will resolve again upon the next execution.

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.

@connectdotz connectdotz added the expected-behavior this is the expected behavior (no fix needed) label May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
expected-behavior this is the expected behavior (no fix needed)
Projects
None yet
Development

No branches or pull requests

2 participants