Learn Assert by building a javascript testing library
Tags: dev, code, javascript, showdev
Published: 2018-06-28 06:02:32

---
title: Learn Assert by building a javascript testing library
published: true
tags: dev,code,javascript, showdev
canonical_url: 
---

#### Building interesting projects with nodejs standard lib.

![](https://cdn-images-1.medium.com/max/1024/1*8YLBRkeWl4lvbFWmcR2pqg.png)

One way you can learn about node js is by building stuff. A lot of developers say they know Nodejs because it is just javascript on the server side, but many of them don’t know how to use the nodejs standard library. Lets start by working with the assert library and build a simple testing library.

A testing library is a piece of software that helps us test out our code to ensure that it is working correctly as intended. An example of a testing library is [Jest](https://facebook.github.io/jest/) by Facebook. We are going to borrow the structure of how jest tests are written.

Starting with the simplest examples. Assuming that we have a piece of code that sums two numbers.

{% gist https://gist.github.com/sadick254/f370897b9253d0074a4f173d4dcfb504 %}

How will the intended testing code be.

{% gist https://gist.github.com/sadick254/bc7a4cba0318becad4b1cf01c54d8049 %}

#### Laying out the structure of the testing library.

{% gist https://gist.github.com/sadick254/0b17e406637d84a89763d5e500107ae2 %}

As you can see from the structure there is nothing fancy, just functions. We are going to use assert in the expect function. Assert is used for assertion.

We want to be able to make assertions like expect(4).toEqual(10) and we are going to make use of the assert lib available in the node js standard library.

{% gist https://gist.github.com/sadick254/86dd9afa540213a74eb6badfbebd7a48 %}

Lets look at the assert functionalities we have used in the expect function

**assert.ok** :- Asserts that any expression passed to ok evaluates to a truthy value failure to which an error will be thrown.

**assert.equal**  :- Asserts that two values are equal, if not an error will be thrown

**assert.deepStrictEqual** :- Asserts that two objects, or their child objects, are equal, an error is thrown if they are

We need a way to show error and success states when tests pass or fail. We will use just a simple try catch block in the it function.

{% gist https://gist.github.com/sadick254/1ddc60bdf3fbdc8acefa1f9ff6d5d48a %}

Here is the complete source code.

{% gist https://gist.github.com/sadick254/79ab060a64b099b20a37775746b90fe1 %}

Am a strong believer that you learn by doing. I have left out a couple of things that you can use to practice. There are many things you could do to make it better and here are two things to get you started.

- Color the output of the tests (passing — green, failing red)
- If the test fail, show values of the expected and actual values passed.

Take your time to implement the two features and when you are done, hit me up on twitter for review. And also if you are stuck you can reach out to me I will be happy to assist.

_If you found this post helpful, please give a clap and share it so that others can find it. You can follow me on_ [_GitHub_](https://github.com/sadick254) _and_ [_LinkedIn_](https://www.linkedin.com/in/isaac-odhiambo-678b1547/)_. If you have any ideas or improvements feel free to share them with me._

[![](https://cdn-images-1.medium.com/max/700/1*PZjwR1Nbluff5IMI6Y1T6g@2x.png)](https://goo.gl/w4Pbea)

* * *