6
votes

so i have 6 karma jasmine tests, in one file i have 3, which im testing all my factories

the test file for the factory is as followed

describe('Quiz Factories', function() {

    beforeEach(function() {

        //Ensure angular modules available
        beforeEach(module('geafApp'));

        beforeEach(inject(function (_counter_) {
            counter = _counter_;
        }));

        beforeEach(inject(function (_answer_sheet_) {
            answer_sheet = _answer_sheet_;
        }));

        beforeEach(inject(function (_questions_) {
            questions = _questions_;
        }));

    });

    it('it should return a number', function () {
        expect(counter).toBeNumber();
    });

    it('it should return an empty object', function () {
        expect(answer_sheet).toBeEmptyObject();
    });

    it('it should return an empty object', function () {
        expect(questions).toHaveObject(answers);
    });

});

in my console log its showing executed 4 of 6

PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 4 of 6 SUCCESS (0.004 secs / 0.029 secs)

So for some reason after the first it 'it' in the factory test file its skipping over the other two, even though there is no failures and all are contained in a beforeEach

3
show your other files. Also, you can combine your three beforeEach injectcts into just one beforeEach.SoEzPz
which other files do you need to see? the tests or application, combining the injectors still results in same behaviourJames Kirkby
You are showing only three of the 6 tests. Where are the rest of them? Are any of the other tests running? What file is that 4th test coming from? show your karma.config as well.SoEzPz
the other three tests are on controllers, the 4th one being executed is the first test in this factory test, i know this because if i change that test to an expression that fails it shows in the logJames Kirkby

3 Answers

3
votes

Well then, let's start here. Change your file to this to clear a few things up and see if it goes away. You also need to define answers in the last test.

describe('Quiz Factories', function() {

  var counter, answerSheet, questions;

  beforeEach( function(){
    module( 'geafApp' );

    inject( function( _counter_, _answer_sheet_, _questions_ ){
      counter = _counter_;
      answerSheet = _answer_sheet_;
      questions = _questions_;
    });
  });

  describe( 'when a question is asked', function(){
    it( 'should return a number', function(){
      expect( counter ).toBeNumber();
    });

    it( 'should return an empty object', function(){
      expect( answerSheet ).toBeEmptyObject();
    });

    it( 'should return an empty object', function(){
      expect( questions ).toHaveObject( answers ); // ??? answers is not defined!!!!
    });
  });
});
9
votes

Further to the accepted answer, which is a much better structure for the tests anyway, I have found reproducible scenario for this: nested beforeEach sections found in a test cause Karma to stop running any further Jasmine tests. You can see in the question that it is indeed the case - the beforeEach's for the injects are within an outer beforeEach.

As part of a merge, one of our beforeEach lines that loads the module under test had been moved within a later beforeEach inadvertently. This was preventing all tests after that one running. Karma was reporting that x of y tests were running where x was 65 less than y, but that the test run was successful and there were none skipped.

So if you encounter this, check your report output for the last 'successfully' execute test (I say successfully in quotes as it's probably the one causing the issue) and see if that doesn't have nested beforeEach's in it.

1
votes

Also relevant:

It can also happen it there is an exception thrown before the tests hit any expect - this will not cause the test to be reported as failed - as it was never run. It will be shown as a reduced number of running tests. You can check your console logs for errors in that case.