【发布时间】:2019-01-16 18:49:37
【问题描述】:
给定班级:
export class Foo {
private static _count = 0;
id: number;
constructor() {
this.id = ++Foo._count; // starts with 1
}
}
还有测试套件:
describe('Foo Class', () => {
describe('constructor', () => {
const fooClassRef = Foo as any; // to pass typechecking
beforeEach(() => {
console.log(`start of beforeEach: ${fooClassRef._count}`);
fooClassRef._count = 0;
console.log(`end of beforeEach: ${fooClassRef._count}`);
});
describe('creating one Foo obj', () => {
console.log(fooClassRef._count);
const foo = new Foo();
it('should have an id of 1', () => {
expect(foo.id).toBe(1);
});
});
describe('creating two Foo objs', () => {
console.log(fooClassRef._count);
const foo1 = new Foo();
const foo2 = new Foo();
it('should have ids of 1 and 2', () => {
expect(foo1.id).toBe(1);
expect(foo2.id).toBe(2);
});
});
});
});
第二次测试失败:
expect(received).toBe(expected) // Object.is equality
Expected: 1
Received: 2
| const foo2 = new Foo();
| it('should have ids of 1 and 2', () => {
> | expect(foo1.id).toBe(1);
| ^
| expect(foo2.id).toBe(2);
| });
| });
生成的日志是:
0
1
start of beforeEach(): 3
end of beforeEach(): 0
start of beforeEach(): 0
end of beforeEach(): 0
似乎beforeEach 代码直到所有测试都完成后才真正运行。
【问题讨论】:
标签: typescript unit-testing testing jasmine jestjs