DEV Community

Discussion on: How do you test your REST API?

Collapse
 
jhechtf profile image
Jim Burbridge

Tests

Well, I use Fastify most times for APIs and it has a nice method called inject, which is mostly just used for testing. The setup ends up looking something like

// server.js
const Fastify = require('fastify');
const app = Fastify();

app.get('/some-route', async () => "data");

app.listen(3000, err =>  {
  if(err) { console.error(err); process.exit(); }
  console.log('listening');
});

module.exports = app;

// main.test.js
const test = require('ava');
const app = require('../server.js');

test('API Testing', async t => {
   await app.inject({ url: '/some-route' })
     .then(res => { t.is(res.statusCode, 200); })
     .catch(err => t.fail());
   });
});

(note: roughed most of this from memory so it might not be 100% runnable).

Otherwise

Most people use something like Postman. I've switched over to the Insomnia REST client.