26

In an app create with CRA v.1 I need to run a specific test file. How do I go about it? There is the --testPathIgnorePatterns flag to add to the npm test script to ignore a file or path but how do I run a particular test file with CRA from the command line?

3 Answers 3

48

I have done this by using -- to pass custom arguments to the npm script.

Documentation on this option can be found here: https://docs.npmjs.com/cli/run-script

So, to run a single test in a create-react-app application, I run the following:

npm run test -- -t 'test-name'

Where test-name is the value used in the describe function in jest -

describe('test-name', () => {
  it('does something', () => { ... });
});
42

You can use the name of the file in the command, and it will run only it.

For example:

npm test src/App.test.js
5
  • Note: I came to this answer by chance as I was having the same issue you are. I'm not sure how reliable it is :) Jun 1, 2019 at 17:52
  • Thank you. I tried the solution you offered. Here's what I got. I have two test files within client/src: sample.test.js and sample2.test.js. When I run npm test sample2.test.js from client/src Jest actually runs sample.test.js and vice versa.
    – El Anonimo
    Jun 1, 2019 at 18:38
  • Maybe this or this has something that works. Jun 1, 2019 at 19:22
  • 1
    For me worked just the file name and not a path: npm test App.test.js Nov 17, 2021 at 11:26
  • as of 2022, this works best!
    – Benjam
    Aug 12, 2022 at 15:11
0

By running the command as the accepted answer mentioned, it will take it as regex and if you have multiple same files name like index.test.tsx, it will execute all.

If you are using jest, you can run command below to force it to run only single test file:

npm test --runTestsByPath "src/pages/parentpath/index.test.tsx"

Refer to this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.