Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I move jest tests to a /test folder and get them to run?

Tags:

edit: I'm pretty sure this question is out of date and Jest has changed since then. Don't assume the answers will work or even be relevant now.

Jest expects tests to be in _tests_ folder or to be named blah.test.js. I don't like either of those patterns. I want my test files to be in /test and naming them test/index.ios.test.js seems redundant and silly. My first stab at this is to change my jest config in package.json to be:

"jest": {     "testPathDirs": ["test"],     "testRegex": "\\.(js|jsx)$",     "preset": "react-native" } 

Which find the tests successfully but they all fail:

Cannot find module 'setupDevtools' from 'setup.js'      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:151:17)   at Object.<anonymous> (node_modules/react-native/jest/setup.js:23:1) 

That function is in node_modules/react-native/Libraries/Core/Devtools/setupDevtools.js. How do I tell Jest where to find it?

like image 953
jcollum Avatar asked Dec 04 '16 21:12

jcollum


People also ask

How do I run a Jest file in a folder?

To run Jest tests only for current folder, we can run jest with the path of the folder with the tests we want to run. to run the tests in the dev/app folder given that the test script runs jest . We can also set the testPathPattern option to the folder with the tests we want to run. to run the tests in dev/app .

How do I run a test case in a folder?

If you want to run the tests from a specific folder user the --testPathPattern jest flag. When setting up the npm script add the path to the folder as well. In your package. json add the flag in you npm scripts.

How do I run a test file in Jest?

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.

Where should Jest tests be stored?

src/file. test. js mentioned first in the Getting Started docs, and is great for keeping tests (especially unit) easy to find next to source files.


Video Answer


2 Answers

I solved the problem by just setting the regex so it finds all .js files in my ./test folder:

"jest": {    "preset": "react-native",     "globals": {         "__DEV__": true     },     "testRegex": "./test/.*.js$",     "rootDir": "."  }, 

(jcollum here: I don't know what __DEV__ is actually doing there but an error will be thrown if isn't. jest 17.0.3)

like image 52
Andreas Köberle Avatar answered Oct 18 '22 08:10

Andreas Köberle


I think the best way to go about this is to configure testMatch. It defaults to:

[ "**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ] 

So you could either change __tests__ to tests or add a new entry into the array that checks for both. My use the following in my package.json:

  "jest": {     "testMatch": [ "**/tests/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ]   }, 
like image 20
Jiert Avatar answered Oct 18 '22 07:10

Jiert