'expect' was used when there was no current spec, this could be because an asynchronous test timed out in Jasmine 2.3.1

15,045

Solution 1

Could you have nested a couple of tests that should be separated, or have multiple async calls resolving in the same test case?

I produced this same error, but it was of my own doing. I had two async tests inside of one it(). As soon as either of the promises resolved, the test ended. The other promise resolution was orphaned.

Consider these snippets. Assume that the function under test responds correctly as called.

Note: I have left out the error paths from the then() for purposes of illustrating the issue more clearly.

This construction fails. When either of the promises return and done() is fired, the second one now fails with the "'expect' was used when there was no current spec..." error.

describe( "delay", function(){
    var calculator = new Calculator();

    it( "delays execution - add and subtract", function(done){
        delay( 1000, calculator, 'add', [ 10, 5 ] )
            .then(function(result){
                expect(result).toEqual( 15 );
                done();  // <---- as soon as this runs, test is over
            });

        delay( 500, calculator, 'subtract', [ 9, 5 ] )
            .then(function(result){
                expect(result).toEqual( 4 );
                done(); // <---- as soon as this runs, test is over
            });
    });

} );

This is the correct way to write the tests. Each promise is encapsulated in its own test.

describe( "delay", function(){
    var calculator = new Calculator();

    it( "delays execution - add", function(done){
        delay( 1000, calculator, 'add', [ 10, 5 ] )
            .then(function(result){
                expect(result).toEqual( 15 );
                done(); // <--- this is now the only resolution for  this test
            });
    });

    it( "delays execution - subtract", function(done){
        delay( 500, calculator, 'subtract', [ 9, 5 ] )
            .then(function(result){
                expect(result).toEqual( 4 );
                done(); // <--- this is now the only resolution for  this test
            });
    });

} );

As I don't yet have enough reputation to comment, I'm putting my plea here. :-)

Could you mark this answer as correct if this turns out to be your problem?

Solution 2

This is a very real problem, I am currently experiencing it too. I think there is a core bug here. I have very well encapsulated tests. they are small (at most 3 lines each)

I have a main describe section with 2 nested describes first describe has 8 it() functions second has 3it() functions.

i.e

describe("main", ()=>{
    describe("1st", ()=>{
        //here are 8 it() definitions
    })
    describe("2nd", ()=>{
        //here are 3 it() definitions
    })
})

Now when I remove a single it() definition from either describe, the issue disappears. Alternatively, if I add a 3rd describe(), the issue disappears.

This is an issue in jasmine - either they are not reporting an error correctly, or there is something horribly wrong. Alternatively it may be karma trying to be smart by running multiple tests simultaneously.. Either way, this problem is real and it's got nothing to do with messy code.

Perhaps it has to do with the underlying unit being tested - my function is recursive (though my test cases don't dive deep).

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

Solution 3

Had the same issue here, it turn out that I had a test with setTimeout. Cleared that and all good!

Share:
15,045
Jay Shukla
Author by

Jay Shukla

Cool, Share knowledge - Get knowledge.. :)

Updated on June 06, 2022

Comments

  • Jay Shukla
    Jay Shukla about 2 years

    I'm running karma test cases through gulp as below:

    gulp.task('unit-test-karma', function () {
        return gulp.src(filePaths.libraryPaths.concat(filePaths.codePathsVerbose.concat(filePaths.testPaths).concat(filePaths.htmlPaths).concat(filePaths.jadePaths)))
            //.pipe(plumber({ errorHandler: notify.onError(function(error) { console.log(error.message); return "Karma Error"; }) }))
            .pipe(karma({
                configFile: './karma.conf.js',
                action: 'run', // watch
                singleRun: true,
                reporters: [ 'dots' ]
            }));
    });
    

    When I run with action as run, IE 11 throws below error.

    IE 11.0.0 (Windows 10 0.0.0) ERROR
      'expect' was used when there was no current spec, this could be because an asynchronous test timed out
      at C:/BbCAT-WebDI/BbCAT-Web/BbCAT-Angular/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:938
    

    But if run the same with action as watch then all test case executing successfully in chrome, IE and firefox.

    After reading some post, It seems there is some issue with $http service call but not able to find from where exactly the problem is!