I feel like a lot of node projects use lib
and test
as the directory names. It would be nice to be able to keep that convention.
Yea this is a good point. Let's make a config option for this (update coming)
Added config.testDirectoryName
https://github.com/facebook/jest/commit/fcc7b26b7a3bbac959e96438a97fffef59a8b35b
Thanks!
I'm trying to not use a folder for my test, leaving config.testDirectoryName
empty or "."
. Can't seem to get that working. I have all my testable js files in their own folder, so i would like my structure to be:
-- componentfolder
---- component.js
---- component.test.js
Isn't this possible?
@laurenskling unfortunately that isn't possible right now. I might add this feature in the future however, when I rewrite how config/setup of jest works.
Thanks, I'm sure I'm not the only one who would want a folder setup like this.
+1
Yep, specs in with the source is a very common setup, +1.
+1 I was hoping to be able to put the tests with the components
I set the testDirectoryName to my root/components dir, which then allowed me to find all the tests (even if it was nested multiple levels deep) and have the tests next to the component
Please please please enable tests in the same folder as the source file!
Hi guys, I am starting with jest but what I did to fix this problem is the next configuration:
My folder structure:
-- componentfolder
---- component.js
---- component.test.js
Jest configuration:
"testPathDirs": [
"./"
],
"testFileExtensions": [
"test.js"
],
"testDirectoryName": "src"
I hope this help somebody.
For those looking to colocate their tests with their components (e.g. .test.js
), I'm using the following config to find the right tests:
"jest": {
"automock": false,
"testRegex": "\\.test\\.js$"
}
Edit: These are now default in the latest Jest! o/
Where do you set config.testDirectoryName?
We have testRegex
now. testDirectoryName
was removed.
Thank you. I added testRegex in package.json like so:
"main": "index.js",
"jest": {
"testRegex": "\_spec\.js$"
}
Works!
I'm getting this error when running the tests:
Cannot find module 'react/lib/ReactComponentTreeHook' from 'ReactDebugTool.js'
I read it may have to do with renaming the directory, though renaming it '__tests__' did not help.
Here's my package.json:
"name": "App",
"version": "0.0.1",
"description": "App",
"main": "index.js",
"jest": {
"testRegex": "\\_spec\\.js$"
},
"scripts": {
"start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js --hot --inline",
"logger": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js --hot --inline --config webpack-redux-logger.config.js",
"watch": "webpack --watch",
"test": "jest"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-polyfill": "^6.13.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.1.18",
"babel-preset-stage-2": "^6.18.0",
"chai": "^3.5.0",
"chai-jquery": "^2.0.0",
"ckeditor": "^4.5.10",
"css-loader": "^0.24.0",
"enzyme": "^2.6.0",
"jest": "^17.0.3",
"jquery": "^2.2.1",
"jsdom": "^8.1.0",
"mocha": "^2.4.5",
"node-sass": "^3.8.0",
"normalizr": "^2.2.1",
"object-path-immutable": "^0.5.0",
"react-addons-test-utils": "^0.14.7",
"react-autosuggest": "^6.0.4",
"react-css-modules": "^3.7.10",
"react-hot-loader": "^1.3.0",
"react-select2": "4.0.2",
"sass-loader": "^4.0.1",
"striptags": "^2.1.1",
"style-loader": "^0.13.1",
"webpack": "^1.13.2",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"axios": "0.13.1",
"babel-jest": "^17.0.2",
"babel-plugin-transform-es2015-template-literals": "^6.8.0",
"babel-plugin-transform-object-rest-spread": "^6.16.0",
"babel-preset-stage-1": "^6.1.18",
"chart.js": "^2.3.0",
"classnames": "^2.2.5",
"deep-freeze": "^0.0.1",
"immutable": "^3.8.1",
"lodash": "^4.16.3",
"material-ui": "^0.13.0",
"moment": "^2.14.1",
"react": "^0.14.3",
"react-chartjs-2": "^1.5.0",
"react-day-picker": "^2.4.1",
"react-dom": "^0.14.3",
"react-hammerjs": "^0.5.0",
"react-input-range": "^0.9.2",
"react-modal": "^1.5.2",
"react-redux": "^4.0.0",
"react-router": "^2.0.1",
"react-tap-event-plugin": "^0.2.2",
"react-test-renderer": "^15.4.1",
"react-tooltip": "^3.2.2",
"react-waypoint": "^3.1.3",
"redux": "^3.0.4",
"redux-form": "^5.3.2",
"redux-logger": "^2.7.4",
"redux-promise": "^0.5.3",
"redux-thunk": "^2.1.0"
}
}
@jackson-sandland not sure if that's the issue, but generally you should keep react
and react-test-renderer
on the same major version (so either 14 or 15, not both)
Soooo. I know next to nothing when it comes to regex. How would you write a testRegex to test nested sub directories of tests?
spec
I have been looking for about 2 hours now.
Sorry never mind I found it online. "testRegex": "(/^spec/.*|\\.(test|spec))\\.(ts)$"
You can use testMatch if you are more familiar with globs.
@cpojer Could you give an example? I had to look up how to do regex queries on file system directories and I just guessed at it. Jest doesn't have examples of this kind of stuff. As far as I can tell, and I can't find any examples either.
{
"testMatch": ["**/spec/**/*.spec.ts"]
}
@thymikee you cannot seem to use a glob if you want to do individual tests in Webstorm. If I use testRegex it lets me do tests on individual tests.
as of [email protected]
, you can put your test files anywhere. Mine are on resources/assets/js/__tests__
, then I have even more directories in there such as /redux/reducers
and /sagas/
. You can also change the name of the directory and it will still find all your test files.
What jest does is it iterates through all the files and folders that you have, and matches the file name to a specific regex.
I use the following settings on my package.json
"jest": {
"testPathIgnorePatterns": [
"/node_modules/",
"/vendor"
],
"testRegex": "\\.spec\\.js",
"setupFiles": [
"raf/polyfill"
]
},
as long as you name your file file.spec.js
it will parse that, you can change that as well.
Then when running the test just do jest --json --outputFile=jest-output.json
.
I struggle to get this to work, we have a pattern like this ./test/unit/<subfolders>/<file>.spec.js
and I can't get it to work. Not with testRegex and not with testMatch... :(
A simple path option would have been awesome.
It's not obvious to me too where the test files and prod files should be put.
I would like to separate those in two different root folders (i m from java background) like:
|-src
--|-main/path/to/prod.js
--|-test/path/to/prod.test.js
In prod.test.js, i do ,
import func from 'path/to/prod.js'
expect(func()).toBe(<expected>)
I config jest options in package.json , e.g. moduleDirectories or modulePaths to include "src/main/" or "
Most helpful comment
For those looking to colocate their tests with their components (e.g.
.test.js
), I'm using the following config to find the right tests:Edit: These are now default in the latest Jest! o/