首页 > 解决方案 > 尝试运行 jest 的 jest 版本不正确

问题描述

我在我的角度项目中有开玩笑的测试。

我有一个 package.json 文件,指定了我想用来运行测试的 jest 版本。该文件包括:

"@types/jest": "^24.0.18",
"jest": "^24.9.0",
"jest-preset-angular": "^7.1.1",

开玩笑的配置还包括:

"setupFilesAfterEnv": [
  "<rootDir>/setup-jest.ts"
],

这就是问题发生的地方。尝试运行时jest,我收到以下消息:

● Validation Warning:

Unknown option "setupFilesAfterEnv" with value ["<rootDir>/setup-jest.ts"] was found.
This is probably a typing mistake. Fixing it will remove this message.

Configuration Documentation:
https://jestjs.io/docs/configuration.html

我看了看,jest -h发现了一个标志,它可以让我设置 jest 环境。

jest --showConfig

然而,这表明我在版本上开玩笑

"version": "23.6.0"

所以我的问题就在这里。在我做了一个之后npm i,尝试运行测试的开玩笑版本是不同的/旧的。

-g我尝试使用标志和标志安装 jest-cli save-dev

如果有任何帮助,还尝试在 VS Code 中运行测试。

请帮忙。

先感谢您。

完整的日志npx jest --showConfig

● Validation Warning:

  Unknown option "setupFilesAfterEnv" with value ["<rootDir>/setup-jest.ts"] was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration.html

{
  "configs": [
    {
      "automock": false,
      "browser": false,
      "cache": true,
      "cacheDirectory": "/var/folders/bs/wrvrgl6132df8l5ndxv40m3m0000gn/T/jest_dx",
      "clearMocks": false,
      "coveragePathIgnorePatterns": [
        "/node_modules/",
        "setup-jest.ts"
      ],
      "detectLeaks": false,
      "detectOpenHandles": false,
      "errorOnDeprecated": false,
      "filter": null,
      "forceCoverageMatch": [],
      "globals": {
        "ts-jest": {
          "tsConfig": "<rootDir>/tsconfig.spec.json",
          "stringifyContentPathRegex": "\\.html$",
          "astTransformers": [
            "jest-preset-angular/InlineHtmlStripStylesTransformer"
          ]
        }
      },
      "haste": {
        "providesModuleNodeModules": []
      },
      "moduleDirectories": [
        "node_modules"
      ],
      "moduleFileExtensions": [
        "ts",
        "html",
        "js",
        "json"
      ],
      "moduleNameMapper": [
        [
          "@app/(.*)",
          "/Users/name/Projects/project/src/app/$1"
        ],
        ...
      ],
      "modulePathIgnorePatterns": [],
      "name": "6caa4...",
      "prettierPath": "/Users/name/Projects/project/node_modules/prettier/index.js",
      "resetMocks": false,
      "resetModules": false,
      "resolver": null,
      "restoreMocks": false,
      "rootDir": "/Users/name/Projects/project",
      "roots": [
        "/Users/name/Projects/project"
      ],
      "runner": "jest-runner",
      "setupFiles": [],
      "setupTestFrameworkScriptFile": null,
      "skipFilter": false,
      "snapshotSerializers": [],
      "testEnvironment": "/Users/name/Projects/project/node_modules/jest-environment-jsdom-thirteen/build/index.js",
      "testEnvironmentOptions": {},
      "testLocationInResults": false,
      "testMatch": [
        "**/__tests__/**/*.js?(x)",
        "**/?(*.)+(spec|test).js?(x)"
      ],
      "testRegex": "",
      "testRunner": "/Users/name/node_modules/jest-jasmine2/build/index.js",
      "testURL": "http://localhost",
      "timers": "real",
      "transform": [
        [
          "^.+\\.(ts|js|html)$",
          "/Users/name/Projects/project/node_modules/ts-jest/dist/index.js"
        ]
      ],
      "watchPathIgnorePatterns": []
    }
  ],
  "globalConfig": {
    "bail": false,
    "changedFilesWithAncestor": false,
    "collectCoverage": true,
    "collectCoverageFrom": null,
    "coverageDirectory": "/Users/name/Projects/project/coverage",
    "coverageReporters": [
      "json",
      "text",
      "lcov",
      "clover"
    ],
    "coverageThreshold": null,
    "detectLeaks": false,
    "detectOpenHandles": false,
    "errorOnDeprecated": false,
    "expand": false,
    "filter": null,
    "globalSetup": null,
    "globalTeardown": null,
    "listTests": false,
    "maxWorkers": 7,
    "noStackTrace": false,
    "nonFlagArgs": [],
    "notify": false,
    "notifyMode": "always",
    "passWithNoTests": false,
    "projects": null,
    "rootDir": "/Users/name/Projects/project",
    "runTestsByPath": false,
    "skipFilter": false,
    "testFailureExitCode": 1,
    "testPathPattern": "",
    "testResultsProcessor": null,
    "updateSnapshot": "new",
    "useStderr": false,
    "verbose": null,
    "watch": false,
    "watchman": true
  },
  "version": "23.6.0"
}

在这里也显示npm config get日志:

; cli configs
metrics-registry = "http://.../.../npm-group/"
scope = ""
user-agent = "npm/6.9.0 node/v10.15.3 darwin x64"

; project config /Users/user/Projects/project/.npmrc
registry = "http://.../.../npm-group/"

; node bin location = /Users/user/.nvm/versions/node/v10.15.3/bin/node
; cwd = /Users/user/Projects/project
; HOME = /Users/user
; "npm config ls -l" to show all defaults. 

标签: jestjs

解决方案


I had the same issue, after a long search I've tried this:

type jest

Which gave me the location:

/usr/local/bin/jest

Renaming this file (or deleting it), solved the problem (note that now running jest will give command not found).


推荐阅读