用TypeScript开玩笑:TypeError:environment.teardown不是一个函数

时间:2018-08-12 14:52:02

标签: javascript node.js typescript jestjs

尝试使用Jest运行测试时出现此错误:

 FAIL  src/__tests__/jokeGenerator.test.tsx
  ● Test suite failed to run

    TypeError: environment.teardown is not a function

      at node_modules/jest-runner/build/run_test.js:230:25

我在这里遇到了可能的解决方案:How to solve TypeError: environment.teardown is not a function?

但是在执行建议后:删除了yarn.lock文件,node_modules文件夹,从package.json中删除了Jest,并再次使用yarn重新安装了所有内容-我遇到了一个新问题:

  ● Test suite failed to run

    TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string

      at assertPath (path.js:39:11)
      at Object.relative (path.js:1173:5)
      at Object.getCacheKey (node_modules/ts-jest/dist/utils/get-cache-key.js:15:16)

我很直觉,以前的解决方案对其他人有用的原因是因为他们使用了create-react-app,然后在它旁边安装了一个相互矛盾的玩笑版本。如果是这种情况,则上述解决方案不适用于我的问题,因为我没有使用create-react-app

所以我重新安装了jest和@ types / jest,现在遇到了相同的初始问题...

这是我的webpack配置:

module.exports = {
  entry: './src/index.tsx',
  devServer: {
    contentBase: __dirname + '/dist/',
  },
  module : {
    rules: [
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        use: {
          loader: 'ts-loader'
        }
      },
      {
        test: /\.css$/,
        exclude: /node_modules/,
        use: [
          'style-loader',
          {
            loader: 'typings-for-css-modules-loader?modules?named',
            options: {
              modules: true,
              namedExport: true
            }
          }
        ]
      }
    ]
  }
}

这是我的package.json:

{
  "name": "practice-testing",
  "private": true,
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "start": "webpack-dev-server --mode development --open --hot",
    "build": "webpack --mode production",
    "test": "jest"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/jest": "^23.3.1",
    "@types/react": "^16.4.9",
    "@types/react-dom": "^16.0.7",
    "babel-core": "^6.26.3",
    "babel-jest": "^23.4.2",
    "css-loader": "^1.0.0",
    "css-modules": "^0.3.0",
    "jest": "^23.5.0",
    "react-testing-library": "^5.0.0",
    "style-loader": "^0.22.1",
    "ts-jest": "^23.1.3",
    "ts-loader": "^4.4.2",
    "typescript": "^3.0.1",
    "typings-for-css-modules-loader": "^1.7.0",
    "webpack": "^4.16.5",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.5"
  },
  "dependencies": {
    "react": "^16.4.2",
    "react-dom": "^16.4.2"
  },
  "jest": {
    "testEnvironment": "node"
  }
}

这是我开玩笑的配置:

module.exports = {
  "roots": [
    "<rootDir>/src"
  ],
  "transform": {
    "^.+\\.tsx?$": "ts-jest"
  },
  "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json",
    "node"
  ],
}

最后,这是我的tsconfig:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "jsx": "react",
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "sourceMap": true,
    },
    "include": [
        "./src/**/*",
    ],
    "exclude": [
        "node_modules",
    ]
}

2 个答案:

答案 0 :(得分:14)

TLDR

此错误通常意味着在jest-environment-jsdom的根目录中安装了jest-environment-node和/或node_modules,与用于运行Windows XP的Jest的版本不兼容。测试。

详细信息

这很有趣。

问题是css-modules。它具有对jest@20.0.4的依赖关系(应该在其devDependencies下)。

jest@20.0.4最后安装了jest-environment-jsdom@20.0.3jest-environment-node@20.0.3,它们安装在node_modules的根目录中。

jest@^23.5.0已安装,它在jest-environment-jsdom@^23.4.0内的多个位置安装jest-environment-node@^23.4.0node_modules/jest,但自{{1 }}版本。

node_modules指定testEnvironment时,解析过程将查找环境。 The first place it tries is within the project,在这种情况下将解析为20.0.3版本。

早期版本的测试环境并未包含Jest更高版本所要求的所有内容,包括20.0.3的定义。

Jest删除teardown(),删除css-modules和/或package.jsonpackage-lock.json并运行yarn.lock,这应该清除所有内容。

(请注意,node_modules only has 133 weekly downloads and no listed github site是我误将其添加为依赖项,它与CSS Modules不相关)

答案 1 :(得分:2)

以防万一其他人在使用纱线工作区的项目中偶然发现此问题,我通过not hoisting jest解决了项目根目录中的相同问题:

"workspaces": {
    "packages": [
        "packages/*"
    ],
    "nohoist": [
        "**/jest/**",
        "**/jest"
    ]
},