Javascript 如何编写 Jest 配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30027494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:31:15  来源:igfitidea点击:

How to write a Jest configuration file

javascriptjestjs

提问by Carles Barrobés

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

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

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

根据文档,您可以使用config.testPathDirshttps: //facebook.github.io/jest/docs/api.html#config-testpathdirs-array-string并且您可以调用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.jsfile:

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

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)

回答by Carles Barrobés

I figured out that the config file is JSON.

我发现配置文件是 JSON。

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

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

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

回答by Volker Rose

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.

在我看来,您可以直接在您的 .js 中配置 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 <[email protected]> (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"
    }
  }
}

回答by Dan Zuzevich

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.

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

My package.json scripts section:

我的 package.json 脚本部分:

"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:

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

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!

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

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

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

回答by Vishal Gulati

For JEST V20+:

对于 JEST V20+:

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

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

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

Deprecation Warning:

弃用警告:

Option "testPathDirs" was replaced by "roots".

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

Please update your configuration.

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

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

请更新您的配置。

回答by Valerie

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

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 <path/to/json>to 将 jest 指向您的配置 json 文件,则您只将 JSON 信息放入文件中,而没有“jest”关键字。

// 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 来配置 jest,请添加“jest”键,然后添加您的配置。

// package.json 
{ 
   "name": "packageName",
   "version": "1.0.0",
   "jest": {
     "verbose": true
   }
}