This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Use Chai assertions for tests in a Karma project

    Kent C. DoddsKent C. Dodds

    Chai assertions work great with the Mocha testing framework. In this lesson we'll walk through out to install Chai and karma-chai so you can use the expect assertions in your tests.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 For our test, we want to actually have an assertion in here. We're going to replace this with "expecttrue.to.be.true." This is using the Chai Expect Assertion Library.

    00:11 If we go ahead and run our test right now, we're going to get an error. That's because "expect" is not defined. We need to install the Chai Assertion Library and the Karma Helper so we can get the expect assertions. We'll do that with "npm iD chai" and "karma-chai."

    00:28 We install those dependencies as devDependencies. With those installed, we can check our package.json to ensure Chai and Karma-Chai were installed.

    00:37 We simply need to go to our Karma config and, in our frameworks, add "Chai" so that Karma will load the Chai Helper. We can use the expect assertions.

    00:49 Now, if we run our test, we're going to get "Success," here. Let's go ahead and add another one. We'll run our test again. Those were executed successfully.

    00:59 In review, for us to get our Chai assertions, we can simply install Chai and Karma-Chai. In our Karma config, in the frameworks, we add Chai as one of the frameworks we're using for our test. That's how you add Chai to your Karma configuration.

    Discuss

    Discuss