首页 > 解决方案 > 如何在 typescript 和 ts-jest 中调试测试?

问题描述

我是打字稿的新手,我不明白为什么会出错,最重要的是如何调试它。

这是我的项目的结构:

lpuggini@lpuggini-T3420:~/pisoreview$ ls
jest.config.js  node_modules  package.json  package-lock.json  src  tsconfig.json
lpuggini@lpuggini-T3420:~/pisoreview$ tree src/
src/
├── common
│   ├── apartment.js
│   ├── apartment.ts
│   ├── firestore_manager.ts
│   ├── review.js
│   ├── review.ts
│   ├── tests
│   │   └── apartment.test.ts
│   ├── user.js
│   └── user.ts
├── frntd
│   └── frontend.ts
├── index.js
└── index.ts

3 directories, 11 files
lpuggini@lpuggini-T3420:~/pisoreview$ 

当我运行单元测试时,出现以下错误:

lpuggini@lpuggini-T3420:~/pisoreview$ npm t

> pisoreview@1.0.0 test /home/lpuggini/pisoreview
> jest

 FAIL  src/common/tests/apartment.test.ts
  ✕ create apartment (5ms)

  ● create apartment

    TypeError: ap1.toSet is not a function

       6 |     console.log(ap1)
       7 |     debugger;
    >  8 |     var s = ap1.toSet();
         |                 ^
       9 |     expect(s['city']).toBe('Madrid');
      10 |     })
      11 | 

      at Object.<anonymous> (src/common/tests/apartment.test.ts:8:17)

  console.log src/common/tests/apartment.test.ts:6
    Apartment {
      country: 'Spain',
      city: 'Madrid',
      street: 'calle amalia',
      number: 18,
      flat: 3,
      door: 'B' }

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.872s, estimated 1s
Ran all test suites.
npm ERR! Test failed.  See above for more details.
lpuggini@lpuggini-T3420:~/pisoreview$ 

失败的测试是:


import { Apartment } from '../apartment';

test('create apartment', () => {
    let ap1 = new Apartment('Spain', 'Madrid', 'calle amalia', 18, 3, 'B');
    console.log(ap1)
    debugger;
    var s = ap1.toSet();
    expect(s['city']).toBe('Madrid');
    })

lpuggini@lpuggini-T3420:~/pisoreview$ 

公寓文件是:

lpuggini@lpuggini-T3420:~/pisoreview$ cat src/common/apartment.ts 


export class Apartment {

    country: string
    city: string
    street: string
    number: number
    flat: number
    door: string
    reference: string

    constructor(country:string, city:string, street:string, number:number, flat:number, door:string) {
        this.country = country
        this.city = city
        this.street = street
        this.number = number
        this.flat = flat
        this.number = number
        this.door = door
    }

    public toString() {
        return ` ${this.country}_${this.city}_${this.street}_${this.flat}_${this.number}_${this.door}` 
    }

    public toSet() {

        return {
        "country": this.country,
        "city": this.city,
        "street": this.street,
        "number": this.number,
        "flat": this.flat,
        "door": this.door,
        };
}
}
lpuggini@lpuggini-T3420:~/pisoreview$ 

现在的问题是:
1)为什么会失败?2)如何调试代码?nosetests --pdbpython中有类似的东西吗?

非常感谢

标签: node.jstypescriptunit-testing

解决方案


1)代码中的主要问题是构造函数中的变量使用关键字:“数字”。您可以使用 tslint 轻松检测此类问题。代替:

   constructor(country: string, city: string, street: string, number: number, flat: number, door: string) {
    this.country = country
    this.city = city
    this.street = street
    this.number = number
    this.flat = flat
    this.number = number
    this.door = door
}

应该是这样的:

    constructor(country: string, city: string, street: string, numberapp: number, flat: number, door: string) {
    this.country = country;
    this.city = city;
    this.street = street;
    this.number = numberapp;
    this.flat = flat;
    this.number = numberapp;
    this.door = door;
}

2)我认为调试的简单方法是使用带有vscode-jest插件的Visual Studio Code

我创建了一个在56890570中工作的完整示例


推荐阅读