无法在同步模式下运行Mocha.js [英] Cannot run Mocha.js in synchronous mode

查看:99
本文介绍了无法在同步模式下运行Mocha.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用在Node.js实例中运行的Mocha测试存储过程.我有这个测试框架:

I am testing stored procedures with mocha running in a nodejs instance. I have this test skeleton:

var chai              = require('chai'),
    MyReporter        = require("../MyReporter.js"),
    chokidar          = require('chokidar'),
    expect            = chai.expect,
    should            = chai.should,
    assert            = chai.assert;

var Mocha = require('mocha');
amochai = new Mocha({
   bail: false, 
   debug: true
});

amochai.addFile("mytest_v1.js");

function runMocha(callback) {
    amochai.run(function () {
    callback();
  });
}

watcher.on('change', function(path, stats) {
      runMocha(function () {});
}

问题:尽管我的所有测试都是这样编写的,但我的测试始终以异步模式运行:

Problem: My tests are always run in an asynchronous mode, although all my tests are written like this:

describe('Mysql stored procedures', function(){ 
   describe('Add this data', function(){
      it('-- Should return this information', function(){ 
         // asserts
      });
   });
});

没有done()回调,我的意思是无处不在,因此,正如到处都提到默认情况下mocha.js是同步的,这可能是我的代码以异步模式运行的原因吗?

There is no done() callback, I mean nowhere, so, as it is mentioned everywhere that mocha.js is synchronous by default, what could be the reason why my code is running in a asynchronous mode ?

PATCH

要解决我的问题,我不得不使用before()并检查我的测试状态,但这成了维护的噩梦.

To patch my problem, I had to use before() and check my tests state, but this becomes a nightmare to maintain.

推荐答案

您正在运行在同步Mocha测试中异步的操作.

You are running operations that are asynchronous in your synchronous mocha tests.

说Mocha测试是同步的"是模棱两可的.这可能意味着:

Saying a Mocha test is "synchronous" is ambiguous. It could mean:

  • 测试所测试的操作是同步进行的.
  • Mocha以同步方式处理测试.

两者是不相等的.一个并不意味着另一个.默认情况下,Mocha会同步处理所有测试.我们可以使它异步处理测试,但可以将参数添加到传递给it的回调中(或其他测试接口中的等效参数). (后来的Mocha版本最终添加了另一种使Mocha异步处理测试的方法:返回一个Promise.但是在下面的示例中,我将使用回调.)因此,我们可以有4种同步性组合:

The two are not equivalent. One does not entail the other. By default Mocha handles all tests synchronously. We can make it handle a test asynchronously but adding a parameter to the callback we pass to it (or its equivalent in other test interfaces). (Later versions of Mocha eventually added another way to make Mocha handle a test asynchronously: return a promise. But I'm going to use callbacks for the following examples.) So we can have 4 combinations of synchronicity:

  • 操作同步,Mocha测试同步.

  • Operation synchronous, Mocha test synchronous.

it("reads foo", function () {
    fs.readFileSync("foo");
    // ssert what we want to assert.
});

没问题.

操作同步,Mocha测试异步.

Operation synchronous, Mocha test asynchronous.

it("reads foo", function (done) {
    fs.readFileSync("foo");
    // Assert what we want to assert.
    done();
});

让Mocha测试异步进行是没有意义的,但是没问题.

It is pointless to have the Mocha test be asynchronous, but no problem.

异步操作,Mocha测试异步.

Operation asynchronous, Mocha test asynchronous.

it("reads foo", function (done) {
    fs.readFile("foo", function () {
        // Assert what we want to assert.
        done();
    });
});

没问题.

操作异步,Mocha测试同步.

Operation asynchronous, Mocha test synchronous.

it("reads foo", function () {
    fs.readFile("foo", function () {
        // Assert what we want to assert.
    });
});

这是一个问题. Mocha将立即从测试回调中返回并调用成功(假定fs.readFile不会引发异常).异步操作仍将被调度,并且回调可能仍会在以后被调用.这里重要的一点是:Mocha没有权力使其测试的操作同步化.因此,使 Mocha测试同步对测试中的操作没有影响.如果它们是异步的,则无论我们告诉Mocha什么,它们都将保持异步.

This is a problem. Mocha will return right away from the test callback and call it successful (assuming fs.readFile does not raise an exeption). The asynchronous operation will still be scheduled and the call back may still be called later. One important point here: Mocha does not have the power to make the operations it tests synchronous. So making the Mocha test synchronous has no effect on the operations in the test. If they are asynchronous, they will remain asynchronous, no matter what we tell Mocha.

在您的系统中,最后一种情况将导致存储过程的执行与DB系统一起排队.如果此排队无误地发生,则摩卡立即结束.然后,如果有文件更改,您的观察者将启动另一个Mocha运行,并将更多操作排队,等等.

This last case, in your system would cause the execution of the stored procedures to be queued with the DB system. If this queuing happens without error, Mocha finishes right away. Then if there is a file change, your watcher launches another Mocha run and more operations are queued, etc.

这篇关于无法在同步模式下运行Mocha.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