TypeScript Setup for REST API Tests
Tags: typescript, testing, api, qa
Published: 2022-11-13 18:21:16

Ahoy, mate!

This topic may be useful for test automation engineers who just started to learn **TypeScript**. It also extends my previous post:
{% link https://dev.to/bormando/minimal-rest-api-tests-in-node-js-2jde %}

**TypeScript** is a **JavaScript** superset in **Node.js**, which brings static types and other useful features to your project.

Under the hood, it transforms the code you written to the one that your platform understands (_ES5 standard_).

Source code of project from this article on **GitHub**: https://github.com/bormando/mochapi/tree/future

-----

#Setup

You may clone a **GitHub** repo with few example tests from here: https://github.com/bormando/mochapi/tree/main

After cloning, you must execute this command to download all required dependencies:

`npm i`

To check, if cloned project is working, you may run:

`npm run test`

It should display something like this in the end:

![Code_tRvTtwQQkA](https://dev-to-uploads.s3.amazonaws.com/i/to92ymjxwvfowdfyumr1.png)

-----

#Install TypeScript dependencies

These are standard **TypeScript** dependencies that we'll need to download:
* `typescript`
* `ts-node` (_to transform code from TypeScript to EcmaScript 5 standard on run_)

Also, keep in mind that some **Node.js** packages like `mocha` & `chai` doesn't come with **TypeScript**, so you'll have to install them separately as well:
* `@types/mocha`
* `@types/chai`

To install them all, you'll need to execute in terminal:

`npm i -D typescript ts-node @types/mocha @types/chai`

-----

#TypeScript configuration

Now, as we've finished dependencies installation, we need to configure **TypeScript**.

Simply execute `npx tsc --init` in your project root which will create `tsconfig.json`.

I recommend these options for new projects:

{% gist https://gist.github.com/bormando/11d3783232988b88b8f580969dbeb991 %}

Don't forget to save changes.

-----

#ES5 vs TypeScript

You may read about base differences between **ES5** and **TypeScript** here:

{% medium https://medium.com/geekculture/typescript-vs-javascript-e5af7ab5a331 %}

In our current project, all you'll have to do is:

1. Rename `markets.test.js` to `markets.test.ts`.
2. Change imports in `markets.test.ts` file...

From this:

```
const axios = require('axios');
const assert = require('chai').assert;
```

To this:

```
import axios from 'axios';
import { assert } from 'chai';
```

These are the small changes but this is all we need to make sure that our **TypeScript** setup works fine (since this is the point of current article).

-----

#Reconfigure shortcut

Shortcut for running **Mocha** tests in **ES5** looks like this:

`"test": "npx mocha src/specs"`

You may find it in `scripts` section inside of `package.json` file.

To convert code **TypeScript** code to **JavaScript** on-run, you need to change this shortcut on something like this:

`"test": "npx mocha --require ts-node/register src/specs/**/*.test.ts"`

`ts-node/register` works as a converter in this case.

Also notice `src/specs/**/*.test.ts` in the end - now we have to specify test files pattern, as **Mocha** looks for `.js` files by default.

-----

#Running tests

We're good to go by now, so all you have to do is execute in terminal:

`npm test`

or

`npm run test`

And then you must see report in your terminal output:

![Code_tRvTtwQQkA](https://dev-to-uploads.s3.amazonaws.com/i/to92ymjxwvfowdfyumr1.png)

Thanks for reading, hope you've learned something new.