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

6

25

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!

Mayfield answered 26/12, 2015 at 6:36 Comment(12)
Are you using $httpBackend in your unit tests to stub out your $http service calls? docs.angularjs.org/api/ngMock/service/$httpBackendMod
Yes. that's why I'm wondering because the directive function which I'm testing have 4 http calls and all are simulated with fake response then why it's throwing this error!Mayfield
Have you eliminated the possibility that a different test is causing the error to be thrown, i.e. make sure the test you suspect is the only test being run?Mod
Also, does the directive function call any other services that are not mocked, that may trigger an http request? Be sure to mock everything...even $exceptionHandler could be causing your problem.Mod
@ShaunScovil Thanks for your help but I already make sure all of these cases.Mayfield
Can you show the test and code being tested?Mod
Show us some code please!Soaring
I added code. Please have a lookMayfield
Is your it block inside of a describe block?Toandfro
yes. I didn't paste full code. I verified that this is the block which is having problem as I tried running with xit for this block and it's working fine.Mayfield
Your code is a big mess. That's not how test cases should look like. Please refactor it and use //given//when//then pattern, also test only one with one unit case, otherwise it'll be a huge, complex and unmaintanable test case. Also it's not normal that you have 10+ $httpBackend mocks. Try to clear it and then we can help figuring out what is the issue.Soaring
When I run into weird problems that seem to not make any sense, I try to run it with a different browsers. Chrome or FF might give you some more specific, and actionable error messages or at least some more clues to triangulate the cause.Congresswoman
K
4

I experienced this error, but it was because I had a describe function without an it function inside.

Incorrect

describe('helpDocsDirective', function () {   
    expect(true).toBe(true);
});

Correct

describe('helpDocsDirective', function () {
    it("should return true", function () {
        expect(true).toBe(true);
    });
});
Kessiah answered 13/2, 2020 at 17:28 Comment(1)
🤦 Thanks for saying this. Can't believe I got caught by it.Metralgia
A
2

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?

Ampoule answered 30/11, 2016 at 22:1 Comment(1)
I was getting same error because I was testing asynchronous code, done() does the trickMarlo
C
1

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]

Ciliata answered 1/6, 2016 at 15:53 Comment(3)
Further, if I instead of adding a 3rd describe, add a 9th it() definition, the problem goes away again. - it's certainly racing somewhere...Ciliata
Please try changing to chrome launcher and re running. This will provide two things. 1 phantomjs gives terrible errors. 2. Hopefully it is phantomjs. Because chrome runs headlessExaggerated
There is a known issue with more than 10 tests in a describe. Newer versions of jasmine solves thisCoccid
T
1

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

Trinitrocresol answered 12/6, 2017 at 8:41 Comment(0)
D
0

You should try adding a done inside your catch() or try() just after your expect() call; like this

it('should return a Promise that resolves', (done) => {
        let promise = promiseFunction();
        promise.then(response => {
          expect(true).toBe(true);
          done();
        });
    });
Discountenance answered 5/2, 2023 at 21:55 Comment(0)
P
-1

Also had this error message in Jasmine 3.5 - it threw me more than it should have, as it was talking about async and I have some jquery in project from someone else.

It was just a syntax issue in setting up the test ... my original

it("should ...")
  expect(thing).toBe(whatever);
})

Versus the working ...

it("should ...", function(){
  expect(thing).toBe(whatever);
})
Publicity answered 20/1, 2020 at 1:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.