1. 26
    Use Chai assertions for tests in a Karma project
    1m 17s

Use Chai assertions for tests in a Karma project

Kent C. Dodds
InstructorKent C. Dodds

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 3 years ago

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.

[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.