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)
- [Bug]: mocking a function which is dereferenced using require syntax doesnt get mocked
- [Bug]: getState from jest-circus gets state from parellel environment HOT 1
- [Docs]: repetitive sentences
- TypeError: Cannot read properties of undefined (reading '__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED') who can help me ? HOT 1
- [Feature]: Add a "toHaveBeenCalledExactlyOnceWith" assertion (name TBD) HOT 1
- [Bug]: isolateModules doesn't work properly with auto mocked modules (reopen #14443)
- jest doesn't work with virtual driver created with subst on Windows HOT 3
- [Bug]: jest isolation conflicts with better-sqlite3 native modules
- [Bug]: Native fetch Arrays Incorrectly Matched
- TypeError: Cannot read properties of undefined (reading 'data') HOT 1
- [Feature]: Update deprecated subdependencies: [email protected], [email protected] in @jest/types HOT 1
- TypeError: Cannot read properties of undefined (reading 'data')
- Using setupFiles inside reporters HOT 2
- [Bug]: toHaveBeenCalledWith matcher matches with any URL object
- [Bug]: OpenTelemetry Span Not Being Captured in Jest Tests with Instrumentation (But it works with other test runners) HOT 6
- [Feature]: support type inference for function parameters
- [Feature]: Restore Access to Utility Functions in "jest-snapshot/utils" for Continued Tooling Support HOT 3
- [Bug]: globalSetup doesn't process moduleNameMappers HOT 1
- [Bug]: Failed `toEqual` assertion has some sort of side-effect that causes toEqual using the same value in subsequent tests to fail incorrectly (Jest 29+ only) HOT 5
- [Bug]: fails to mock resolved value HOT 1
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.