【问题标题】:Jest test of Typescript shows wrong error linesTypescript 的 Jest 测试显示错误的错误行
【发布时间】:2018-07-02 21:20:19
【问题描述】:

我在一个结构上运行一个示例 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

然而,测试题实际上在第 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"
    ]
}

【问题讨论】:

    标签: typescript jestjs


    【解决方案1】:

    Typescript 的 Jest 测试显示错误的错误行

    确保您的tsconfig.json 具有sourceMap: true

    【讨论】:

    • 我有 sourceMap: true,并更改为 sourceMaps:true,仍然没有任何变化。我将“sourceMaps”:true 添加到“compilerOptions”部分。我将我的 tsconfig.json 添加到问题中。
    • 我还发现Typescript编译器Schema似乎表明sourceMap。这也不起作用。
    • sourceMaps 给出:[17:52:08] 打字稿错误。未知的编译器选项“sourceMaps”。如果这会改变事情,我会在离子环境中运行。我在主项目目录中有一个文件夹(.sourcemaps)。
    • 刚刚接受了这一点,因为所有内容的较新版本都没有问题。版本和@types 现在可能已正确更新。
    • "sourceMaps" 不正确。 Plus jest with typescript 不使用 tsconfig,而是使用 babel。
    【解决方案2】:

    我的偏离了不止几行。事实上,文件越往下,它就越“关闭”。我在这篇文章 (Jest 26 & Angular - Error Line Numbers incorrectly reported) 中详细说明了这个问题。如果有人可以提供帮助,我还创建了一个最小的 repo。

    【讨论】:

      猜你喜欢
      • 2018-03-14
      • 1970-01-01
      • 2020-01-11
      • 2017-09-22
      • 2018-09-10
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多