简体   繁体   English

开玩笑找不到相对于目录的预设“ env”

[英]Jest Couldn't find preset “env” relative to directory

I am trying to use Jest to test my node API. 我正在尝试使用Jest测试我的节点API。 I have a simple npm script which calls jest 我有一个简单的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"
    ]
  }
}

I have a jest config in my package.json which tells jest what to test: 我的package.json有一个jest配置,告诉jest要测试什么:

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

I am not using babel at all in my project but for some reason when I try run my one test in ./test.test.js 我在项目中根本没有使用babel,但是由于某种原因,当我尝试在./test.test.js运行一个测试时

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

I get the following error: 我收到以下错误:

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

I have tried adding a babel env preset but still getting the same error. 我尝试添加babel env预设,但仍然收到相同的错误。 I don't think I should have to add a babel config just for testing either. 我认为我不必只为测试而添加babel配置。

I am running node version 8.11.3 and yarn 1.7.0 and have jest installed locally in the project using version ^23.2.0 我正在运行节点版本8.11.3和yarn 1.7.0并使用版本^23.2.0在项目中本地安装了^23.2.0

How do I tell jest not to worry about babel env? 我如何告诉玩笑不要担心babel env?

babel-jest is added automatically when you install Jest. 安装Jest时会自动添加babel-jest If you don't intend to transpile ES6 code (seems like your case) maybe just reset the config: 如果您不打算移植ES6代码(似乎像您的情况),则只需重置配置即可:

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

This error usually happens when you forget to add the babel-preset-env dependency. 当您忘记添加babel-preset-env依赖项时,通常会发生此错误。

As you have already said that you tried this solution, try to see if it is some specific version of babel-preset-env. 正如您已经说过尝试过此解决方案一样,请尝试查看它是否是babel-preset-env的某些特定版本。

Which version are you using? 您正在使用哪个版本?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:找不到相对于目录的预设“ es2015” - Error: Couldn't find preset “es2015” relative to directory 错误:找不到相对于目录的预设“ react-hmre” - Error: Couldn't find preset “react-hmre” relative to directory 错误:找不到相对于目录的预设“ es2015” - Error: Couldn't find preset “es2015” relative to directory 如何设置 babel.config.js,因此找不到相对于目录的预设“@babel/env”工作 - 反应 - How to setup babel.config.js, so Couldn't find preset "@babel/env" relative to directory works - react 找不到预设“@ babel / env” - reactjs - Couldn't find preset “@babel/env” - reactjs 错误:无法找到相对于目录“/ Users / username”的预设“es2015” - Error: Couldn't find preset “es2015” relative to directory “/Users/username” 为什么在ReactJS for React Hot Loader中出现“错误:找不到相对于目录的预设“反应热”…”? - Why the 'Error: Couldn't find preset “react-hot” relative to directory…" in ReactJS for React Hot Loader? VueJs模块构建失败:错误:无法找到相对于目录的预设“@ vue / app” - VueJs Module build failed: Error: Couldn't find preset “@vue/app” relative to directory 模块构建失败:错误:无法找到相对于目录的预设“transform-class-properties” - Module build failed: Error: Couldn't find preset “transform-class-properties” relative to directory 错误:无法在浏览器中加载相对于/的预设环境 - Error: Cannot load preset env relative to / in a browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM