【问题标题】:Angular async not resolving timeouts角度异步无法解决超时
【发布时间】:2017-12-07 22:04:21
【问题描述】:

看起来 @angular/core/testing 中的 Angular 的 asyncasync 的测试中没有解决超时问题,而 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


    【解决方案1】:

    在撰写本文时,这是 Angular 的一个错误。我发现 2 张票可以确认这一点:#16647#12115

    【讨论】:

      【解决方案2】:

      来自 @angular/core/testing 的异步无法解决异步测试中的超时问题,而 beforeEach 也具有异步功能

      async 只解析 Promise 而不是 setTimeout。您可以将 setTimeout 包装在延迟函数中

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

      【讨论】:

      • 如果beforeEach 没有asyncasync 仍然适用于规范。您是否有任何文档表明 async 不适用于 setTimeout
      • 您没有提供任何文档来支持 async 仅解析 Promises 而不是 setTimeout,这实际上是不正确的,并且您链接到您自己的材料,该材料在没有透露您的隶属关系的情况下讨论了这一点。我正在报告这个垃圾邮件的答案。
      • 这个答案实际上也是不正确的。 Angular 文档中没有任何地方说async 不适用于setTimeout。事实上,我链接的问题之一表明有人正在这样做。此外,当在没有 beforeEach 的规范上使用 async 时,它完全按预期工作。
      猜你喜欢
      • 2017-02-26
      • 2015-07-06
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      相关资源
      最近更新 更多