Babel Setup for REST API Tests
Tags: qa, rest, testing, babel
Published: 2021-01-28 20:15:05

Ahoy, mate!

This topic may be useful for beginners in Test Automation or those testers who works with other languages (_like Java or Python_) and new to **JavaScript** (_Node.js_). It also expands my previous post:
{% link https://dev.to/bormando/minimal-rest-api-tests-in-node-js-2jde %}

**Babel** is a tool that convert **ES5** to modern versions (_ES6+_) of code.

**ES** (_ECMAScript_) is a **JavaScript** standard meant to ensure the interoperability of web pages across different web browsers. **ECMAScript** is commonly used for client-side scripting on the **W**orld **W**ide **W**eb, and it is increasingly being used for writing server applications and services using **Node.js**. By default, **Node.js** uses **ES5**, which was released a long time ago, in 2009.

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

-----

#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 Babel dependencies

These are standard **Babel** dependencies that we'll need to download:
* `@babel/cli`
* `@babel/core`
* `@babel/plugin-transform-runtime`
* `@babel/preset-env`
* `@babel/register`

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

`npm i -D @babel/cli @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/register`

-----

#Babel configuration

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

Simply create `.babelrc` file in root directory of our project and add this data to it:

{% gist https://gist.github.com/bormando/85fb6a5f8d9de5f9ee47d909902a6c33 %}

Don't forget to save changes.

-----

#ES5 to ES6+

You may read about most of the changes between **ES5** and **ES6+** with examples here:

{% medium https://muthuks.medium.com/difference-between-es-5-and-es-6-e993c7ab0a70 %}

In our current project, all you'll have to do is to change imports in `markets.test.js` 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 **Babel** 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 **ES6+** code to **ES5** on-run, you need to change this shortcut on something like this:

`"test": "npx mocha --require @babel/register src/specs"`

`--require @babel/register` works as a converter in this case.

-----

#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.