NodeJS - 运行 使用 Jest 进行测试:TypeScript 配置文件出现错误
NodeJS - Running tests with Jest: getting an error with TypeScript config file
我正在尝试 运行 使用 Jest 进行一些测试,但我一直收到错误。这与 TypeScript 配置文件有关,我已经搜索过但没有找到太多。我正在使用 NodeJS、TypeORM 和 TypeScript。
当 运行ning 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"
}
}
我今天遇到了完全相同的错误,试图 运行 开玩笑地使用 Angular 和 Typescript 进行测试。
通过重新安装 jest 解决了我的问题:
npm install --save-dev jest
这表明我的设置存在潜在的配置问题 - 但至少我没有看到失败的错误。
问题是我使用快速启动主题启动项目:
但是当我使用安装主题手动启动项目时,由于某种原因它起作用了。
文件
jest.config.ts
应该是
jest.config.js
检查babel.config.js
文件是否在项目的根/文件夹中,如果不是问题follow these instructions
删除 ts-node
并改为安装 ts-node-dev
。这对我有用!
我正在尝试 运行 使用 Jest 进行一些测试,但我一直收到错误。这与 TypeScript 配置文件有关,我已经搜索过但没有找到太多。我正在使用 NodeJS、TypeORM 和 TypeScript。
当 运行ning 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"
}
}
我今天遇到了完全相同的错误,试图 运行 开玩笑地使用 Angular 和 Typescript 进行测试。 通过重新安装 jest 解决了我的问题: npm install --save-dev jest
这表明我的设置存在潜在的配置问题 - 但至少我没有看到失败的错误。
问题是我使用快速启动主题启动项目:
但是当我使用安装主题手动启动项目时,由于某种原因它起作用了。
文件
jest.config.ts
应该是
jest.config.js
检查babel.config.js
文件是否在项目的根/文件夹中,如果不是问题follow these instructions
删除 ts-node
并改为安装 ts-node-dev
。这对我有用!