71

If I have code in a test that should never be reached (for example the fail clause of a promise sequence), how can I force-fail the test?

I use something like expect(true).toBe(false); but this is not pretty.

The alternative is waiting for the test to timeout, which I want to avoid (because it is slow).

2 Answers 2

91

Jasmine provides a global method fail(), which can be used inside spec blocks it() and also allows to use custom error message:

it('should finish successfully', function (done) {
  MyService.getNumber()
  .success(function (number) {
    expect(number).toBe(2);
    done();
  })
  .fail(function (err) {
    fail('Unwanted code branch');
  });
});

This is built-in Jasmine functionality and it works fine everywhere in comparison with the 'error' method I've mentioned below.

Before update:

You can throw an error from that code branch, it will fail a spec immediately and you'll be able to provide custom error message:

it('should finish successfully', function (done) {
  MyService.getNumber()
  .success(function (number) {
    expect(number).toBe(2);
    done();
  })
  .fail(function (err) {
    throw new Error('Unwanted code branch');
  });
});

But you should be careful, if you want to throw an error from Promise success handler then(), because the error will be swallowed in there and will never come up. Also you should be aware of the possible error handlers in your app, which might catch this error inside your app, so as a result it won't be able to fail a test.

1
  • 4
    For those stuck on an older version of Jasmine, the 'fail' method is not available in 1.3, but can be effectively polyfilled by throw new Error(message). The effectiveness can depend on your test runner - also consider something like expect('[reason for failure]').toBeNull(). Sep 15, 2017 at 15:47
8

Thanks TrueWill for bringing my attention to this solution. If you are testing functions that return promises, then you should use the done in the it(). And instead of calling fail() you should call done.fail(). See Jasmine documentation.

Here is an example

describe('initialize', () => {

    // Initialize my component using a MOCK axios
    let axios = jasmine.createSpyObj<any>('axios', ['get', 'post', 'put', 'delete']);
    let mycomponent = new MyComponent(axios);

    it('should load the data', done => {
        axios.get.and.returnValues(Promise.resolve({ data: dummyList }));
        mycomponent.initialize().then(() => {
            expect(mycomponent.dataList.length).toEqual(4);
            done();
        }, done.fail);     // <=== NOTICE
    });
});
2
  • I'm using jasmine-node (version 3.0.0) for unit tests, and I'm getting errors that both fail and done.fail are not functions (probably meaning undefined). I'm having to use the inelegant expect(false).toBeTruthy() as a work-around.
    – kshetline
    Jun 22, 2019 at 16:38
  • I'm using typescript, and I put import 'jasmine'; at the top of my testcase file. Sounds like you are missing the import, or require, or something like that. Jun 25, 2019 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.