【问题标题】:how to debug test in typescript and ts-jest?如何在 typescript 和 ts-jest 中调试测试?
【发布时间】:2019-11-15 08:25:15
【问题描述】:

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

这是我项目的结构:

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)如何调试代码? python中有nosetests --pdb之类的东西吗?

非常感谢

【问题讨论】:

    标签: node.js typescript unit-testing


    【解决方案1】:

    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) 我认为调试的简单方法是使用 Visual Studio Codevscode-jest 插件

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-28
      • 2022-07-19
      • 2016-01-19
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 2018-06-27
      • 2020-01-01
      相关资源
      最近更新 更多