1. 26
    Use Chai assertions for tests in a Karma project
    1m 17s
⚠️ This lesson is retired and might contain outdated information.

Use Chai assertions for tests in a Karma project

Kent C. Dodds
InstructorKent C. Dodds
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 2 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.

egghead
egghead
~ 2 hours ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today