【发布时间】:2017-12-07 22:04:21
【问题描述】:
看起来 @angular/core/testing 中的 Angular 的 async 在 async 的测试中没有解决超时问题,而 beforeEach 也有 async。
不幸的是,我无法在任何类型的 Plunkr 或 JSFiddle 上重现此错误。
重新创建它的最简单方法是简单地创建一个全新的 Angular CLI 应用并将此代码粘贴到 app.component.spec.ts 中:
import { async } from "@angular/core/testing";
describe("test async", () => {
let count: number = 0;
beforeEach(async(() => {
count++;
console.log('async before test ' + count);
setTimeout(() => {
console.log('timeout before test ' + count);
}, 1000);
}))
it("async test 1", async(() => {
console.log('starting test 1');
setTimeout(() => {
console.log('test 1 finished');
console.log('expected count: 1, actual count: ' + count);
expect(count).toBe(1, "should be test 1");
}, 2000);
}));
it("async test 2", async(() => {
console.log('starting test 2');
setTimeout(() => {
console.log('test 2 finished');
console.log('expected count: 2, actual count: ' + count);
expect(count).toBe(2, "should be test 2");
}, 2000);
}));
it("async test 3", async(() => {
console.log('starting test 3');
setTimeout(() => {
console.log('test 3 finished');
console.log('expected count: 3, actual count: ' + count);
expect(count).toBe(3, "should be test 3");
}, 2000);
}));
});
如果您随后运行此测试,您应该会看到如下输出:
async before test 1
timeout before test 1
starting test 1
async before test 2
timeout before test 2
starting test 2
async before test 3
test 1 finished
expected count: 1, actual count: 3
timeout before test 3
starting test 3
test 2 finished
expected count: 2, actual count: 3
test 3 finished
expected count: 3, actual count: 3
但这在我的理解中是不正确的,因为测试中的超时应该在下一个测试开始之前完成,这就是异步的全部意义。
这是正常工作时的输出结果:
async before test 1
timeout before test 1
starting test 1
test 1 finished
expected count: 1, actual count: 1
async before test 2
timeout before test 2
starting test 2
test 2 finished
expected count: 2, actual count: 2
async before test 3
timeout before test 3
starting test 3
test 3 finished
expected count: 3, actual count: 3
任何帮助将不胜感激,谢谢!
【问题讨论】:
标签: angular unit-testing typescript jasmine