How to chain http calls with superagent/supertest?

后端 未结 4 1839
一个人的身影
一个人的身影 2021-02-01 13:28

I am testing an express API with supertest.

I couldn\'t get multiple requests in a test case to work with supertest. Below is what i tried in a test case. But the test c

4条回答
  •  無奈伤痛
    2021-02-01 14:01

    I built upon Tim’s reply but used async.waterfall instead, to be able to do assert tests on the results (note: I use Tape here instead of Mocha):

    test('Test the entire API', function (assert) {
        const app = require('../app/app');
        async.waterfall([
                (cb) => request(app).get('/api/accounts').expect(200, cb),
                (results, cb) => { assert.ok(results.body.length, 'Returned accounts list'); cb(null, results); },
                (results, cb) => { assert.ok(results.body[0].reference, 'account #0 has reference'); cb(null, results); },
                (results, cb) => request(app).get('/api/plans').expect(200, cb),
                (results, cb) => request(app).get('/api/services').expect(200, cb),
                (results, cb) => request(app).get('/api/users').expect(200, cb),
            ],
            (err, results) => {
                app.closeDatabase();
                assert.end();
            }
        );
    });
    

提交回复
热议问题