UNPKG

820 BJavaScriptView Raw
1'use strict';
2
3const supertest = require('supertest');
4const http = require('http');
5
6
7const apiTestUtil = {
8
9 _initPromise: false,
10
11 _app: null,
12
13 before (mocha, appPromise) {
14 this._initialize(mocha, appPromise);
15 },
16
17 request () {
18 if (!this._initialized) {
19 throw new Error('Mocha should be initialized!');
20 }
21 return supertest.agent(this._app);
22 },
23
24 _initialize (mocha, appPromise) {
25 if (!this._initPromise) {
26 this._initPromise = appPromise;
27 }
28 mocha.before((done) => {
29 this._initPromise.then((app) => {
30 this._app = http.createServer(app.callback());
31 this._initialized = true;
32 done();
33 }, done);
34 });
35 }
36};
37
38module.exports = apiTestUtil;