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?
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 .
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.
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.
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.
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)
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)" ] },
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With