Testing with Mocha - Part 1

Introduction

This journal entry was written by me, Brandon Nolet, talking about my experience while I code some tests for my software project genera(develop branch). This will be an “ongoing series” but not like the patching series that I just finished. The posts will be much more spread out.

Mocha

Mocha is a feature-rich JavaScript test framework running on Node.js I chose this testing suite because I felt like it would be the least friction to get it up and uses a lot of the built-in features of Node itself. See the Mocha documentation here.

Testing

Basically the concept is you write test cases that will either pass or fail. In the way that I’m writing my server, I write the code, write tests based on what I expect the code to do and then run those tests.

If the tests pass, then all is good. If the tests fail then, at this point, it’s because the test isn’t properly written. This is what’s happening.

Chai

So while Mocha provides the framework for the tests, the assertions are truly where it’s at. I’m using the Chai assertion library. Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.. See the here.

With Chai, you want to tell the test suite what you expect to be the result of the test. If the test is to check whether or not a given request returns a given value, then you’re telling the test suite that. Example of an assertion:


chai.request(app)
  .get('/tasks/query?inbox=true')
  .end(function(err, res) {
    res.body.should.be.a('array')
    res.body.should.have.lengthOf(2)
    })

In this assertion I say that I want Chai to make a GET request to the server at the address /tasks/query?inbox=true and that I expect the response to be an array with a length of 2.

Style

At the moment, I’m really not sure what sort of style I should be using when writing mocha code. I switch between using assert() and using the should styles and it seems to be fine. The should style is more like the example above, whereas the assert() style is more like this:


chai.request(app)
  .get(`/tasks/${res.body[0]._id}`)
  .end(function(error,response) {
    assert.equal(response.body.task, "Mow the lawn")
    response.body.should.be.a('object')
    })

In this snippet I’m making a GET request to the server at an address that corresponds to /tasks/ plus the _id property of the first object within the array res.body. res corresponds to the response of a previous GET request. As you can see I mixed the styles and it’s more of a functional decision that I end up making.

The one thing I’m having trouble dealing with, and this is more of a personal preference I think, is having crash-type messages when one or more tests fail. I would prefer messages simply be something along the lines of “The body wasn’t an object” rather than UnhandledPromiseRejectionWarning: AssertionError: expected.....

That can be done, but I just have to learn how to do it.

Frustration

Seeing as I’m, firstly, still learning node, and secondly, still learning Mocha+Chai (which use NodeJS as the language), I find myself putzing about trying to get the tests to even work properly in the first place. It’s almost like trying to use the ladder you’re in the process of building to build a house.

I still don’t have a complete handle on how to use a Promise or how asynchronous code works. Eventually I’ll become more comfortable and familiar with these concepts but I don’t know how long that will take.

Conclusion

Even though the server has a lot of effort put into it and almost works as desired, there’s still a lot of work to be done. genera/generalist has a long way to go and I feel like I’ve only just touched the surface of what I’m meant to learn here.