Configuring applications for integration testing

The only prerequisite you need is the Node runtime environment running correctly. In JavaScript, we have some tools to make integration tests, such as the following:

  • SuperTest: The best feature is its strong documentation; easy to understand and implement, you just need to write a few lines of code to start testing your application.
  • Mocha: A simple JavaScript test framework. It can be executed in the web browser or in your Node environment. Since Mocha is based on JavaScript, it can execute asynchronous tests and generate very useful reports.

We will use both tools together for our testing purposes.

With SuperTest, you gain advantages such as the following:

  • You can simulate a multiple user interaction, storing different credentials (tokens) to switch between users.
  • You don't need to worry about delete or add mock data. SuperTest will perform the operations to clean or add data to your store.
  • The most useful feature—all these tests can be automated and integrated in your CI pipeline.

SuperTest is a big help for productivity; also, it offers a natural way to write and test your code at the same time; it is very intuitive and human readable. Let’s go over how to quickly set it up for something like user retrieving data.

Where can you run SuperTest tests? Basically, you can run them in any server you want. No matter whether you are deploying on local dev servers or cloud providers, SuperTests can be executed from any of them, but you must know something—SuperTest includes its own express server. This server should not be running all the time, but using some external tools like nodemon, you can automatically restart your server each time we have a change and need to test it. If you don't want to run all the tests, Mocha's only specifier is a nice solution too.

First, we need to download our dependencies:

npm i supertest mocha chai -s

Chai allows us to choose any of the following prefixes: should, expect, or assert. Just like other test tools, they are all available here too.

We already have our server.js file, so you won't need to add any code there. Yes, you don't need to run any server, and this is the most beautiful advantage of SuperTest!

Remember that our test files should be separated from the application files. Next, create your tests file with the touch tests.spec.js command, and let's add some code:

const app = require('./server');
const chai = require('chai');
const request = require('supertest');

var expect = chai.expect;

describe('API Tests', () => {
  it('should return football teams', function(done) {
    request(app)
      .get('/teams')
      .end(function(err, res) {
        expect(res.statusCode).to.equal(200);
        done();
      });
  });
});

Let's explain what the code is doing.

We are importing our server, Chai and SuperTest. SuperTest includes its own .expect(), but we are using Chai’s syntax. The code sets a group of API Tests and creates one test to check whether the /teams endpoint returns 200 (OK) as status code. Note that the done() function is important to declare our asynchronous tests are complete. Of course, this is a very high-level test, and we can add more assertions such as evaluating the response's content and more. For example purposes, this is very simple to understand and know how SuperTest works.

Now, let’s see if it works. Run the following command:

npm test

You should get this:

> npm test

> [email protected] test /Projects/worldcup-app
> mocha '**/*.spec.js'

  API tests
     should return football teams

  1 passing (41ms)
Make sure you have correctly configured the test command in the script section on package.json file.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.212.54