我正在对一个结构运行一个示例Jest测试,我发现Jest的输出摘要中的错误已经过了几行。
的package.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
但是,测试问题实际上是在第29行,基于it()
的文字。
Venue.spec.ts:
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"
]
}
答案 0 :(得分:0)
Typescript的Jest测试显示错误的错误行
确保您的tsconfig.json
有sourceMaps: true
答案 1 :(得分:0)
我的距离超过几行。事实上,文件越往下,它就越“关闭”。我在这篇文章 (Jest 26 & Angular - Error Line Numbers incorrectly reported) 中详细介绍了这个问题。如果有人可以提供帮助,我还创建了一个最小的存储库。