简体   繁体   English

摩卡咖啡测试即将推出

[英]Mocha Tests Are Timing Out

I'm running Node.js 4.0 so it supports generators now. 我正在运行Node.js 4.0,因此它现在支持生成器。

I've tried gulp-mocha-co and also recently removed that as well as upgraded to Node 4.0 since it supports generators now. 我已经尝试过gulp-mocha-co,最近还删除了它,并升级到Node 4.0,因为它现在支持生成器。

Either way, as soon as I started to try to make my mocha tests generator friendly I get timeouts on all those tests after adding the * to make my mocha unit tests generators. 无论哪种方式,一旦我开始尝试使我的摩卡测试生成器变得友好,在添加*使我的摩卡单元测试生成器之后,我就会在所有这些测试上超时。 I noticed that it doesn't even run my test implementation code. 我注意到它甚至没有运行我的测试实现代码。 It gets to the *function() of my test and that's when it just sits and times out. 它到达我测试的* function(),这就是它坐下并超时的时候。

I am using gulp-mocha right now. 我现在正在使用gulp-mocha

myTests.js myTests.js

"use strict";

var chai = require('chai'),
    should = chai.should(),
    testUtil = require('../../../test/testUtilities'),
    carUseCase = require('../../../src/usecases/carGet'),
    gateway= require('../../../test/gateway'),
    carRequestModel = require('../../../src/models/http/request/carRequest');

describe('Get Car by Id', function() {

    it('should return no car when no cars exist', function*(done){
        var cars = [];

        inMemoryGateway.data(cars);
        carUseCase.gateway(gateway);

        var request = testUtil.createCarRequest();
        var responseModel = yield carUseCase.find(request);
        should.not.exist(responseModel.cars);

        var request = testUtil.createCarRequest(0, "", "", "");
        var responseModel = yield carUseCase.find(request);
        should.not.exist(responseModel.cars);

        done();
    });

gulp.js gulp.js

var gulp = require('gulp'),
    mocha = require('gulp-mocha');

...
    gulp.task('mocha-unit', function() {
        process.env.PORT = 5001;
        return gulp.src([config.test.src.unit], { read: false })
            .pipe(mocha({
                reporter: config.test.mocha.reporter,
                ui: 'bdd'
            }))
    });

carGet.js carGet.js

var car = require('../entities/car'),
    realGateway = require('../../src/gateways/carGateway'),
    carResponse = require('../../src/models/http/response/carResponse'),
    _gateway;

module.exports = {
    find: function *(carRequest){

        carResponse.http.statusCode = 200;

        var entity = yield _gateway.find(carRequest.id);

        if(!entity.cars || entity.cars.length == 0){
            entity.cars = null;
            carResponse.http.statusCode = 204;
        }

        carResponse.cars = entity.cars;

        return carResponse;
    }
};

gatewayTestDouble.js gatewayTestDouble.js

'use strict';

var _data;

module.exports = {
    data: function(data){
        _data = data
    },
    find: function *(id) {
        var found = [];

        if(id == null && hasData(_data)){
            yield _data;
            return;
        }

        if(!id && !isPositiveNumber(id)){
            yield found;
            return;
        }

        if(isPositiveNumber(id) && hasData(_data)) {
            for (var i = 0; i < _data.length; i++) {
                if (_data[i].id === id)
                    found.push(_data[i]);
            }
        }

        yield found;
    }
};

Error 错误

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

There's a couple things going on here. 这里发生了几件事。

  1. Since you've declared done as a callback argument, Mocha waits for it to be called, which never happens because... 由于您已将done声明为回调参数,因此Mocha等待它被调用,这永远不会发生,因为...
  2. Mocha doesn't support generators as callbacks. Mocha不支持将生成器用作回调。 All it sees is that your callback returned an iterator. 它所看到的只是您的回调返回了迭代器。 Since Mocha doesn't run the iterator to completion, the done() call is never reached. 由于Mocha不会运行迭代器来完成操作,因此永远不会到达done()调用。

Mocha does however support functions that return promises, as a mutually-exclusive alternative to functions that declare done in their arg lists. 但是,Mocha 确实支持返回承诺的函数,作为在arg列表中声明done函数的互斥替代。

The co utility can wrap generators that iterate multiple promises, turning them into functions that return a single promise. co实用程序可以包装可迭代多个promise的生成器,将它们转换为返回单个promise的函数。

In order to work, don't declare done in the arg list, then import co and do something like this: 为了工作,请不要在arg列表中声明done ,然后导入co并执行以下操作:

it('should foo', co.wrap(function*() {
  var foo = yield somethingThatReturnsAPromise();
  // do something with foo
}));

Note that you could alternatively do the below, and Mocha wouldn't be able to tell the difference: 请注意,您也可以执行以下操作,而Mocha无法分辨出区别:

it('should foo', co.wrap(function*() {
  return somethingThatReturnsAPromise().then(function(foo) {
    // do something with foo
  });
}));

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM