首页 > 解决方案 > 尝试运行 TypeScript 测试时出现开玩笑错误

问题描述

尝试在双客户端/服务器存储库中运行测试时,出现以下错误,我似乎无法克服。

> jest --debug
{
  "configs": [
    {
      "automock": false,
      "cache": true,
      "cacheDirectory": "/private/var/folders/ff/6qxxwcg50zq0mr4xzbfbyqyh0000gp/T/jest_dy",
      "clearMocks": false,
      "coveragePathIgnorePatterns": [
        "/node_modules/"
      ],
      "cwd": "/Users/jsavino/Development/mercury-mail-admin",
      "detectLeaks": false,
      "detectOpenHandles": false,
      "errorOnDeprecated": false,
      "extraGlobals": [],
      "forceCoverageMatch": [],
      "globals": {},
      "haste": {
        "throwOnModuleCollision": true
      },
      "injectGlobals": true,
      "moduleDirectories": [
        "node_modules"
      ],
      "moduleFileExtensions": [
        "js",
        "json",
        "jsx",
        "ts",
        "tsx",
        "node"
      ],
      "moduleNameMapper": [],
      "modulePathIgnorePatterns": [],
      "name": "d373cb7a79c783e70b01f44f2dfaf64c",
      "prettierPath": "prettier",
      "resetMocks": false,
      "resetModules": false,
      "restoreMocks": false,
      "rootDir": "/Users/jsavino/Development/mercury-mail-admin",
      "roots": [
        "/Users/jsavino/Development/mercury-mail-admin/server"
      ],
      "runner": "jest-runner",
      "setupFiles": [],
      "setupFilesAfterEnv": [],
      "skipFilter": false,
      "slowTestThreshold": 5,
      "snapshotSerializers": [],
      "testEnvironment": "/Users/jsavino/Development/mercury-mail-admin/node_modules/jest-environment-jsdom/build/index.js",
      "testEnvironmentOptions": {},
      "testLocationInResults": false,
      "testMatch": null,
      "testPathIgnorePatterns": [
        "/node_modules/"
      ],
      "testRegex": [],
      "testRunner": "/Users/jsavino/Development/mercury-mail-admin/node_modules/jest-jasmine2/build/index.js",
      "testURL": "http://localhost",
      "timers": "real",
      "transform": [
        [
          "^.+\\.(ts|tsx)$",
          "/Users/jsavino/Development/mercury-mail-admin/node_modules/ts-jest/dist/index.js",
          {}
        ],
        [
          "^.+\\.tsx?$",
          "/Users/jsavino/Development/mercury-mail-admin/node_modules/ts-jest/dist/index.js",
          {}
        ]
      ],
      "transformIgnorePatterns": [
        "/node_modules/",
        "\\.pnp\\.[^\\/]+$"
      ],
      "watchPathIgnorePatterns": []
    }
  ],
  "globalConfig": {
    "bail": 0,
    "changedFilesWithAncestor": false,
    "collectCoverage": false,
    "collectCoverageFrom": [],
    "coverageDirectory": "/Users/jsavino/Development/mercury-mail-admin/coverage",
    "coverageProvider": "babel",
    "coverageReporters": [
      "json",
      "text",
      "lcov",
      "clover"
    ],
    "detectLeaks": false,
    "detectOpenHandles": false,
    "errorOnDeprecated": false,
    "expand": false,
    "findRelatedTests": false,
    "forceExit": false,
    "json": false,
    "lastCommit": false,
    "listTests": false,
    "logHeapUsage": false,
    "maxConcurrency": 5,
    "maxWorkers": 15,
    "noStackTrace": false,
    "nonFlagArgs": [],
    "notify": false,
    "notifyMode": "failure-change",
    "onlyChanged": false,
    "onlyFailures": false,
    "passWithNoTests": false,
    "projects": [],
    "rootDir": "/Users/jsavino/Development/mercury-mail-admin",
    "runTestsByPath": false,
    "skipFilter": false,
    "testFailureExitCode": 1,
    "testPathPattern": "",
    "testSequencer": "/Users/jsavino/Development/mercury-mail-admin/node_modules/@jest/test-sequencer/build/index.js",
    "updateSnapshot": "new",
    "useStderr": false,
    "watch": false,
    "watchAll": false,
    "watchman": true
  },
  "version": "26.6.3"
}
TypeError: _jestHasteMap(...).default is not a constructor
    at Function.createHasteMap (/Users/jsavino/Development/mercury-mail-admin/node_modules/jest-runtime/build/index.js:475:12)
    at /Users/jsavino/Development/mercury-mail-admin/node_modules/@jest/core/build/cli/index.js:327:55
    at Array.map (<anonymous>)
    at buildContextsAndHasteMaps (/Users/jsavino/Development/mercury-mail-admin/node_modules/@jest/core/build/cli/index.js:324:13)
    at _run10000 (/Users/jsavino/Development/mercury-mail-admin/node_modules/@jest/core/build/cli/index.js:401:47)
    at runCLI (/Users/jsavino/Development/mercury-mail-admin/node_modules/@jest/core/build/cli/index.js:271:9)
    at async Object.run (/Users/jsavino/Development/mercury-mail-admin/node_modules/jest/node_modules/jest-cli/build/cli/index.js:163:37)

