首页 > 解决方案 > Mocha 重试不适用于 should.js 进行异步调用

问题描述

我正在尝试使用 mocha retry 进行异步测试,但它似乎不起作用。当测试失败时(在回调的上下文中)它不会调用 done() ,因此重试不起作用。

我已经用这种方式编写了很多测试,我需要一个不需要更改所有测试流程的解决方案

var should = require('should');
var counter = 0;

describe('async',function(){
    it('AsyncTest', function(done) {
        setTimeout(function()
        {
           console.log('attempt::', counter++);
           counter.should.equal(3);
           done();
        }, 300);
    });
});

以下简单代码正在同步测试中工作:

var counter = 0;
var should = require('should');
describe('sync', function() {
    it('sync', function() {
        if(counter > 1) {
            console.log('counter::', counter);
        } else {
            counter++;
            counter.should.equal(10);
        }
    });
});

运行异步代码结果: mocha testRetries.js --timeout 259200000 --ui bdd --retries 3

异步尝试:: 0 1) AsyncTest

0 通过 (319ms) 1 失败

1)异步异步测试:

  Uncaught AssertionError: expected 1 to be 3
  + expected - actual

  -1
  +3

  at Assertion.fail (/Users/shaylang/dev/nodejsautomation/src/node_modules/should/cjs/should.js:258:17)
  at Assertion.value (/Users/shaylang/dev/nodejsautomation/src/node_modules/should/cjs/should.js:335:19)
  at Timeout._onTimeout (testRetries.js:20:27)

运行同步代码结果:

mocha testRetries.js --timeout 259200000 --ui bdd --retries 3

同步计数器:: 2 ✓ 同步

1 次通过(8 毫秒)

摩卡——版本 6.1.4

标签: mocha.jsshould.js

解决方案


推荐阅读