You are setting path
with the return value of the make_valid
function, yet that function returns nothing.
When I've used jasmine
, I had success with the done()
callbacks only for the tests themselves (the it()
functions).
When needing an asynchronous function before each test, I used their runs()
and waitsFor()
functions.
Try some code like this (modified for your example):
var code = null;
beforeEach( function() {
// get a token so we can do test cases. async so use jasmine's async support
runs( function() {
make_valid( function( error, returnCode ) {
if( error ) code = "ERROR_CODE";
else code = returnCode;
} );
} );
waitsFor( function() {
return code !== null;
} );
runs( function() {
expect( code ).toBeDefined();
} );
} );
afterEach( function() {
code = null;
} );
EDIT:
Based on our comments, a suggested approach for a one-time asynchronous test that applies to other tests down the line would be this sample-spec.js
. The inner tests will not pass if the outer it()
does not use done()
.
/*jslint node: true */
"use strict";
describe( "The full, outer test", function() {
var code = null;
it( "should setup for each inner test", function(done) {
make_valid( function( error, returnCode ) {
expect( returnCode ).toBeDefined();
code = returnCode;
done();
} );
} );
describe( "The inner tests", function() {
it( "inner test1", function() {
expect( code ).not.toBeNull();
} );
it( "inner test2", function() {
expect( code ).toBe(200);
} );
} );
} );
function make_valid( callback ) {
setTimeout( function(){ callback( null, 200 ); }, 500 );
}