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', () => { ... });
});
Answer from user210757 on Stack OverflowI 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
Since at least 2019:
npm test -- bar.spec.js
In 2015:
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.spec.js.
Note: You don't have to enter the full path to your test file. The argument is interpreted as a regular expression. Any part of the full path that uniquely identifies a file suffices.
All you have to do is chant the magic incantation:
npm test -- SomeTestFileToRun
The stand-alone -- is *nix magic for marking the end of options, meaning (for NPM) that everything after that is passed to the command being run, in this case jest. As an aside, you can display Jest usage notes by saying
npm test -- --help
Anyhow, chanting
npm test -- Foo
runs the tests in the named file (FooBar.js). You should note, though, that:
Jest treats the name as case-sensitive, so if you're using a case-insensitive, but case-preserving file system (like Windows NTFS), you might encounter what appears to be oddness going on.
Jest appears to treat the specification as a prefix.
So the above incantation will
- Run
FooBar.js,Foo.jsandFooZilla.js - But not run
foo.js
» npm install react-scripts-test
You arrived at the answer yourself. To use jest your tests need to go through babel for the runner to understand react syntax. take a look at the babel-doc to understand it at greater detail. it's just a transformation tool that transforms fancy syntax into something javascript understands. install the following plugins and presets.
Presets
npm i --save @babel/preset-env
npm i --save @babel/preset-react
Plugins
npm install --save babel-plugin-transform-export-extensions
in your .babelrc add the following lines:
{
"env": {
"test": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"transform-export-extensions",
],
"only": [
"./**/*.js",
"node_modules/jest-runtime"
]
}
}
}
Now try running jest on the command-line from your project directory to make sure your tests are configured correctly.
react-scripts is a preconfigured set of commands that come out of the box with create-react-app if you want to use that instead of jest command, check here.
react-scripts expects your tests folder location to follow a certain convention.
this is probably why the tests weren't getting fetched when the react-scripts test command was run out of the box.
in package.json change
"scripts": {
"test": "jest",
},
to the following:
"scripts": {
"test": "react-scripts test",
},
i.e. don't change to jest in the first place