Comments (27)
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?
from jest.
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/
from jest.
We have testRegex
now. testDirectoryName
was removed.
from jest.
Please please please enable tests in the same folder as the source file!
from jest.
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.
from jest.
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
.
from jest.
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.
from jest.
Thanks, I'm sure I'm not the only one who would want a folder setup like this.
from jest.
Added config.testDirectoryName
from jest.
@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)
from jest.
Yea this is a good point. Let's make a config option for this (update coming)
from jest.
Thanks!
from jest.
@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.
from jest.
+1
from jest.
Yep, specs in with the source is a very common setup, +1.
from jest.
+1 I was hoping to be able to put the tests with the components
from jest.
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
from jest.
Where do you set config.testDirectoryName?
from jest.
Thank you. I added testRegex in package.json like so:
"main": "index.js",
"jest": {
"testRegex": "\_spec\.js$"
}
Works!
from jest.
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"
}
}
from jest.
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
- utils
- testThisFile.spec.ts
- utils
I have been looking for about 2 hours now.
Sorry never mind I found it online. "testRegex": "(/^spec/.*|\\.(test|spec))\\.(ts)$"
from jest.
You can use testMatch if you are more familiar with globs.
from jest.
@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.
from jest.
{
"testMatch": ["**/spec/**/*.spec.ts"]
}
from jest.
@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.
from jest.
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 "/src/main/" but no luck.
from jest.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.
from jest.
Related Issues (20)
- [Feature]: outputFile support for listTests option HOT 1
- [Bug]: `structuredClone` under jest fails `instanceof` check HOT 3
- [Bug]: `URLSearchParams#get(k, v)` is not implemented and uses `URLSearchParams#get(k)` instead HOT 1
- [Bug]: Unable to get Lit + Jest + Typescript + SWC Jest to work properly HOT 1
- [Bug]: When WorkerThreads are enabled the Jest fails to collect test coverage from untested files HOT 2
- [Bug]: `[email protected]` is pulling older jsdom v20 HOT 2
- [Bug]: Getting the compile time error
- [Bug]: Can't configure Jest to find tests under a `node_modules` directory HOT 2
- [Bug]: Errors thrown in setTimeout callback are not caught by test.failing HOT 1
- [Feature]: Print all `Error` properties instead of only `.stack` and `.cause` HOT 1
- [Bug]: required inputs in Angular afterNextRender hook throws an error in jest
- EMFILE: too many open files jest tests on github linux pipeline HOT 1
- [Bug]: EMFILE: too many open files jest tests on github linux pipeline HOT 3
- [Feature]: Auto mock class / object
- [Feature]: Dynamic hook (afterThis) to enable clean-up during test HOT 2
- [Bug]: 'node' entry in package.json exports is not respected HOT 4
- [Feature]: jest could detect some errors it is currently silencing when using `jsdom` environment
- [Bug]: coverageThreshold for single path is not working
- [Docs]: Egghead.io link on snapshot testing docs page is broken
- [Bug]: Jest duplicates modules imported in both global setup/teardown and code under test
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jest.