我正在对一个结构运行示例 Jest 测试,我发现 Jest 的输出摘要中的错误减少了几行。
包.json
"devDependencies": {
"@types/jest": "^22.0.1",
"jest": "^22.1.4",
"jest-preset-angular": "^5.0.0",
"ts-jest": "^22.0.1",
"typescript": "^2.6.2"
},
输出:
Venue › Venue Structure › should have all the structure fields
expect(received).toBeDefined()
Expected value to be defined, instead received
undefined
20 | it('should be an instance of DataStructure', () => {
21 | expect(VenueInfo instanceof DataStructure).toBeTruthy();
> 22 | })
23 |
24 | it('should have the proper number of data fields', () => {
25 | expect(VenueInfo.NumberOfFields).toBe(14); // LastUpdated is added automatically
at src/structures/Venue.spec.ts:22:35
但是,根据
it()
中的文本,测试问题实际上位于第 29 行。
场地规格:
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { Venue } from './Venue';
import { DataStructure } from '../library/base/DataStructure';
describe('Venue', () => {
let VenueInfo: Venue;
beforeEach(() => {
VenueInfo = new Venue();
});
describe('Class', () => {
it('should be an instance of Venue', () => {
expect(VenueInfo instanceof Venue).toBeTruthy(); ;
});
it('should be an instance of DataStructure', () => {
expect(VenueInfo instanceof DataStructure).toBeTruthy();
})
it('should have the proper number of data fields', () => {
expect(VenueInfo.NumberOfFields).toBe(14); });
});
describe('Venue Structure', () => {
it('should have all the structure fields', () => {
expect(VenueInfo.Key).toBeDefined();
expect(VenueInfo.Description).toBeDefined();
expect(VenueInfo.Address1).toBeDefined();
expect(VenueInfo.Address2).toBeDefined();
expect(VenueInfo.City).toBeDefined();
expect(VenueInfo.Province).toBeDefined();
expect(VenueInfo.Country).toBeDefined();
expect(VenueInfo.PostalCode).toBeDefined();
expect(VenueInfo.Telephone).toBeDefined();
expect(VenueInfo.Latitude).toBeDefined();
expect(VenueInfo.Longitude).toBeDefined();
expect(VenueInfo.MajorIntersection).toBeDefined();
expect(VenueInfo.DartBoards).toBeDefined();
});
});
});
tsconfig.json
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"dom",
"es2015"
],
"module": "es2015",
"moduleResolution": "node",
"sourceMaps": true,
"target": "es5"
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"src/**/*.spec.ts",
"src/test-config"
],
"compileOnSave": false,
"atom": {
"rewriteTsconfig": false
}
}
从早期的配置来看,我的 Ionic 应用程序的 src 目录中也有一个 tsconfig.spec.ts,其中也有 sourceMaps: true,但似乎没有任何影响。
tsconfig.spec.json(不确定它的用途,因为我找不到该文件的引用):
{
"extends": "../tsconfig.json",
"compilerOptions": {
"allowJs": true,
"module": "commonjs",
"outDir": "../out-tsc/spec",
"sourceMaps": true,
"target": "es5"
},
"files": [
"**/*.ts"
],
"include": [
"*.spec.ts",
"**/*.spec.ts",
"**/*.d.ts",
"test-config/*.ts",
"test-config/**/*.ts"
]
}
Typescript 的 Jest 测试显示错误的错误行
确保您的
tsconfig.json
有 sourceMap: true
扩展@Juan Carlos 评论,对我有帮助的是编辑 babel 而不是编译器选项,通过将以下内容添加到 jest 配置文件中:
"globals": {
"ts-jest": {
"babelConfig": {
"sourceMaps": true
}
}
}
我的偏离了几行以上。事实上,文件越往下,它就越“偏离”。我在这篇文章中详细介绍了这个问题(Jest 26 & Angular - 错误行号错误报告)。如果有人可以帮忙的话,我还创建了一个最小的仓库。