jest: --findRelatedTests don't find test file if path include special characters and file extension

πŸ› Bug Report

Hi guys,

Implementing Stryker on my repository was a really difficult task and after searching why jest give me a lot of additional work, i’m openning this issue.

I think the code responsibly to parse and find test paths with the --findRelatedTests contains (maybe, i’m not sure) a bug. Every things is explained in this other issue but to summarize, in my context i have an angular project which is running with jest. Architecture folders are like this :

β”œβ”€β”€ src (application codebase)
β”‚   β”œβ”€β”€ app
β”‚   β”‚   β”œβ”€β”€ @core (local libraries)
β”‚   β”‚   β”œβ”€β”€ @shared (shared modules)
β”‚   β”‚   β”œβ”€β”€ app
β”‚   β”‚   β”œβ”€β”€ ** (main views / containers)

if i run npx jest --findRelatedTests src/app/app.module.ts everything is ok. Jest found 2 spec files. (except 240 seconds is needed to pass them … for just 2 assertions - codebase = 29K pure ligne of code)

if i run npx jest --findRelatedTests src/app/@core/store/store.ts jest stdout this :

No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In C:\Users\xxxxxx\Projects\xxxxxxxxxxxxxxxxxxxxx
  977 files checked.
  testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 333 matches
  testPathIgnorePatterns: \\node_modules\\ - 977 matches
  testRegex:  - 0 matches
Pattern: src\\app\\@core\\store\\store.ts - 0 matches

To Reproduce

git clone https://github.com/blephy/jest-path-bug.git
cd jest-path-bug
npm i
npm run test:every

# look at the package.json to see paths

Expected behavior

Jest should understand paths with special characters and with or without file extensions

envinfo

β”œβ”€β”€ @stryker-mutator/core@5.0.1
β”œβ”€β”€ @stryker-mutator/jest-runner@5.0.1
β”œβ”€β”€ @stryker-mutator/typescript-checker@5.0.1
β”œβ”€β”€ jest@26.6.3
β”œβ”€β”€ @angular/cli@11.2.11
β”œβ”€β”€ @angular-builders/jest@11.2.0
β”œβ”€β”€ jest-preset-angular@8.4.0
β”œβ”€β”€ ts-jest@26.5.5
β”œβ”€β”€ typescript@4.1.5
β”œβ”€β”€ ts-node@8.3.0

Regards !

and many thanks about maintaining jest πŸ˜‰

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 19 (8 by maintainers)

Most upvoted comments

I’ve found the issues. It has to do with the way file patterns are handled on windows. I’ve implemented a fix in #11548 (which also fixes #9728). This merely fixes the bug, it doesn’t add support for targeting directories.

@nicojs you rock πŸ₯°

I’ve noticed that this only seems to affect Windows.

On Linux (WSL):

$ npx jest --findRelatedTests src/@core/my-lib.ts
 PASS  src/@core/my-lib.spec.ts
  Square method
    βœ“ should be defined (2 ms)
    βœ“ should return right value (3 ms)
    

$ npx jest --findRelatedTests 'src/@$_(ok)/my-lib.ts'
 PASS  src/@$_(ok)/my-lib.spec.ts
  Square method
    βœ“ should be defined (2 ms)
    βœ“ should return right value (2 ms)

I’ll try to debug to see what’s going on here.

While we’re at it: #9728 is also a big hurdle for StrykerJS users πŸ˜‡

@blephy Thanks for all your hard work on this issue πŸŽ‰

StrykerJS uses --findRelatedTests to speed up jest test execution during mutation testing considerably (since StrykerJS knows exactly which file is mutated at a particular time, it specifies that file with --findRelatedTests), but you can turn this behavior off using { "jest": { "enableFindRelatedTests": false } }, so that is the current workaround for projects that have issues. See https://stryker-mutator.io/docs/stryker-js/jest-runner#configuration.

@blephy’s suggestion would work for us. We mostly need a β€œbugfree” version of --findRelatedTests. We only use the β€œfull file path” (incl extension), so that would work.

However, from Jest’s point of view, it would be technically impossible to know the difference between path that includes a file extension vs one that doesn’t without performing disk IO. For example, β€œfoo.bar” might refer to a file with name β€œfoo” with extension β€œ.bar”, or it could refer to a directory β€œfoo.bar” which contains files. I would suggest the following:

  • If the path contains β€œmagic” (based on "testMatch" rules, see https://jestjs.io/docs/configuration#testmatch-arraystring)
    • Resolve the paths to directory entries (using node-glob or similar implementation)
  • For each listed path:
    • Resolve to a directory entry.
      • If the directory entry resolves to a directory, search recursively in that directory for files with supported extensions that DO NOT match the testPattern regex (Note: this would be optional for StrykerJS).
      • If this directory entry resolves to a file, use that
  • Run β€œfindRelatedTest” lookup on all the resolved file paths and execute those files.

I like the suggestion.

cc @SimenB