DEV Community

Cover image for Babel Setup for REST API Tests
Dmitrii Bormotov
Dmitrii Bormotov

Posted on

Babel Setup for REST API Tests

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:

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 World Wide Web, 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

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:

Don't forget to save changes.


ES5 to ES6+

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

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;
Enter fullscreen mode Exit fullscreen mode

To this:

import axios from 'axios';
import { assert } from 'chai';
Enter fullscreen mode Exit fullscreen mode

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

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

Top comments (0)