How to start unit testing your Javascript projects - Pt.2

with Jest

As we saw in the first part of this series, we briefly introduced what jest is and how to set it up in a simple nodejs project. In this second part, we're going to level up our tests by testing other things than a simple string.

Testing arrays and Javascript objects

So imagine we want to test whether two arrays are equal ...

it("should check if arrays share the same elements", () => {
  const a = ["apple", "banana", "avocado", "orange"];
  const b = ["apple", "banana", "avocado", "orange"];

  expect(a).toEqual(b);
});

You must be wondering why we didn't use the same Jest method toBe() we used in the previous article to compare two strings and the reason why is because the toBe() method uses under the hood the javascript method Object.is() to determine whether or not two values share the same value. According to MDN, Object.is()can only be used to compare two javascript objects (arrays included) when those reference the same object in memory. Putting in simple words we can test whether the arrays a and b are equal with toBe() if they were previously created from a third array like c:

c = [1, 2, 3];

a = c;
b = c;
Object.is(a, b) // true

Otherwise we would need to use toEqual() to tests those arrays. The same rules applies to a Javascript object with its key values pairs.

Now imagine we want to test whether an array contain an element or not, one way we can do that is by doing so:

it("should test whether an array has an given element or not", () => {
  const fruits = ["apple", "banana", "avocado", "orange"];
  const hasApple = fruits.includes("apple");

  expect(hasApple).toBe(true);
});

or simply by using a jest method especially created for this situation:

it("should test whether an array has an given element or not", () => {
  const fruits = ["apple", "banana", "avocado", "orange"];

  expect(fruits).toContain("apple");
});

Testing numbers

To test numbers Jest hands us a set of useful methods like:

  • toBeGreaterThan()
  • toBeGreaterThanOrEqual()
  • toBeLessThan()
  • toBeLessThanOrEqual()

So imagine we want to test if a function does its job correctly:

function doublesNumber(n) {
  return n * 2;
}

it("should test doublesNumber function", () => {
  const number = doublesNumber(2);

  expect(number).toBeGreaterThan(2);
  expect(number).toBeGreaterThanOrEqual(4);
  expect(number).toBeLessThan(5);
  expect(number).toBeLessThanOrEqual(7);
});

The intresting part about testing numbers is that we can use the methodstoBe() and toEqual() interchangeably.

test("should test doublesNumber function", () => {
  const n = 10;

  expect(n).toBe(10);
  expect(n).toEqual(10);
});

Testing exceptions

Firstly, what is an exception? Exceptions are objects created and thrown by developers when errors occurs. In web development we generally use exceptions when an error raises during an API call or an database connection for example. We can simply test an exception by:

function throwError() {
  throw new Error('Something went wrong');
}

test("should test thrown function", () => {
   expect(() => throwError()).toThrow();
   expect(() => throwError()).toThrow('Something went wrong');
});

All right guys, with this second part we only scratched the surface, stay tuned for next part of this series.

Cheers!