我的回购是这样组织的:

package.json
jest.config.js
tsconfig.json
server/ <- Location where the tests files are being run. A pretty basic express app written in typescript
client/ <- The output of CRA, also in typescript. Jest tests in this directory run fine

我的jest.config.js

module.exports = {
  roots: ["<rootDir>/server"],
  testMatch: null,
  transform: {
    "^.+\\.(ts|tsx)$": "ts-jest",
  },
  testRegex: [],
  preset: "ts-jest",
};

我的tsconfig.json

{
    "compilerOptions": {
      "target": "ES2020",                                /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
      "module": "commonjs",                           /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
      "outDir": "./build",                              /* Redirect output structure to the directory. */
      "rootDir": "./server",                                /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
      "strict": true,                                 /* Enable all strict type-checking options. */
      "moduleResolution": "node",                  /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
      "allowSyntheticDefaultImports": true,        /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
      "esModuleInterop": true,                        /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
      "resolveJsonModule": true,
      "skipLibCheck": true,                           /* Skip type checking of declaration files. */
      "forceConsistentCasingInFileNames": true        /* Disallow inconsistently-cased references to the same file. */
    },
    "include": [
      "./server"
    ],
    "exclude": [
      "client"
    ]
  }

该应用程序本身在编译/直接运行时都可以正常工作。

我的依赖:

  "dependencies": {
    "@kubernetes/client-node": "^0.14.3",
    "body-parser": "^1.18.3",
    "connect": "^3.7.0",
    "cors": "^2.8.5",
    "cronstrue": "^1.112.0",
    "express": "^4.17.1",
    "express-openapi-validator": "^4.12.7",
    "swagger-express-router": "^1.0.0",
    "swagger-routes-express": "^3.3.0",
    "swagger-ui-express": "^4.1.6",
    "ts-jest": "^26.5.5",
    "uuid": "^8.3.2",
    "validator": "^13.5.2",
    "yamljs": "^0.3.0"
  },
  "devDependencies": {
    "@babel/core": "^7.13.16",
    "@babel/preset-env": "^7.13.15",
    "@babel/preset-typescript": "^7.13.0",
    "@types/cors": "^2.8.10",
    "@types/express": "^4.17.11",
    "@types/jest": "^26.0.22",
    "@types/node": "^14.14.40",
    "@types/swagger-ui-express": "^4.1.2",
    "@types/uuid": "^8.3.0",
    "@types/validator": "^13.1.3",
    "@types/yamljs": "^0.2.31",
    "babel-jest": "^26.6.3",
    "concurrently": "^6.0.2",
    "jest": "^26.6.3",
    "nodemon": "^2.0.7",
    "ts-node": "^9.1.1",
    "typescript": "^4.2.4"
  }

我已经被这个问题难住了几天,所以任何朝着正确方向的轻推将不胜感激。谢谢!

标签: typescriptjestjs

解决方案


原来这是一个奇怪的package-lock.json问题。擦掉node_modules/package-lock.json重新安装固定的东西。不太确定事情是如何变得异常的,但他们以某种方式做到了。


推荐阅读