TDD with jest and typescript
Aaron Ross
Software Engineer / Web Developer @ EyeCue LabTesting software is essential and serves more functions than are immediately apparent. Good tests can ensure not only that a unit of code works as intended but, as an application grows, they provide a record of stability and when new developers come on to a project they provide a safety net against unintended side effects.
This article will walk through setting up test driven development in node using typescript and jest.
Get the code
completed example code for this article can be found here
#
Set Up#
Initialize a repositoryThere are 2 options, if you have a github account and if you do not, but for both we will create/clone the repository and then enter the directory and switch to the set-up
branch
#
Github UsersGo the node-jest-typescript-example repository and click on the
Use this template
button:Enter you repository name and description on the ensuing page:
make sure to check Include all branchesclone your repository to your local machine
#
Regular Git Workflow- start in the parent directory where you want to install the project and run the following to clone the repository into the directory
<your-project-name>
:
#
Package manager and DependenciesYou should now have a file structure similar to:
which are essential files that every public repository needs or will want. I won't explain them here, but if you aren't familiar there are lots of good resources out there.
The next steps are to initialize our package manager and install the necessary dependencies for this project. I will be using yarn as my package manager of choice.
- Create a
package.json
file
Run yarn init
at the root of the project. Which will ask you a series of questions with some values pre-populated from our existing LICENSE
file and create a package.json
file.
- Install Dependencies
most of this comes directly from the jest getting started guide. First we need to add the dependencies we need:
- Create a
babel.config.js
(jest requires babel to transpile ts code)
and paste in the following:
Finally, lets open up package.json
and add some new keys:
Which adds a script
to run our tests and a jest
key to tell jest what file extensions to load and where our node modules are stored. It is now time to commit you code and move on to the next step.
#
Write the test firstGet the code
If you haven't been coding along you can start here by checking out the add-tests branch from my repository.
Since we are using Test Driven Development for this project it is now time to write the fist test. Lets make some directories and files:
and then open up the .spec file and add a test:
but if we try and run it with yarn test
But that makes sense because we haven't actually written the add
function (or anything) in basicMath yet.
#
ImplementNow we can write our implementation of basicMath.add:
#
Praise the green checkNow we can finally run our test and see that it works with yarn test
in the base directory and we should get the output with our wonderful green check: