我刚开始使用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这样的东西吗?
非常感谢
发布于 2019-07-23 18:04:40
1)代码中的主要问题是构造函数中的一个变量使用了一个关键字:"number“。使用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的完整示例
https://stackoverflow.com/questions/56890570
复制相似问题