Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jest.config.js with create-react-app

I would like to move my jest config out of my package.json, i am trying to use the --config as suggested here but get the error argv.config.match is not a function

package.json

  "scripts": {     "start": "react-scripts start",     "build": "react-scripts build",     "test": "react-scripts test --config jest.config.js",     "eject": "react-scripts eject",   }, 

cli

hutber@hutber-mac:/var/www/management/node$ npm test -u  > [email protected] test /var/www/management/node > react-scripts test --config jest.config.js  Usage: test.js [--config=<pathToConfigFile>] [TestPathPattern]   argv.config.match is not a function npm ERR! Test failed.  See above for more details. 
like image 701
Jamie Hutber Avatar asked Jan 23 '20 12:01

Jamie Hutber


People also ask

Does create React app use Jest?

Create React App uses Jest as its test runner. To prepare for this integration, we did a major revamp of Jest so if you heard bad things about it years ago, give it another try. Jest is a Node-based runner. This means that the tests always run in a Node environment and not in a real browser.

Can I use npm With create React app?

Create React App is a comfortable environment for learning React, and is the best way to start building a new single-page application in React. npx on the first line is not a typo — it's a package runner tool that comes with npm 5.2+.


2 Answers

For me appending -- --config=jest.config.js worked.

So the whole string react-scripts test -- --config jest.config.js in your case.

like image 77
McTrafik Avatar answered Sep 22 '22 10:09

McTrafik


TL;DR

Add -- before your options.

"test": "react-scripts test -- --config=jest.config.js", 

The problem here is with react-scripts not seeing the options being passed to it. We can demonstrate this by running it directly.

./node_modules/.bin/react-scripts test --config=jest.config.js # argv.config.match is not a function  ./node_modules/.bin/react-scripts test -- --config=jest.config.js # This works. 

Variations

How you pass options to scripts varies depending on which versions of npm or Yarn you use. For completeness, here are the results for the variations:

# This runs, but completely ignores the option. npm test --config=jest.config.js  # These result in "argv.config.match is not a function," indicating that the # options were not understood. npm test -- --config=jest.config.js yarn test -- --config=jest.config.js yarn test --config=jest.config.js 
  • https://jestjs.io/docs/en/cli#using-with-yarn
  • https://jestjs.io/docs/en/cli#using-with-npm-scripts

create react app sets up the test script in package.json with

"test": "react-scripts test", 

You can set additional options like so.

"test": "react-scripts test -- --config=jest.config.js", 

Something like this might work if you want to send options through the CLI.

"test": "react-scripts test --", 
yarn test --bail # comes through as react-scripts test -- --bail 

Resources

Here are a few resources to explain the different usage.

  • https://medium.com/fhinkel/the-curious-case-of-double-dashes-b5e7711698f
like image 45
reergymerej Avatar answered Sep 21 '22 10:09

reergymerej