Coder Social home page Coder Social logo

Comments (27)

laurenskling avatar laurenskling commented on May 7, 2024 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.

holic avatar holic commented on May 7, 2024 27

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.

cpojer avatar cpojer commented on May 7, 2024 12

We have testRegex now. testDirectoryName was removed.

from jest.

rbalicki2 avatar rbalicki2 commented on May 7, 2024 8

Please please please enable tests in the same folder as the source file!

from jest.

Jose-Bustamante avatar Jose-Bustamante commented on May 7, 2024 6

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.

aprilmintacpineda avatar aprilmintacpineda commented on May 7, 2024 5

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.

burzum avatar burzum commented on May 7, 2024 5

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.

laurenskling avatar laurenskling commented on May 7, 2024 4

Thanks, I'm sure I'm not the only one who would want a folder setup like this.

from jest.

jeffmo avatar jeffmo commented on May 7, 2024 1

Added config.testDirectoryName

fcc7b26

from jest.

thymikee avatar thymikee commented on May 7, 2024 1

@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.

jeffmo avatar jeffmo commented on May 7, 2024

Yea this is a good point. Let's make a config option for this (update coming)

from jest.

pspeter3 avatar pspeter3 commented on May 7, 2024

Thanks!

from jest.

cpojer avatar cpojer commented on May 7, 2024

@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.

jpgarcia avatar jpgarcia commented on May 7, 2024

+1

from jest.

erquhart avatar erquhart commented on May 7, 2024

Yep, specs in with the source is a very common setup, +1.

from jest.

isaacws avatar isaacws commented on May 7, 2024

+1 I was hoping to be able to put the tests with the components

from jest.

conradho avatar conradho commented on May 7, 2024

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.

jackson-sandland avatar jackson-sandland commented on May 7, 2024

Where do you set config.testDirectoryName?

from jest.

jackson-sandland avatar jackson-sandland commented on May 7, 2024

Thank you. I added testRegex in package.json like so:
"main": "index.js",
"jest": {
"testRegex": "\_spec\.js$"
}

Works!

from jest.

jackson-sandland avatar jackson-sandland commented on May 7, 2024

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.

marcusjwhelan avatar marcusjwhelan commented on May 7, 2024

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

I have been looking for about 2 hours now.

Sorry never mind I found it online. "testRegex": "(/^spec/.*|\\.(test|spec))\\.(ts)$"

from jest.

cpojer avatar cpojer commented on May 7, 2024

You can use testMatch if you are more familiar with globs.

from jest.

marcusjwhelan avatar marcusjwhelan commented on May 7, 2024

@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.

thymikee avatar thymikee commented on May 7, 2024
{
  "testMatch": ["**/spec/**/*.spec.ts"]
}

from jest.

marcusjwhelan avatar marcusjwhelan commented on May 7, 2024

@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.

abgalphabetz avatar abgalphabetz commented on May 7, 2024

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.

github-actions avatar github-actions commented on May 7, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.