Mocha和Chai如约超时

Mocha and Chai-as-promised time out

本文关键字:超时 Chai Mocha      更新时间:2024-04-22

这里有一个我认为应该有效的测试。

var chai = require( 'chai' ),
    chaiAsPromised = require( 'chai-as-promised' ),
    assert = require( 'chai' ).assert,
    should = require( 'chai' ).should(),
    saveImage = require( 'helpers/save-image' ),
    Mocha = require( 'mocha' ),
    path = require( 'path' ),
    getUser = require( 'helpers/get-user' )
describe( 'GetUser', function () {
    describe( "getUser( token )", function() {
        it( "should return a user's ID when given token", function() {
            this.timeout( 4000 )
            var token = "LONG-STRING"
            return getUser( token ).should.eventually.include( 'ANOTHER-STRING' )
        })
    })
})

这是的功能

var User = require( '../models/userModel' ),
    Q = require( 'q' )
module.exports = function getUser ( token ) {
    return Q.Promise( function ( resolve, reject, notify ) {
        User.findOne( { token: token } ).exec()
        .then( function ( data ) {
            if ( !data )
                reject( new Error( "There was a problem getting the user. No user with that token." ) )
            resolve( data.id )
        }, function ( error ) {
            reject( new Error( error ) )
        })
    })
}

当我在我的应用程序中调用这个函数,并使用节点theseus调试器时,我看到第8行的函数会触发(.then( function ( data ) { ...)。

但当我用调试器运行Mocha时,我看到该函数或任何其他函数都不会启动。我只看到4号线和5号线有火警。测试使用与我在应用程序前端使用的token参数完全相同的LONG-STRING

为什么Mocha和node对我的应用程序的看法不同,以及我如何让这个测试发挥作用?

问题是应用程序实际上并没有运行,我需要像app = require( '../server.js' )一样启动它。