If you are a developer with a bit of experience with Typescript and unit test, this post is for you.

The test-driven programming approach has become many successful developers’ essential building block to deliver high-quality code. Mocha and Chai are a prevalent testing tool in the Javascript/ Node.js domain in the last few years. Being a big fan of it, I have delivered numerous successful projects with it.

I was happy with Mocha and Chai until I started to use Typescript. In Typescripts’ setup with different ECMAScript or Babel versions, you may need various hacks to get it to work. Tooling is vital as other developers wouldn’t have frustration and obstacle because of the fragile and hacky solution that might be broken somewhere at a point in the future when tools and libraries are updated.

Jest is a better solution as it can test both Typescript and Javascript in a straightforward config. Let’s see how it works.

We’re gonna create the simplest setup of Typescript and Jest. In a blank new project, initiate with

$ yarn init -y

Then install typescriptjest@types/jestts-jest as development dependencies.

$ yarn add -D typescript jest @types/jest ts-jest

Cool. Let’s have the bare minimal config.

First, generate typescript config with tsc.

$ tsc --init 

Second, generate Jest config with ts-jest.

$ yarn ts-jest config:init

There you go! You’re ready to write the test. Straightforward enough?

#typescript #unittest #jest #chai #mocha

Jest With Typescript
1.55 GEEK