Chai Assert!
This week I have decided to deep dive into the Chai assert interface. If you read last weeks blog post you saw a few examples of assert (assert.equal(), and assert.typeOf()), but we only scratched the surface of what you can do with assert! In this blog post we will take a look at some of the most useful methods thats come with Chai assert. Here is the function that we will be testing in our examples…
const addNumbers = (a, b) => a + b
as you can see it’s a function called addNumbers that takes in two arguments (a, b)returns the value of the two.
1. assert.strictEqual(actual, expected,[message])
This method takes in 3 arguments. The first argument being the problem you are asking, the second being the expected results, and the third being a message of what you are expecting. Lets test addNumbers() using the assert.equal() method!
describe('addNumbers()', () => { it('should add both arguments and return the value', () => { let result = addNumbers(1, 5) assert.strictEqual(result, 6, 'should equal 6') })
})
Here is what we get when we run npm run test
addNumbers()
✔ should add both arguments and return the value
Great! So we see that our function is working as expected! assert.strict equal() is checking if the result and the expected result are exactly equal to each other and in this case they are!
2. assert.typeOf(value, name, [message])
It is always important to check for bad input when programming and type of helps us do just that. It will take in a value as the first argument, the second will be the name of the data type it should be, and the third a message. Lets make sure that our function is returning a number…
describe('addNumbers()', () => {it('should add both arguments and return the value', () => {let result = addNumbers(1, 5)assert.equal(result, 6)})it('add numbers should return type number', () => {let result = addNumbers(5, 5)assert.typeOf(result, 'number', 'should be true')})})
Nice! This one passes! 5 + 5 of course will return the number 10
addNumbers()
✔ should add both arguments and return the value
✔ add numbers should return type number
3. assert.notTypeOf(value, name, [message])
This one is exactly the opposite of the assert.typeOf() method. It makes sure that the input is not the data type that the function should be expecting to return. As I said earlier its always important to check if we are getting the correct data types. Lets test this on out with the same input as our last test..
describe('addNumbers()', () => {it('should add both arguments and return the value', () => {let result = addNumbers(1, 5)assert.equal(result, 6)})it('add numbers should return type number', () => {let result = addNumbers(5, 5)assert.typeOf(result, 'number', 'should be true')})it('add numbers should not return type string', () => {let result = addNumbers(5, 5)assert.notTypeOf(result, 'string', 'numbers are not strings')})})
Here are the results
addNumbers()
✔ should add both arguments and return the value
✔ add numbers should return type number
✔ add numbers should not return type string
Great our function is passing all the tests that we are throwing at it!
Summary
There are many more methods that come with Chai assert check them all out at https://www.chaijs.com/api/assert/. I hope this was helpful in your journey to becoming a TDD! Join me in the fight against faulty code!