Unit Testing JavaScript

Unit Testing JavaScript

I find the best way to learn things is by practice. This is probably true for most people so I wanted to share an easy set up for practicing JavaScript problems locally.

We'll do this by setting up Node in a Unix environment and then relying on the node packages called Mocha and Should.

  1. Installing NVM

The easiest way to set up Node in my experience is by using NVM (Node Version Manager). Luckily there is an easy to use install  bash script if you are working with Unix based system like macOS or Linux. If you are using Windows 10 I recommend working with WSL (Windows Subsystem for Linux). Once you have a Unix environment the command for installing NVM is as simple as this:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash

After installing you can restart the terminal or follow the prompt instructions in order to activate it in the current shell. Either way you choose, you can verify it installed correctly by running this command:

nvm -v

2. Installing Node

With NVM now installed, we can use it to easily install the latest Long Term Support release of Node. To do that we run the following command:

nvm install --lts

Once that finishes, you can verify both Node and NPM have been installed using these two commands

node -v
npm -v

You can also see where they installed by running:

which node
which npm

You'll see it installed in a .nvm folder where we can have multiple version of Node. This is useful when different project depend on different versions.