玩笑会重新定义错误全局变量吗?
我有一个非常简单的测试:
beforeAll(async () => {
try {
await db.connect();
} catch(e: any) {
expect(e).toBeInstanceOf(Error);
}
});
但是由于这个非常奇怪的消息而失败
expect(received).toBeInstanceOf(expected)
Expected constructor: Error
Received constructor: Error
,因此我怀疑error
在运行嘲笑测试时的值重新定义。
jest.config.js:
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testPathIgnorePatterns: ["/node_modules/"]
};
部分 package.json:
{
"devDependencies": {
"@types/jest": "28.1.3",
"@types/node": "18.0.0",
"jest": "28.1.1",
"ts-jest": "28.0.5",
"ts-node": "10.8.1",
"typescript": "4.7.4",
},
"scripts": {
"coverage": "jest --coverage",
"test": "jest",
}
}
tsconfig.json:
{
"compilerOptions": {
"alwaysStrict": true,
"esModuleInterop": true,
"moduleResolution": "Node",
"noImplicitAny": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"strict": true,
"strictBindCallApply": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"target": "ESNext"
},
"exclude": ["*.d.ts"],
"include": ["*.ts", "test/*.ts"]
}
I have a very simple test:
beforeAll(async () => {
try {
await db.connect();
} catch(e: any) {
expect(e).toBeInstanceOf(Error);
}
});
but it fail with this really strange message
expect(received).toBeInstanceOf(expected)
Expected constructor: Error
Received constructor: Error
so I suspect the value of Error
while running a jest test is redefined.
jest.config.js:
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testPathIgnorePatterns: ["/node_modules/"]
};
partial package.json:
{
"devDependencies": {
"@types/jest": "28.1.3",
"@types/node": "18.0.0",
"jest": "28.1.1",
"ts-jest": "28.0.5",
"ts-node": "10.8.1",
"typescript": "4.7.4",
},
"scripts": {
"coverage": "jest --coverage",
"test": "jest",
}
}
tsconfig.json:
{
"compilerOptions": {
"alwaysStrict": true,
"esModuleInterop": true,
"moduleResolution": "Node",
"noImplicitAny": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"strict": true,
"strictBindCallApply": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"target": "ESNext"
},
"exclude": ["*.d.ts"],
"include": ["*.ts", "test/*.ts"]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论