如何编写Jest配置文件

Car*_*bés 31 javascript jestjs

我试图将配置文件传递给Jest,以便仅从给定目录运行测试.

根据文档,您可以使用config.testPathDirs:https://facebook.github.io/jest/docs/api.html#config-testpathdirs-array-string,您可以调用jest --config=<config-file>.

遗憾的是,文档中没有包含配置文件外观的任何描述.

我在一个jest-config.js文件中尝试了这两个选项:

testPathDirs = ['coredata/src'];
Run Code Online (Sandbox Code Playgroud)

config.testPathDirs(['coredata/src']);
Run Code Online (Sandbox Code Playgroud)

跑了:

$ jest --config=jest-config.js
Run Code Online (Sandbox Code Playgroud)

...但我得到这种类型的错误:

$ 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)
Run Code Online (Sandbox Code Playgroud)

Car*_*bés 36

我发现配置文件是JSON.

{
  "testPathDirs": ["coredata/src"]
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我在文档中找不到任何关于此的提示.

  • 现在"testsPathDirs"被改为"root",如下所述:[jest roots config](https://facebook.github.io/jest/docs/configuration.html#roots-array-string) (7认同)
  • 在你的package.json中:"jest":{"testPathDirs":["app /"]}, (4认同)
  • 已被`roots`和`rootDir`弃用 (2认同)

Vol*_*ose 13

在我看来,您可以直接在您的内部配置jest package.json,请参阅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"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果我想使用单独的配置文件来配置jest,我在`jest`下的package.json文件中放了什么? (2认同)
  • @Panshul,当您在测试脚本中调用`jest`时,您会将配置文件的路径传递给`jest --config。/ config / jest-config.js`。 (2认同)
  • 问题是关于配置文件而不是“ package.json”。答案无效 (2认同)

Dan*_*ich 10

截至 2018 年 10 月 5 日的当前日期,我能够轻松设置一个 jest 配置文件,而无需它采用 JSON 格式。

我的 package.json 脚本部分:

"scripts": {
    "start": "node server.js",
    "dev": "webpack-dev-server --hot --inline",
    "test": "jest --config ./jest-config.js",
    "build": "webpack",
    "postinstall": "webpack"
  }
Run Code Online (Sandbox Code Playgroud)

我决定将 jest-config 文件与 package.json 一起保存在根路径中,这样它就指向了它所在的位置,但您可以将它放在任何您想要的位置。这是配置文件,虽然有点短:

module.exports = {
  verbose: true,
  setupTestFrameworkScriptFile: "./enzyme.js",
  roots: [
    "../__tests__"
  ],
  modulePaths: [
    "./__stubs__"
  ],
  moduleNameMapper: {
    ".scss$": "scss-stub.js"
  }
}
Run Code Online (Sandbox Code Playgroud)

“配置 Jest”部分中的 Jest 文档(此处链接)说您可以使用 module.exports 对象创建它。它们包括一个警告,上面写着“请记住,生成的配置必须是 JSON 可序列化的”,这增加了混淆。“令人愉快的 Javascript 测试!” 哈!

PS配置中的 roots 属性告诉 Jest 在哪里查找所有测试。认为它可能有帮助。


Vis*_*ati 6

对于 JEST V20+:

{
 "roots": ["__tests__/"]
}
Run Code Online (Sandbox Code Playgroud)

对于 v 20-,如果您使用 testPathDirs,您将收到以下警告:

弃用警告:

选项“testPathDirs”被“roots”取代。

Jest 现在将您当前的配置视为: { "roots": [" tests /unit/"] }

请更新您的配置。


Val*_*rie 5

更新了文档以解释配置.以下是寻找更多信息的人的链接:

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

要旨:

如果使用--config <path/to/json>直接jest到配置json文件,则只将JSON信息放在没有"jest"关键字的文件中.

// config.json at <path/to/json>
{
   "verbose": true
}
Run Code Online (Sandbox Code Playgroud)

如果要使用package.json配置jest,请添加"jest"键,然后添加配置.

// package.json 
{ 
   "name": "packageName",
   "version": "1.0.0",
   "jest": {
     "verbose": true
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 当前链接(截至2017年10月)是https://facebook.github.io/jest/docs/en/configuration.html#content (2认同)