Testing with Mocha and Chai

Testing with Mocha and Chai

ยท

4 min read

Mocha is a JavaScript test framework and Chai is an assertion library that can be paired with any JavaScript testing framework.

Writing tests in a nutshell consists of checking the code by running it to ensure that the actual output of your code is the output you expected.

Unit testing is simply testing a unit of your code, so a small part of your code.

Integration testing is testing the interaction between two units of your code to see if they work correctly together.

Keep in mind that there are other forms of testing too like performance testing.

You can use different techniques such as Behavior Driven Development (BDD), or Test Driven Development (TDD) for a flow to how you go about testing, but I won't dive into those in this article.

The initial test you write, contains your description of the function or set of functions and what you expect the function to be doing. That initial description is named a specification or spec, and what it includes are assertions. Let's take a closer look at the parts of a test below.

Test Building Blocks

Your test usually has three parts to it:

The description of the overall functionality you will be testing, this is a title you can give to the group of tests.

describe("main overarching title", function() {...})

Describing the individual use case in a human-readable way (you can include many it() function calls in one describe block).

it("specific description of this use case", function() {...})

The code to execute inside the it block.

expect(answer).to.equal(42);

Mocha Hooks

Mocha provides hooks to set up your tests such as:

describe() This is used to group the tests.

it() This is the test case.

before() This hook runs once before the first test in this block.

after() This hook runs once after the last test in this block.

beforeEach() This hook runs before each test in this block.

afterEach() This hook runs after each test in this block.

Descriptions may be added to hooks and they may be sync or async as well. You can see more examples of how to use the above hooks in the full Mocha docs.

If you plan on writing a test but haven't completed the test yet, you can simply leave out the callback in the it() statement and that will mark it as pending in the test results. You would just leave that part out, like in the example below:

describe('isValidUsername', function () {
        it('should return false if username is invalid')
})

You can also skip the test-case or test-suite by adding .skip() after the describe (describe.skip(...)) or the it (it.skip(...)). They will show as pending in the test summary.

If you wish to only run specific tests, you can add .only() after the describe or it statements so that you only see those tests in the test results.

Chai

There are three ways of assertion in Chai, the chain-capable BDD styles expect, should and the TDD assert style.

Both expect and should use the same chainable language to construct assertions, but they work differently. The expect require is a reference to the function, while with the should require, the function is being executed.

With expect you can also include messages to prepend to any failed assertions that makes it easier to understand what is happening when your test expects the result as booleans or numbers. The should interface extends Object.Prototype, so that all objects have access to it through the prototype chain. This also means that the should interface will not work in some scenarios, you can read more about that here .

Here are some common Chai assertions:

expect

let username = 'darth_sidious'

expect(username).to.be.a('string');
expect(username).to.equal('darth_sidious');
expect(username).to.have.lengthOf(13);

should

let username = 'darth_sidious'

username.should.be.a('string');
username.should.equal('darth_sidious');
username.should.have.lengthOf(13);

assert

let username = 'darth_sidious'

assert.typeOf(username, 'string');
assert.typeOf(username, 'string', 'username is a string'); //optional message
assert.equal(username, 'darth_sidious', 'username equals `darth_sidious`');
assert.lengthOf(username, 13, 'username value has a length of 13');

Note that the should interface runs on node.js and in all modern browsers except Internet Explorer. There are also some scenarios in which the use of the should interface will not work such as when you are trying to check the existence of an object. Learn more about that here.

That's all for now! I hope this beginner's article on Mocha and Chai testing was useful to you, if you liked it, will you give it a share on Twitter? ๐Ÿ’– Feel free to tag me on Twitter and I will repost. If you have feedback, leave me a comment~

P.S. Take a peek at my online community for women in tech called CyberWomxn and join our monthly hangouts โœจโ˜•๏ธ here.

ย