Karma: One failing test causes other async tests to fail in the Karma DEBUG RUNNER

Created on 18 Aug 2017  路  2Comments  路  Source: karma-runner/karma

Consider the following tests (in Jasmine), where the first is synchronous and the second is asynchronous:

describe("The system", function() {

    it("should fail", function() {
        expect(true).toBe(false);
    });

    it("should succeed", function(done) {
        setTimeout(function() {
            expect(1).toBe(1);
            done();
        }, 10);
    });
});

Expected behaviour

  • When I run the tests in the default runner, I expect the first test to fail and the second test to pass.
  • When I run the tests in the Karma DEBUG RUNNER, I expect the first test to fail and the second test to pass.

Actual behaviour

  • When I run the tests in the default runner, the first test fails and the second test passes, as expected.
  • When I run the tests in the Karma DEBUG RUNNER, both tests fail. The error message for the second test is the same failure message that caused the first test to fail:
Uncaught Expected true to be false.
    at UserContext.<anonymous> (http://localhost:9876/base/test.spec.js:4:22) thrown

Environment Details

  • Karma version (output of karma --version): 1.7.0
  • karma.config.js file is default out of the box, configured to load '*.js' files.

Steps to reproduce the behaviour

  1. Create a new application using npm init
  2. npm install karma karma-jasmine jasmine-core karma-chrome-launcher --save-dev
  3. Create a new file called test.spec.js and add the test code that is listed above.
  4. Run karma init and accept all defaults. Enter *.js and *.spec.js as files location.
  5. Add "test": "karma start" to the package.json scripts section.
  6. run npm run test
  7. Click the Debug button to open the Karma DEBUG RUNNER.
  8. In the console of Google Chrome, you can now see the error messages.

Most helpful comment

@daanstolp I think this has resurfaced. Using Angular CLI 6.0.6 (karam ^2.0.4).
@dignifiedquire

If a component throws an error (in our case we have a validator that throws an error if it's used incorrectly), the next test will fail. Running the tests in isolation will return a success.

In our case, we have a custom validator that matches two fields, but if the selected field does not exist the validator with throw an error. This causes the next it() test to fail.

All 2 comments

@daanstolp I think this has resurfaced. Using Angular CLI 6.0.6 (karam ^2.0.4).
@dignifiedquire

If a component throws an error (in our case we have a validator that throws an error if it's used incorrectly), the next test will fail. Running the tests in isolation will return a success.

In our case, we have a custom validator that matches two fields, but if the selected field does not exist the validator with throw an error. This causes the next it() test to fail.

@michael-letcher I'm encountering this with such code:

spyOn(service, 'uploadFile').and.returnValue(throwError(Error()));

If I test in isolation, it works, if I run all tests, the second one fails.
Also, if I use DoneFn and I call it, the test including the spyOn fails

Was this page helpful?
0 / 5 - 0 ratings