55

I am trying to pass along a configuration file to Jest in order to run tests only from a given directory.

According to documentation, you can use config.testPathDirs: https://facebook.github.io/jest/docs/api.html#config-testpathdirs-array-string and you can call jest --config=<config-file>.

Unfortunately the documentation doesn't include any description of how the configuration file should look like.

I tried both these options in a jest-config.js file:

testPathDirs = ['coredata/src'];

and

config.testPathDirs(['coredata/src']);

and ran:

$ jest --config=jest-config.js

...but I get this type of error:

$ jest --config=jest-config.js
Using Jest CLI v0.4.0
Failed with unexpected error.

/Users/carles/dev/azazo/coredata/node_modules/jest-cli/src/jest.js:179
      throw error;
            ^
SyntaxError: Unexpected token c
    at Object.parse (native)
    at /Users/carles/dev/azazo/coredata/node_modules/jest-cli/src/lib/utils.js:291:23
    at _fulfilled (/Users/carles/dev/azazo/coredata/node_modules/jest-cli/node_modules/q/q.js:798:54)
    at self.promiseDispatch.done (/Users/carles/dev/azazo/coredata/node_modules/jest-cli/node_modules/q/q.js:827:30)
    at Promise.promise.promiseDispatch (/Users/carles/dev/azazo/coredata/node_modules/jest-cli/node_modules/q/q.js:760:13)
    at /Users/carles/dev/azazo/coredata/node_modules/jest-cli/node_modules/q/q.js:574:44
    at flush (/Users/carles/dev/azazo/coredata/node_modules/jest-cli/node_modules/q/q.js:108:17)
    at process._tickCallback (node.js:419:13)
Cihan Keser
  • 3,190
  • 4
  • 30
  • 43
Carles Barrobés
  • 11,608
  • 5
  • 46
  • 60

6 Answers6

44

I figured out that the config file is JSON.

{
  "testPathDirs": ["coredata/src"]
}

Unfortunately I found nowhere in the documentation a hint about this.

Carles Barrobés
  • 11,608
  • 5
  • 46
  • 60
16

Seems to me you can configure jest directly within your package.json, see https://github.com/shidhincr/react-jest-gulp-jspm-seed/blob/master/package.json.

{
  "name": "react-jest-gulp-seed",
  "version": "1.0.0",
  "description": "Seed for React Jest Gulp Project",
  "main": "index.js",
  "scripts": {
    "test": "jest",
    "postinstall": "jspm install"
  },
  "jest": {
    "scriptPreprocessor": "./preprocessor.js",
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/jspm_packages"
    ],
    "unmockedModulePathPatterns": [
      "./node_modules/react"
    ]
  },
  "author": "Shidhin CR <shidhincr@gmail.com> (http://www.undefinednull.com/)",
  "license": "ISC",
  "dependencies": {
    "del": "^1.1.1",
    "gulp": "^3.8.11",
    "gulp-filter": "^2.0.2",
    "gulp-load-plugins": "^0.10.0",
    "gulp-react": "^3.0.1",
    "gulp-shell": "^0.4.1",
    "harmonize": "^1.4.1",
    "jest-cli": "^0.4.1",
    "jspm": "^0.15.5",
    "react": "^0.13.2",
    "react-tools": "^0.13.2",
    "run-sequence": "^1.1.0"
  },
  "devDependencies": {
    "browser-sync": "^2.7.1",
    "gulp": "^3.8.11"
  },
  "jspm": {
    "directories": {},
    "dependencies": {
      "react": "npm:react@^0.13.2"
    },
    "devDependencies": {
      "babel": "npm:babel-core@^5.1.13",
      "babel-runtime": "npm:babel-runtime@^5.1.13",
      "core-js": "npm:core-js@^0.9.4"
    }
  }
}
Volker Rose
  • 1,808
  • 15
  • 16
  • 2
    What do i put in the package.json file under `jest` if I want to use a separate config file for configuring jest? – Panshul Dec 20 '16 at 14:39
  • 2
    @Panshul, When you call `jest` in the test script, you pass in the path to the config file like `jest --config ./config/jest-config.js` – stone Feb 07 '17 at 10:00
  • 3
    The question is about configuration file not `package.json`. Answer is not valid – Firanolfind Jul 30 '18 at 08:02
11

As of the current date of October 5, 2018, I was able to setup a jest configuration file with ease, without it requiring it to be in JSON format.

My package.json scripts section:

"scripts": {
    "start": "node server.js",
    "dev": "webpack-dev-server --hot --inline",
    "test": "jest --config ./jest-config.js",
    "build": "webpack",
    "postinstall": "webpack"
  }

I decided to keep the jest-config file in the root path alongside package.json, so that's where it is pointing to, but you can put it anywhere you want. Here is the configuration file, albeit a little short:

module.exports = {
  verbose: true,
  setupTestFrameworkScriptFile: "./enzyme.js",
  roots: [
    "../__tests__"
  ],
  modulePaths: [
    "./__stubs__"
  ],
  moduleNameMapper: {
    ".scss$": "scss-stub.js"
  }
}

The Jest documentation in the "Configuring Jest" section (link here) says that you can create it with a module.exports object. They include a caveat that says, "Please keep in mind that the resulting configuration must be JSON-serializable", which adds to the confusion. "Delightful Javascript Testing!" Hah!

P.S. The roots property in the config tells Jest where to look for all of your tests. Thought it might help.

Dan Zuzevich
  • 3,651
  • 3
  • 26
  • 39
8

The docs were updated to explain the configuration. Here is the link for anyone looking for more information:

https://facebook.github.io/jest/docs/en/configuration.html#content

The gist:

If using the --config <path/to/json> to direct jest to your configuration json file, you only put the JSON information in the file without the "jest" keyword.

// config.json at <path/to/json>
{
   "verbose": true
}

If you want to use the package.json to configure jest, add the "jest" key and then your configuration.

// package.json 
{ 
   "name": "packageName",
   "version": "1.0.0",
   "jest": {
     "verbose": true
   }
}
Valerie
  • 91
  • 1
  • 4
7

For JEST V20+:

{
 "roots": ["__tests__/"]
}

For v 20-, you will get following warning if you use testPathDirs:

Deprecation Warning:

Option "testPathDirs" was replaced by "roots".

Jest now treats your current configuration as: { "roots": ["tests/unit/"] }

Please update your configuration.

Peter
  • 10,492
  • 21
  • 82
  • 132
4
// package.json

{
 ...
 scripts: {
   "test": "jest --config ./jest.config.js"
 }
 ...
}
TheEhsanSarshar
  • 2,677
  • 22
  • 41
  • Ref. https://jestjs.io/docs/configuration – ikhvjs Apr 21 '22 at 07:02
  • @ikhvjs where is it? I don't see where it is – Ooker Jul 15 '23 at 05:58
  • 1
    @Ooker first sentence. It is recommended to define the configuration in a dedicated JavaScript, TypeScript or JSON file. The file will be discovered automatically, if it is named jest.config.js|ts|mjs|cjs|json. You can use --config flag to pass an explicit path to the file. – ikhvjs Jul 15 '23 at 13:43