Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a specific test with Create React App

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?

like image 511
El Anonimo Avatar asked Jun 01 '19 10:06

El Anonimo


People also ask

How do I run a specific test in Jest react?

In order to run a specific test, you'll need to use the jest command. npm test will not work. To access jest directly on the command line, install it via npm i -g jest-cli or yarn global add jest-cli . Then simply run your specific test with jest bar.


2 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', () => { ... });
});
like image 169
user210757 Avatar answered Sep 22 '22 03:09

user210757


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

For example:

npm test src/components/App.test.js
like image 45
araraonline Avatar answered Sep 20 '22 03:09

araraonline