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

How to ignore files on jest watch mode #2516

Closed
noyobo opened this issue Jan 6, 2017 · 13 comments
Closed

How to ignore files on jest watch mode #2516

noyobo opened this issue Jan 6, 2017 · 13 comments

Comments

@noyobo
Copy link

noyobo commented Jan 6, 2017

Do you want to request a feature or report a bug?

  • feature

What is the current behavior?

jest --watch my tests will create new files on project directory. jest rerun when watch new files. run tests loop ......

I try set testPathIgnorePatterns and testPathDirs exclude the new files directory, but same way.

kapture 2017-01-06 at 22 50 19

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

What is the expected behavior?

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

  • yarn 0.18.1
  • jest 16.0.2
  • node 7.3.0
@orta
Copy link
Member

orta commented Jan 6, 2017

Looks like the coverageDirectory option should help you there.

I'm also seeing this danger/danger-js#82, so this is how I debugged it - if you edit your node_modules you can make this function console.log what file is triggering the restart

@orta
Copy link
Member

orta commented Jan 6, 2017

And as I'm interested in specific answers too, my problem was that something was making my fixtures change ( I assume it's the typescript compiler )

screen shot 2017-01-06 at 11 01 18 am

I fixed it by doing this in the package.json

...
 "coveragePathIgnorePatterns": [
      "/node_modules/",
      "(.test)\\.(ts|tsx|js)$",
      "/distribution/.*\\.(ts|js)$"
    ]
...

@cpojer
Copy link
Member

cpojer commented Jan 6, 2017

This should fix it: #2362

@noyobo
Copy link
Author

noyobo commented Jan 6, 2017

