UNPKG

6.79 kBPlain TextView Raw
1import * as test from 'tape';
2import * as request from 'supertest';
3import {response} from './tools';
4
5export interface ITestApi {
6 handler?: any;
7 description?: string;
8 context?: string;
9 route?: string;
10 execute?: any; // promise to execute
11 method?: string;
12 send?: any;
13 expectedCode?: number;
14 contentType?: string;
15 showResults?: boolean;
16 expected?: any;
17 emptyResultExpected?: boolean;
18 failureExpected?: boolean;
19 compared?: any;
20 comparison?: string;
21}
22
23export class TestApi {
24 private app: any;
25
26 constructor(app) {
27 this.app = app;
28 process.setMaxListeners(Infinity);
29 }
30
31 test(params: Array<ITestApi>) {
32 for (let i = 0; i < params.length; i++) {
33 test(`${i + 1}) ${params[i].description}`, t => {
34 this.__test(t, params[i]);
35 });
36 }
37 }
38
39 __test(t: any, params: ITestApi) {
40 const app = this.app;
41 // double Promises to avoid unhandled promises
42 return new Promise((_res, _rej) => {
43 return new Promise((__res, __rej) => {
44 try {
45 // do we have a promise to execute instead of a REST API request?
46 if (params.execute && params.execute.then) {
47 return params.execute
48 .then(success => {
49 if (params.showResults && success.success !== false) {
50 console.log('[showResults: ON]:', success);
51 }else if(params.showResults && success.success === false) {
52 __rej(success);
53 t.error(true, success);
54 t.end();
55 return;
56 }
57 __res(true);
58 _res(true);
59 t.error(false, 'No error');
60 t.end();
61 })
62 .catch(err => {
63 __rej(err);
64 t.error(true, err);
65 t.end();
66 });
67 }
68
69 const expectedCode = params.expectedCode || 200,
70 send = params.send || null,
71 contentType = params.contentType || /json/,
72 showResults = (params.showResults === undefined) ? true : params.showResults;
73
74 let route = params.route.toString().trim(), method;
75
76 if (route.indexOf(' ') > -1) {
77 [method, route] = params.route.split(' ');
78 } else {
79 route = params.route;
80 method = (params.method === undefined) ? 'get' : params.method.toString().toLowerCase();
81 }
82
83 const r = request(app)[method](route);
84
85 if (send) {
86 r.send(send);
87 }
88
89 r.expect('Content-Type', contentType);
90 r.expect(expectedCode);
91 r.end((err, res) => {
92 if (err) {
93 t.error(true, res.body.error || err);
94 __rej(err.message);
95 } else {
96 if (showResults) {
97 console.log('[showResults: ON]:', res.body);
98 }
99
100 // compared is null, it means it is expecting body.data
101 if (params.expected && !params.compared) {
102 console.log('comparing with body.data');
103 params.compared = res.body.data;
104 }
105
106 if (params.compared && params.expected && params.expected.length) {
107 const expected = {};
108 for (let i = 0; i < params.expected.length; i++) {
109 expected[params.expected[i]] = res.body.data[params.expected[i]];
110 }
111
112 t.same(params.compared, expected, `'${params.context}' info matches as expected`);
113 } else {
114 if ((!res.body.data && !res.body.swagger) ||
115 (Array.isArray(res.body.data) && !res.body.data.length)) {
116 if (!res.body.error && (params.emptyResultExpected || params.failureExpected || send)) {
117 t.error(false, 'No error');
118 } else {
119 if (!res.body.success && params.emptyResultExpected) {
120 t.error(false, 'No error');
121 } else {
122 t.error(true, '[114] Empty array returned');
123 }
124 }
125 } else {
126 if (res.body.swagger || res.body.success || params.failureExpected) {
127 // if (res.body.data && res.body.data.Items && !res.body.data.Items.length
128 if ((res.body.data && Array.isArray(res.body.data) && !res.body.data.length) || !res.body.data
129 && !params.emptyResultExpected) {
130 t.error(true, '[120] Empty array/value returned');
131 } else {
132 t.error(false, 'No error');
133 }
134
135 } else {
136 t.error(true, res.body.error);
137 }
138 }
139 }
140 __res(true);
141 _res(res.body);
142 }
143 t.end();
144 });
145 } catch (error) {
146 console.log('138 tools.test.__test()', error);
147 __rej(response(false, error.message));
148 }
149 })
150 .catch(errorResponse => {
151 console.log('143 tools.test.__test()', errorResponse);
152 _res(errorResponse);
153 });
154 });
155 }
156}
\No newline at end of file