This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Use Chai assertions for tests in a Karma project

1:17 Tools lesson by

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.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

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.

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.

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

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

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.

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.

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.


Featured course:

Building apps with Ionic 2

Building apps with Ionic 2

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?