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
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', () => { ... });
});
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
-
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. Jun 1, 2019 at 18:38 -
-
1For me worked just the file name and not a path: npm test App.test.js Nov 17, 2021 at 11:26
-
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.