如何在Typescript和ts-jest中调试测试?

时间:2019-07-04 15:02:24

标签: node.js typescript unit-testing

我是TypeScript的新手,我无法理解为什么会出现错误,以及最主要的调试方法。

这是我的项目的结构:

<navbar></navbar>

<!-- Begin page content -->
<div class="container">
    <router-outlet></router-outlet>    
</div>

<footer-bar></footer-bar>

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

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$ 

现在的问题是:
1)为什么会失败? 2)如何调试代码? python中是否有类似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 个答案:

答案 0 :(得分:0)

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