const onFileChange = (_, filePath: string) => {
  filePath = path.join(root, filePath);
  const coverageDirectory =
    config.coverageDirectory ||
    path.resolve(config.rootDir, 'coverage');
  const isValidPath =
    config.testPathDirs.some(dir => filePath.startsWith(dir)) &&
    !filePath.includes(coverageDirectory);

Look at this condition !filePath.includes(coverageDirectory), the change filepath must includes coverageDirectory.

But coverageDirectory option should output jest coverage files.

I see you use os.tmpdir() to avoid this problem.

@cpojer cpojer closed this as completed Feb 25, 2017
@cpojer
Copy link
Member

cpojer commented Feb 25, 2017

Jest 19 is out now.

@tivac
Copy link

tivac commented Mar 16, 2017

I'm running into this, I'm unable to use jest watch because my test files clean up the test output dirs using afterAll and watch doesn't like that at all.

D:\git\modular-css>npm test cli -- --watch

> [email protected] test D:\git\modular-css
> jest "cli" "--watch"

 PASS  packages\cli\test\cli.test.js
  /cli.js
    √ should show help with no args (304ms)
    √ should default to outputting to stdout (348ms)
    √ should support outputting to a file (342ms)
    √ should support outputting compositions to a file (336ms)
    √ should return the correct error code on invalid CSS (338ms)

Snapshot Summary
 › 1 obsolete snapshot found, press `u` to remove them.

Test Suites: 1 passed, 1 total
Tests:       5 passed, 5 total
Snapshots:   4 passed, 4 total
Time:        2.835s, estimated 3s
Ran all test suites matching /cli/.

Watch Usage
 › Press a to run all tests.
 › Press o to only run tests related to changed files.
 › Press u to update failing snapshots.
 › Press p to filter by a filename regex pattern.
 › Press t to filter by a test name regex pattern.
 › Press q to quit watch mode.
 › Press Enter to trigger a test run.
events.js:163
      throw er; // Unhandled 'error' event
      ^

Error: EPERM: operation not permitted, lstat 'D:\git\modular-css\packages\cli\test\output\classes.json'

Ideally I can tell jest to ignore all output dirs for watching files, but coveragePathIgnorePatterns description doesn't make it sound like the right thing.

Is coveragePathIgnorePatterns really the right approach?

@tivac
Copy link

tivac commented Mar 16, 2017

Doesn't seem like it, I get the same error with the following added to my package.json.

"jest": {
    "coveragePathIgnorePatterns": [
      "node_modules",
      "output"
    ]
  }

@fathyb
Copy link

fathyb commented Mar 21, 2017

Same here using Jest 19.0.2, stuck in an infinite watch loop without --coverage, my tests are in src/ and creates files in tests-data/. Using testPathIgnorePatterns: [/tests-data/, /node_modules/] doesn't seems to do the trick, neither do coveragePathIgnorePatterns.

@reergymerej
Copy link

watchPathIgnorePatterns is the current way.

@tivac
Copy link

tivac commented Jul 18, 2018

I actually have that set up already, but it still doesn't work, I get crashes any time I try to use jest --watch.

https://github.com/tivac/modular-css/blob/d459beb2f41d389bd397a73821b59135b0e9f791/jest.config.js#L13-L16

@marcinczenko
Copy link

marcinczenko commented Mar 18, 2020

I am using a monorepo. I have a fixture folder where I create temporary files used by some tests. I can't get watchPathIgnorePatterns working. Does not matter if I keep on the top level of the jest.config.js, or inside specific project entry in projects. If I run yarn jest --watch SomeTestFile.test the runner keeps restarting. The only way I can get it to work is via changing the name of the fixture folder to .fixtures.

I also tried to use an absolute path, with 1-1 match of the file causing the problem, also does not help...

My intended setup is this:

module.exports = {
  projects: [
    'workspaces/hush-hush',
    'workspaces/queuing-service',
    'workspaces/telepath',
    'workspaces/idbox-react-ui',
    'workspaces/utils',
    {
      rootDir: 'workspaces/idservice',
      testEnvironment: 'node'
    },
    {
      rootDir: 'workspaces/nameservice',
      testEnvironment: 'node'
    },
    {
      testMatch: ['<rootDir>/dummy']
    }
  ],
  collectCoverage: true,
  collectCoverageFrom: [
    'source/**/*.js',
    'src/**/*.js',
    'pages/*.js',
    'components/**',
    '!pages/_app.js',
    '!**/jest.config.js',
    '!**/_document.js',
    '!**/*.test.js',
    '!**/__mocks__/**.js',
    '!**/node_modules/**',
    '!**/.next/**'
  ],
  coverageReporters: [
    'text-summary',
    'lcov'
  ],
  watchPathIgnorePatterns: [
    '<rootDir>/fixtures/idservice/'
  ]
}

My tests dynamically create a fixture file in <rootDir>/fixtures/idservice/ - in this case in the root of the monorepo, not in the root of the workspace. Since this does not seem to work, I thought maybe I need to use the root of the workspace itself, so I put watchPathIgnorePatterns inside projects entry like this:

module.exports = {
  projects: [
    // ....
    {
      rootDir: 'workspaces/idservice',
      testEnvironment: 'node',
      watchPathIgnorePatterns: [
        '<rootDir>/fixtures/idservice/'
      ]
    },

and then have the fixtures folder in the workspace root, but this does not seem to work either. I tried different values, absolute paths, nothing seems to work. What am I doing wrong?

The link to repo is here: https://github.com/identity-box/identity-box

I am using jest version 25.1.0.

@jason-henriksen
Copy link

Using jest 26.6.3 and none of this works for me. I'm trying both pacakge and jest.config.

npm command is:
"test:simple": "jest --watch --verbose=false --config ./jest.config.js",

In package:
"coveragePathIgnorePatterns": [ "node_modules", "output" ], "watchPathIgnorePatterns": [ "node_modules", "output" ], "jest": { "coveragePathIgnorePatterns": [ "node_modules", "output" ], "watchPathIgnorePatterns": [ "node_modules", "output" ] }

In jest.config.js
watchPathIgnorePatterns: [ "<rootDir>/output/" ], coveragePathIgnorePatterns: [ "<rootDir>/output/" ],

But when my test executes this code:
const stringReport = JSON.stringify(rep,null,2) fs.writeFileSync('./output/transform.map.json', stringReport)

Then the test runs in an infinite loop. I comment out that writeFileSync line and everything behaves as expected.

Any help would be much appreciated.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants