简体   繁体   中英

How can I tell Jest through the command line not to run a certain test file, but run all others?

I have three test files, acceptance.test.js routes.test.js snapshots.test.js

I want to create an npm script in my package.json that would ignore one of these test files when it's used. I'm trying to use the Jest option --testPathPattern to do this. For example:

  "scripts": {
    "no-snapshots-test": "jest --coverage --testPathPattern=^(?!snapshots).*$"
  }

This particular example gives an error of .*$ was unexpected at this time in the terminal, though. I've tried removing .*$ off the end, but that causes my snapshot tests to still run.

What's the correct regex to allow me to specify the file (or files) that I want to ignore? I need to do this in the CLI, so I can't use the Jest config option testPathIgnorePattern.

The beginning ^ and the end $ are not supported. Omit them and use the very same Regex without the negative lookahead:

"scripts": {
    "no-snapshots-test": "jest --coverage --testPathPattern=snapshots.*"
}

Just a guess: you might need to use the proper JavaScript Regex syntax: /snapshots.*/g .

Instead of trying to use a negative lookahead in the testPathPattern you can use the the testPathIgnorePattern instead. It is not documented in the jest CLI documentation but it still works.

"scripts": {
    "no-snapshots-test": "jest --coverage --testPathIgnorePattern=snapshots"
  }

You might need to change the ignore pattern though if it matches too many of your tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM