提问人:teac 提问时间:11/16/2023 更新时间:11/16/2023 访问量:21
jest 错误报告显示错误的错误位置
The jest error report displays the wrong error location
问:
当我使用 jest 进行单元测试时,我编写了正确的测试用例,运行 jest 后一切看起来都很好。
我的测试用例:
it('isString', () => {
expect(isString('string')).toBeTruthy();
expect(isString(1)).toBeFalsy();
expect(isBoolean(NaN)).toBeFalsy();
expect(isString(true)).toBeFalsy();
expect(isString(null)).toBeFalsy();
expect(isString(undefined)).toBeFalsy();
expect(isString({})).toBeFalsy();
expect(isString([])).toBeFalsy();
expect(isString(new Set())).toBeFalsy();
expect(isString(new Map())).toBeFalsy();
expect(isString(Symbol(1))).toBeFalsy();
expect(
isString(() => {
/* */
}),
).toBeFalsy();
});
但是我想知道当测试用例出错时会是什么样子,所以我修改了一个用例
it('isString', () => {
expect(isString('string')).toBeTruthy();
>-- expect(isString(1)).toBeFalsy();
>++ expect(isString(1)).toBeTruthy();
expect(isBoolean(NaN)).toBeFalsy();
expect(isString(true)).toBeFalsy();
expect(isString(null)).toBeFalsy();
expect(isString(undefined)).toBeFalsy();
expect(isString({})).toBeFalsy();
expect(isString([])).toBeFalsy();
expect(isString(new Set())).toBeFalsy();
expect(isString(new Map())).toBeFalsy();
expect(isString(Symbol(1))).toBeFalsy();
expect(
isString(() => {
/* */
}),
).toBeFalsy();
});
显然,这与预期不符,它指向另一个测试用例,我收到以下错误:
FAIL utils/type/index.test.ts (9.957 s)
● type › isString
expect(received).toBeTruthy()
Received: false
21 | expect(typeOf(1)).toBe('number');
22 | expect(typeOf(true)).toBe('boolean');
> 23 | expect(typeOf(null)).toBe('null');
| ^
24 | expect(typeOf(undefined)).toBe('undefined');
25 | expect(typeOf({})).toBe('object');
26 | expect(typeOf([])).toBe('array');
at Object.<anonymous> (utils/type/index.test.ts:23:42)
我预期的错误应该如下
FAIL utils/type/index.test.ts (9.957 s)
● type › isString
expect(received).toBeTruthy()
Received: false
38 | expect(isString('string')).toBeTruthy();
> 39 | expect(isString(1)).toBeTruthy();
| ^
40 | expect(isBoolean(NaN)).toBeFalsy();
41 | expect(isString(true)).toBeFalsy();
42 | expect(isString(null)).toBeFalsy();
at Object.<anonymous> (utils/type/index.test.ts:23:42)
- 开玩笑:29.7.0
- 开玩笑:29.1.1
这是我的 Jest 配置
import path from 'path';
export default {
collectCoverage: true,
collectCoverageFrom: [
'<rootDir>/utils/**/*.ts',
'!<rootDir>/utils/*.ts',
'!<rootDir>/**/*.test.ts',
],
coverageDirectory: 'coverage',
coveragePathIgnorePatterns: ['/node_modules/'],
rootDir: path.resolve(__dirname, '../../../'),
testEnvironment: 'node',
testMatch: ['<rootDir>/utils/**/*.test.ts'],
testPathIgnorePatterns: ['/node_modules/'],
transform: {
'\\.ts$': 'ts-jest',
},
transformIgnorePatterns: ['/node_modules/', '\\.pnpm?\\.[^\\/]+$'],
};
答: 暂无答案
评论