有关阻止it()的信息调用Mocha

时间:2015-09-24 21:34:45

标签: javascript node.js asynchronous mocha bdd

我发现很难找到有关mocha中的()调用是否以及为什么阻塞的信息。它们似乎是。

以下代码按预期工作,运行速度很慢,因为有大量项要循环,但更重要的是it()调用似乎阻塞,这样一个it()调用将不会开始执行在前一个完成之前。

var request = require('request');
var path = require('path');
var fs = require('fs');
var assert = require('assert');
var colors = require('colors');
var async = require('async');


describe('@Test_Enrichment*', function () {

    var config, constants, serverURL, serverPort;

    before(function () {
        config = require('univ-config')(this.test.parent.title, 'setup/testSetup');
        constants = config.get('sc_constants');
        serverURL = config.get('test_env').smartconnect_server_config.url;
        serverPort = config.get('test_env').smartconnect_server_config.port;
    });


   fs.readdirSync(__dirname + '/test_data/enriched_payloads').filter(function (file) {

        return (path.extname(file) === '.json');

    }).map(function (file) {

        return __dirname + '/test_data/enriched_payloads/' + file;

   }).forEach(function (file) {

        it('[test] ' + path.basename(file), function (done) {   //this is the correct way to do it, but for every it() in the loop we have to wait for it complete before the next it is called
            var jsonDataForEnrichment = require(file);
            jsonDataForEnrichment.customer.accountnum = "8497404620452729";
            jsonDataForEnrichment.customer.data.accountnum = "8497404620452729";

            var options = {
                url: serverURL + ':' + serverPort + '/event',
                json: true,
                body: jsonDataForEnrichment,
                method: 'POST'
            };

            request(options, function (error, response, body) {
                if (error) {
                    done(error);
                }
                else {
                    assert(response.statusCode == 201, "Error: Response Code");
                    done();
                }
            });
        });

    });
});

---->有没有办法使it()异步,以便it()可以并行运行?

更多信息:我在Mocha的Github帐户上打开了一个问题:https://github.com/mochajs/mocha/issues/1902#issuecomment-143071872

0 个答案:

没有答案