NodeJS -使用Jest运行测试:获取TypeScript配置文件错误

iqxoj9l9  于 2023-01-10  发布在  Jest
关注(0)|答案(7)|浏览(266)

I'm trying to run some tests with Jest but I keep getting an error. It's something to do with a TypeScript config file, I already searched but didn't find much. I'm using NodeJS, TypeORM with TypeScript.
I'm getting this error when running yarn test :
Error: Jest: Failed to parse the TypeScript config file
C:\Users\demis\Documents\Projects\Personal\nlw_node\jest.config.ts TypeError: registerer.enabled is not a function*** at readConfigFileAndSetRootDir (C:\Users\demis\Documents\Projects\Personal\nlw_node\node_modules\jest-config\build\readConfigFileAndSetRootDir.js:150:13) at readConfig (C:\Users\demis\Documents\Projects\Personal\nlw_node\node_modules\jest-config\build\index.js:217:18) at readConfigs (C:\Users\demis\Documents\Projects\Personal\nlw_node\node_modules\jest-config\build\index.js:406:26) at runCLI (C:\Users\demis\Documents\Projects\Personal\nlw_node\node_modules@jest\core\build\cli\index.js:230:59) at Object.run (C:\Users\demis\Documents\Projects\Personal\nlw_node\node_modules\jest-cli\build\cli\index.js:163:37)
First.test.ts :

describe("First", () => {
  it("should", () => {
    expect(2 + 2).toBe(4);
  });
});

jest.config.ts :

export default {
  bail: true,
  clearMocks: true,
  coverageProvider: "v8",
  preset: "ts-jest",
  testEnvironment: "node",
  testMatch: ["**/tests/*.test.ts"]
};

tsconfig.json :

{
   "compilerOptions": {
      "lib": [
         "es5",
         "es6"
      ],
      "target": "es5",
      "module": "commonjs",
      "moduleResolution": "node",
      "outDir": "./build",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "sourceMap": true
   }
}

package.json :

{
  "name": "nlw_node",
  "version": "0.0.1",
  "description": "Awesome project developed with TypeORM.",
  "devDependencies": {
    "@types/express": "^4.17.11",
    "@types/jest": "^26.0.20",
    "@types/node": "^8.0.29",
    "@types/uuid": "^8.3.0",
    "jest": "^26.6.3",
    "nodemon": "^2.0.7",
    "ts-jest": "^26.5.3",
    "ts-node": "3.3.0",
    "typescript": "^4.2.3"
  },
  "dependencies": {
    "body-parser": "^1.18.1",
    "express": "^4.15.4",
    "mysql": "^2.14.1",
    "reflect-metadata": "^0.1.10",
    "typeorm": "0.2.31",
    "uuid": "^8.3.2"
  },
  "scripts": {
    "start": "nodemon src/index.ts",
    "typeorm": "ts-node node_modules/typeorm/cli.js",
    "test": "jest"
  }
}

ormconfig.ts :

{
  "type": "mysql",
  "host": "localhost",
  "port": 3306,
  "username": "root",
  "password": "root",
  "database": "nlw_node",
  "synchronize": true,
  "logging": false,
  "entities": ["src/api/models/**.ts"],
  "migrations": ["src/database/migrations/**.ts"],
 
  "cli": {
    "entitiesDir": "src/entity",
    "migrationsDir": "./src/database/migrations",
    "subscribersDir": "src/subscriber"
  }
}
jv4diomz

jv4diomz1#

删除ts-node,改为安装ts-node-dev。这对我很有效!

sz81bmfz

sz81bmfz2#

我也遇到了同样的错误,尝试用Angular和Typescript在jest中运行测试。我的问题通过重新安装jest得到了解决:npm安装--保存开发jest
然后,这显示了我的设置存在一个潜在的配置问题--但至少我没有看到失败的错误。

p3rjfoxz

p3rjfoxz3#

检查babel.config.js文件是否在项目的**root /**文件夹中,如果没有问题,请按照以下说明操作

niwlg2el

niwlg2el4#

我在我的tsconfig.json文件中添加了excludeinclude,它工作了。

{
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "rootDir": "./",
    "baseUrl": "./src",
    "outDir": "./dist",
    "allowJs": true,
    "noEmit": false,
    "noEmitOnError": true,
    "noUnusedLocals": true,
    "strict": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "forceConsistentCasingInFileNames": true,
    "removeComments": true,
    "skipLibCheck": true,
    "typeRoots": [
      "@types",
      "./node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules",
    "dist"
  ],
  "include": [
    "src/**/*",
    "src/__tests__",
    "jest.config.js"
  ]
}
pbpqsu0x

pbpqsu0x5#

我也遇到了同样的问题,但测试只运行了一次。

原来我需要删除node_modules和yarn。锁定并重新安装所有内容

zqdjd7g9

zqdjd7g96#

问题是我是使用快速入门主题

启动项目的
但是当我使用安装主题手动启动项目时,由于某种原因,它工作正常。

eaf3rand

eaf3rand7#

该文件
jest.config.ts
应该是
jest.config.js

相关问题