如何按顺序执行异步Mocha测试(NodeJS)?

fad*_*bee 52 javascript mocha.js node.js

这个问题与NodeJS的Mocha测试框架有关.

默认行为似乎是启动所有测试,然后在进入时处理异步回调.

在运行异步测试时,我希望在调用之前的异步部分之后运行每个测试.

我怎样才能做到这一点?

pap*_*boy 35

重点不在于"结构化代码以你构造它的顺序运行"(令人惊讶!) - 而是正如@chrisdew所暗示的那样,异步测试的返回顺序无法保证.重述问题 - 在(同步执行)链的下游进行的测试不能保证由异步测试设置的所需条件在它们运行时就已准备就绪.

因此,如果您要求在第一次测试中设置某些条件(如登录令牌或类似),则必须使用类似before()测试的挂钩,然后再继续设置这些条件.

将依赖测试包装在一个块中并对它们运行异步 before挂钩(注意前面块中的'done'):

var someCondition = false

// ... your Async tests setting conditions go up here...

describe('is dependent on someCondition', function(){

  // Polls `someCondition` every 1s
  var check = function(done) {
    if (someCondition) done();
    else setTimeout( function(){ check(done) }, 1000 );
  }

  before(function( done ){
    check( done );
  });

  it('should get here ONLY once someCondition is true', function(){ 
    // Only gets here once `someCondition` is satisfied
  });

})
Run Code Online (Sandbox Code Playgroud)


Aka*_*sha 9

我用你写的东西让我很惊讶.我使用mocha和bdd样式测试(describe/it),然后在我的测试中添加了一些console.logs来查看你的声明是否符合我的情况,但看起来他们没有.

这是我用来查看"end1"和"start1"顺序的代码片段.他们是正确的订购.

describe('Characters start a work', function(){
    before(function(){
      sinon.stub(statusapp, 'create_message');
    });
    after(function(){
      statusapp.create_message.restore();
    });
    it('creates the events and sends out a message', function(done){
      draftwork.start_job(function(err, work){
        statusapp.create_message.callCount.should.equal(1);
        draftwork.get('events').length.should.equal(
          statusapp.module('jobs').Jobs.get(draftwork.get('job_id')).get('nbr_events')
        );
        console.log('end1');
        done();
      });
    });
    it('triggers work:start event', function(done){
      console.log('start2');
      statusapp.app.bind('work:start', function(work){
        work.id.should.equal(draftwork.id);
        statusapp.app.off('work:start');
        done();
      });
Run Code Online (Sandbox Code Playgroud)

当然,这也可能是偶然发生的,但我有很多测试,如果它们并行运行,我肯定会有竞争条件,我没有.

请从mocha问题跟踪器中参考此问题.根据它,测试同步进行.


WiR*_*R3D 8

使用摩卡步骤

无论它们是否异步,它都会保持测试顺序(即你的done功能仍然完全像他们那样工作).它是您的直接替代品,it而不是您使用的step

  • 我查看了代码,它似乎没有提供任何顺序保证,只是它在第一次检测到的故障时停止. (3认同)

Der*_*ler 5

我想用我们的应用程序解决同样的问题,但是接受的答案对我们来说效果不好.特别是在someCondition永远不会是真的.

我们在应用程序中使用promises,这使得很容易相应地构建测试.然而,密钥仍然是通过before钩子延迟执行:

var assert = require( "assert" );

describe( "Application", function() {
  var application = require( __dirname + "/../app.js" );
  var bootPromise = application.boot();

  describe( "#boot()", function() {
    it( "should start without errors", function() {
      return bootPromise;
    } );
  } );

  describe( "#shutdown()", function() {
    before( function() {
      return bootPromise;
    } );

    it( "should be able to shut down cleanly", function() {
      return application.shutdown();
    } );
  } );
} );
Run Code Online (Sandbox Code Playgroud)

  • 我最好将第二行和第三行代码(`application = ...`和`bootPromise = ...`)放在顶层套件("Application")的async`after`块中.否则,将不会捕获并正确报告从此代码抛出的任何异常,更糟糕的是,将阻止执行所有剩余的测试. (3认同)