【问题标题】:Jest Couldn't find preset "env" relative to directory开玩笑找不到相对于目录的预设“env”
【发布时间】:2018-12-15 03:10:15
【问题描述】:

我正在尝试使用 Jest 来测试我的节点 API。我有一个简单的 npm 脚本,它调用 jest

{
  "scripts": {
    "start": "node server.js",
    "dev": "nodemon server.js",
    "test": "jest",
    "precommit": "lint-staged"
  },
  "dependencies": {
    "@koa/cors": "^2.2.1",
    "apollo-link-http": "^1.5.4",
    "apollo-server-koa": "^1.3.6",
    "axios": "^0.18.0",
    "dotenv": "^6.0.0",
    "graphql": "^0.13.2",
    "graphql-tools": "^3.0.4",
    "koa": "^2.5.1",
    "koa-bodyparser": "^4.2.1",
    "koa-router": "^7.4.0",
    "node-fetch": "^2.1.2"
  },
  "devDependencies": {
    "husky": "^0.14.3",
    "jest": "^23.3.0",
    "lint-staged": "^7.2.0",
    "nodemon": "^1.17.5",
    "prettier": "^1.13.7"
  },
  "lint-staged": {
    "gitDir": "./",
    "*.{js,json}": [
      "prettier --no-semi --print-width 140 --tab-width 2 --write",
      "git add"
    ]
  },
  "jest": {
    "bail": true,
    "verbose": true,
    "testMatch": [
      "<rootDir>/**/*.test.js"
    ]
  }
}

我的 package.json 中有一个 jest 配置,它告诉 jest 要测试什么:

"jest": {
    "bail": true,
    "verbose": true,
    "testMatch": [
      "<rootDir>/**/*.test.js"
    ]
  }

我的项目中根本没有使用 babel,但由于某种原因,当我尝试在 ./test.test.js 中运行我的一项测试时

test("Example test", () => {
  expect(true).toBe(true)
})

我收到以下错误:

$ jest
 FAIL  ./test.test.js
  ● Test suite failed to run

    Couldn't find preset "env" relative to directory "/Users/<myusername>/Projects"

      at node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
          at Array.map (<anonymous>)
      at OptionManager.resolvePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
      at OptionManager.mergePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
      at OptionManager.mergeOptions (node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
      at OptionManager.init (node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
      at File.initOptions (node_modules/babel-core/lib/transformation/file/index.js:212:65)
      at new File (node_modules/babel-core/lib/transformation/file/index.js:135:24)
      at Pipeline.transform (node_modules/babel-core/lib/transformation/pipeline.js:46:16)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        7.563s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

我已尝试添加 babel env 预设,但仍然遇到相同的错误。我认为我也不应该仅仅为了测试而添加一个 babel 配置。

我正在运行 node 版本 8.11.3 和 yarn 1.7.0 并使用版本 ^23.2.0 在项目中本地安装 jest

我如何告诉 jest 不要担心 babel env?

【问题讨论】:

  • 你的项目没有声明任何.babelrc文件吗?至少提供您的package.json 会很有用。
  • @AndreaCarraro 不太理解您的问题,抱歉。如果您问我是否有.babelrc 文件,答案是否定的。我试过添加一个,但我得到了同样的错误。我不想以任何方式转换我的代码,所以根本不需要 babel。我已在完整的 package.json 中添加。

标签: javascript node.js babeljs jestjs


【解决方案1】:

此错误通常发生在您忘记添加 babel-preset-env 依赖项时。

正如您已经说过您尝试过此解决方案,请尝试查看它是否是 babel-preset-env 的某个特定版本。

您使用的是哪个版本?

【讨论】:

  • 这应该是一条评论
  • 我使用的是current,它是节点版本 8.11.3。但是为什么开玩笑需要babel-preset-env。如果我正在使用当前版本的节点不支持但我不支持的功能,我可以理解。
【解决方案2】:

babel-jest 会在您安装 Jest 时自动添加。如果您不打算转译 ES6 代码(看起来像您的情况),也许只需重置配置:

// package.json
{
  "jest": {
    "transform": {}
  }
}

【讨论】:

  • 我已经更新了我的 jest 配置 "jest": { "bail": true, "verbose": true, "transform": {}, "testMatch": [ "&lt;rootDir&gt;/**/*.test.js" ] } 但仍然遇到同样的错误
  • 奇怪。顺便问一下testEnvironment 是否设置为node
  • 我不这么认为。你的意思是--NODE_ENV=node?
  • 我在周末中断代码后再次尝试了此操作,设置 "transform": {} 现在正在解决问题。不确定我在第一次尝试时做错了什么。感谢您的帮助。
猜你喜欢
  • 2019-05-31
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
  • 2018-04-08
  • 2019-08-15
  • 1970-01-01
  • 2016-08-27
  • 2018-07-22
相关资源
最近更新 更多