如何使用Ava和Test-Listen测试Micro EndPoints

时间:2018-04-19 12:23:01

标签: javascript unit-testing ava micro

让我说我有这个routes.js in micro:

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

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

我有以下测试设置,但不幸的是我得到了undefined而不是Hello。我有什么想法吗?

'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 个答案:

答案 0 :(得分:0)

我认为它可能只是t.is(res.body, 'Hello!')