HowTo test Micro EndPoints with Ava and Test-Listen

100 Views Asked by At

Let's say I have this routes.js in micro:

const hello = async (req, res) => {
    send(res, 200, `Hello, ${req.params.who}`);
});

module.exports = router(
    post('/hello/:who', hello),
);

I have the following test set-up, but unfortunately I am getting undefined instead of Hello. Any ideas what I am doing wrong?

'use strict';

const listen = require('test-listen');
const micro = require('micro');
const test = require('ava');
const got = require('got');

require('async-to-gen/register')({includes: /index\.js$/});
const app = require('./'); // eslint-disable-line import/order

test('echo back the text', async t => {
    const service = micro(app);
    const url = await listen(service);

    const res = await got(url, {
        method: 'post',
        json: true,
        headers: {'content-type': 'application/json'},
        body: JSON.stringify({text: 'Hello!'})
    });

    t.is(res.body.text, 'Hello!');
});
1

There are 1 best solutions below

0
On

I think it might just be t.is(res.body, 'Hello!').