How to do it...

Let's follow the given steps to create an API integration test for our Express web server's /api endpoint:

  1. First, we will create a new test file, called /test/integration/test-route-api.ts. This new integration folder helps us keep our tests organized logically in terms of their level of abstraction, while the naming convention reinforces the relationship of the tested content to the project's source code:
import { suite, test } from 'mocha-typescript';
import * as chai from 'chai';
const expect = chai.expect;

@suite class API {

}
  1. Before we can start writing integration tests, we need to instantiate our application to the state that we will need in order to run the test. For this API test, we will need to configure our application to start up and run at a predetermined host and port. We'll configure our server in a before method. It's important that this method is declared as a static method so that mocha-typescript will only invoke it once before all our test contexts run. Similarly, we'll declare an after method to close down our server when all our tests have finished running:
import { suite, test } from 'mocha-typescript';
import { Server } from "http";
import * as chai from 'chai';
const expect = chai.expect;

import app from '../../src/App';

@suite class API {
private static port:number = 3000;
private static host:string = 'http://localhost';
private static connection:string;
private static server:Server;

public static before(done:MochaDone) {

this.connection = this.host + ":" + this.port;
this.server = app.listen(this.port, done);
}


public static after() {
this.server.close();
}

}
  1. We're now ready to write our first test; a simple status check of our /api REST API endpoint. We will need to import chai-http in order to connect to our API to assess its behavior. This is as simple as providing the location of where our web server is running, and using the built-in get method to make a request to our endpoint. Then, we can simply evaluate the response using Chai's assertion library:
import { suite, test } from 'mocha-typescript';
import {Server} from "http";
import * as chai from 'chai';
const expect = chai.expect;

import app from '../../src/App';
import chaiHttp = require('chai-http');
chai.use(chaiHttp)
;

@suite class API {
...

@test("responds with status 200")
public status200(done:MochaDone) {
chai.request(API.connection)
.get('/api')
.end(function(err, res) {
expect(res).to.have.status(200);
expect(res.text).to.equal('API is running');
done();
});
}

}
  1. This particular endpoint doesn't do much; it only allows GET requests. So, we can write two different error tests to assert that POST requests will be served either a 415 or a 404, depending on the POST request's Content-Type header. With these tests, it is easy to see how we can quickly and easily set up a new request context in a new test without having to restart or tear down our application context again:
...

@suite class API {
...

@test("responds to POST with status 415")
public status415(done: MochaDone) {
chai.request(API.connection)
.post('/api')
.end(function (err, res) {
expect(res).to.have.status(415);
done();
});
}

@test("responds to JSON POST with status 404")
public status420(done: MochaDone) {
chai.request(API.connection)
.post('/api')
.set('Content-Type', 'application/json')
.end(function (err, res) {
expect(res).to.have.status(404);
done();
});
}
}
  1. To run our integration test, we simply need to call our npm test script, and our test will run along with our existing unit tests:
npm test
..................Content has been hidden....................

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