角度异步不能解决超时问题

时间:2017-12-07 22:04:21

标签: angular unit-testing typescript jasmine

来自async的Angular @angular/core/testing似乎并未解决async测试中的超时问题,因为async之前有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); })); }); 同样。  不幸的是,我还没有能够在任何类型的Plunkr或JSFiddle上重现这个bug。

重新创建它的最简单方法是简单地创建一个全新的Angular CLI应用程序并将此代码粘贴到app.component.spec.ts中:

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

但是根据我的理解这是不正确的,因为测试中的超时应该在下一次测试开始之前完成,这是异步的全部要点。

如果输出正常,输出将是这样的:

require 'redis-rack'

use Rack::Session::Redis, :redis_server => "redis://#{ENV['REDIS_HOST']}:6379/0"

非常感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

在编写本文时,它是Angular的一个错误。我发现了2张确认为此的门票:#16647#12115

答案 1 :(得分:-2)

  来自@ angular / core / testing的

async在使用异步的测试时没有解决超时问题,而且还有一个带有异步的beforeEach

async只能解析Promise setTimeout。您可以在延迟函数

中包装setTimeout
const delay = (ms) => new Promise(res => setTimeout(res,ms));