mardi 24 février 2015

Mocha and Chai-as-promised time out

Here is a test I believe should work.



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' )
})

})

})


Here is the function



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 ) )
})
})
}


When I call this function in my app, and use the node-theseus debugger, I see that the function on line 8 fires (.then( function ( data ) { ...).


But when I run Mocha with the debugger I see that function, or any other, doesn't fire. I only see a call fire on line 4 and 5. The test is being done with the exact same LONG-STRING as a token argument that I'm using on my app front end.


Why is there a difference in how Mocha and node see my app and how can I make this test work?


Aucun commentaire:

Enregistrer un commentaire