Search code examples
javascriptkarma-runner

In my Javascript Unit test, expect(true).toBeTruthy() fails


I do not understand how this is possible. I got this test from an example and it seems impossible for it to be failing, but it is:

expect(true).toBeTruthy();

.

PhantomJS 2.1.1 (Windows 7 0.0.0) Controller:TrivialCtrl should be truthy FAILED
    undefined is not a constructor (evaluating 'expect(true).toBeTruthy()')
    c:/Users/Donald/workspace/karma-trivial/spec.js:25:28

This is Karma with Chai, Mocha, and PhantomJS


Solution

  • The expect function is typically used with the chai assertion library: have you loaded this plugin?

    You need to:

    npm install chai karma-chai --save-dev

    And add karma-chai to your list of plugins.

    Additionally, reviewing the chai docs, I'm not sure if toBeTruthy is an operator: http://chaijs.com/api/bdd/.

    You can however use ok:

    expect(true).to.be.ok