簡體   English   中英

開玩笑找不到相對於目錄的預設“ env”

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

我正在嘗試使用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配置。

我正在運行節點版本8.11.3和yarn 1.7.0並使用版本^23.2.0在項目中本地安裝了^23.2.0

我如何告訴玩笑不要擔心babel env?

安裝Jest時會自動添加babel-jest 如果您不打算移植ES6代碼(似乎像您的情況),則只需重置配置即可:

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

當您忘記添加babel-preset-env依賴項時,通常會發生此錯誤。

正如您已經說過嘗試過此解決方案一樣,請嘗試查看它是否是babel-preset-env的某些特定版本。

您正在使用哪個版本?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM