如何编写 Jest 配置文件

新手上路,请多包涵

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

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

不幸的是,该文档没有包含任何关于配置文件应该是什么样子的描述。

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

 testPathDirs = ['coredata/src'];

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

跑了:

 $ jest --config=jest-config.js

…但是我得到了这种类型的错误:

 $ 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)

原文由 Carles Barrobés 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 733
2 个回答

我发现配置文件是 JSON。

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

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

原文由 Carles Barrobés 发布,翻译遵循 CC BY-SA 3.0 许可协议

在我看来,您可以直接在 package.json 中配置 jest,请参阅 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 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题