首页 > 解决方案 > 如何使用 Jasmine 1.3.1 和 Atom 进行异步测试

问题描述

我正在关注有关异步支持的 Jasmine 1.3 文档,但无法使示例正常工作。

我正在使用的(稍作修改)源代码spec/async-spec.js如下:

describe("Asynchronous specs", function() {
  var value, flag;

  it("should support async execution of test preparation and expectations", function() {

  runs(function() {
    flag = false;
    value = 0;

    console.log("HERE"); 

    setTimeout(function() {
      console.log("HERE2"); 
      flag = true;
    }, 750);
  });

  waitsFor(function() {
      value++;
      return flag;
  }, "The Value should be incremented", 5000);


  runs(function() {
      expect(value).toBeGreaterThan(0);
    });
  });
});

我正在使用以下命令运行它:

atom --test --timeout 60 spec/async-spec.js

它给出了以下结果:

HERE
F

Asynchronous specs
  it should support async execution of test preparation and expectations
    timeout: timed out after 5000 msec waiting for The Value should be incremented


Finished in 5.746 seconds
1 test, 1 assertion, 1 failure, 0 skipped

我希望测试同时返回HEREandHERE2并且断言通过,但事实并非如此。

atom --version详情如下:

Atom    : 1.38.2
Electron: 2.0.18
Chrome  : 61.0.3163.100
Node    : 8.9.3

确切的茉莉花版本是:1.3.1 revision 1354556913

我对 Atom/Jasmine 测试相当陌生,因此将不胜感激任何帮助。

标签: javascriptunit-testingasynchronousjasmineatom-editor

解决方案


这不是一个理想的解决方案,因为出于几个原因我想继续使用默认的 Jasmine 版本,但我发现安装以下软件包使我能够获得更好的异步支持:

https://www.npmjs.com/package/atom-jasmine2-test-runner

然后我可以参考 Jasmine 2.9 文档并正确实现它们:

https://jasmine.github.io/2.9/introduction


推荐阅读