To do API automation testing with JavaScript, we’ll first need to know which libraries or frameworks we can use that’ll help build our test framework.
Since we are doing API testing, we’ll need some way to make HTTP calls programmatically and for that, we will be using the SuperTest npm package.
SuperTest
SuperTest is built on top of SuperAgent which basically allows you to make HTTP requests and SuperTest provides a high-level abstraction for testing those HTTP requests. Let’s take a look at an example:
const request = require('supertest'); request() .get('/user') // access user route .expect('Content-Type', /json/) // verify the content type .expect('Content-Length', '15') .expect(200) // verify the status code .end(function (err, res) { if (err) throw err; // any additional code });
You can easily add assertions by just chaining these expect commands. We can continue to write tests this way but we need a better way to be able to group tests or run individual tests and for that, we’ll need some kind of test framework.
Mocha JS
Mocha JS, is a really popular JavaScript test framework that runs on node js. You get a lot of features pre-build with the Mocha test framework –
- You can group your tests
- Skip your tests
- Use hooks to set up or tear down your tests
- It also comes with reporting as well as retry support along with many other useful features
Let’s take a look at basic Mocha JS example –
describe('Array', function () { // describe block allows you to group your tests describe('#indexOf()', function () { it('should return -1 when the value is not present', function () { // it block is where you will run your test // any verification here... }); }); });
Now, let’s take a look at how Mocha test would look like with SuperTest –
describe('GET /user', function () { it('responds with json', function (done) { request(app) .get('/user') .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200, done); }); });
If you notice, we took the SuperTest request
block and put that inside the it
block. In the same way, we can create multiple it
blocks for various test scenarios.
Now, for any test, we need to verify whether something works or not, and for that, we can use assertions.
Chai JS
Assertion library for node and browser that can be paired with any testing framework (in our case Mocha JS). Chai provides multiple interfaces which are basically different ways for you to write your assertions – so they have should
, expect
, and assert
. So whichever style you are comfortable with using, you can use that in your tests.
Let’s take a look at some Chai assertion example –
// Using chai.should(); foo.should.be.a('string'); foo.should.equal('bar'); // Using chai.expect expect(foo).to.be.a('string'); expect(foo).to.equal('bar'); // Using chai.assert assert.typeOf(foo, 'string'); assert.equal(foo, 'bar');
Now once we have added assertion, we can start writing tests with no issues. However, we need some way to see a report of all the tests we are running and see the pass / fail status of each.
So good thing is that Mocha comes with pre-built reporters which we can use that is the spec reporter
which gives this simple terminal results view.
It is sufficient enough when we are getting started but we can also implement a nice HTML reporter using mochawesome reporter.
Mochawesome
Mochawesome has a nice, modern look and comes with some good features –
- supports for test and suite nesting
- shows before and after hooks
- shows code along with each test which is great for debugging purposes.
Here’s how a sample Mochawesome report looks like –
So there you go using SuperTest, Mocha, Chai, and Mochawesome we can create an API test automation framework in JavaScript. Note: There are many other alternatives that can be used for these libraries or frameworks, however, for this tutorial series this is what we will be using.