Skip to content

Commit 58bfa7e

Browse files
authored
Tests - use jest instead of mocha (#186)
* Use jest instead of mocha Resolve some security alerts from dependabot $ npm t > @automattic/request-promise-native@2.5.3 test > jest RUNS deps/promise-core/test/spec/request2.js RUNS deps/promise-core/test/spec/plumbing.js RUNS test/spec/request-test.js * PASS test/spec/request-test.js * PASS deps/promise-core/test/spec/plumbing.js * npm install --save-dev @jest/globals * npm remove chai * deps/promise-core/test/spec/request2.js - work in progress * Allow test case to run in parallel - use ports 4000 and 4001 for test servers * Make linter happy - jest seems to be auto-included * jest: do not inject globals - let the test code require the "jest" global * deps/promise-core/test/spec/request2.js: call jest.resetModules() before each call to stealthyRequire()
1 parent 98f9cb1 commit 58bfa7e

File tree

8 files changed

+4641
-1191
lines changed

8 files changed

+4641
-1191
lines changed

deps/promise-core/test/spec/plumbing.js

Lines changed: 122 additions & 121 deletions
Large diffs are not rendered by default.

deps/promise-core/test/spec/request2.js

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
var Bluebird = require('bluebird'),
1+
const Bluebird = require('bluebird'),
22
configure = require('../../configure/request2.js'),
3-
errors = require('../../errors'),
43
stealthyRequire = require('stealthy-require'),
5-
startServer = require('../fixtures/server.js'),
6-
expect = require('chai').expect;
4+
startServer = require('../fixtures/server.js');
75

6+
const { afterAll, afterEach, beforeAll, describe, expect, it, jest } = require('@jest/globals');
87

98
describe('Promise-Core for Request@2', function () {
109

1110
describe('during configuration', function () {
1211

12+
afterEach(function () {
13+
jest.resetModules();
14+
});
15+
1316
it('should verify the options', function () {
1417

1518
var request = stealthyRequire(require.cache, function () {
@@ -18,66 +21,68 @@ describe('Promise-Core for Request@2', function () {
1821

1922
expect(function () {
2023
configure();
21-
}).to.throw('Please verify options');
24+
}).toThrow('Please verify options');
2225

2326
expect(function () {
2427
configure('not an object');
25-
}).to.throw('Please verify options');
28+
}).toThrow('Please verify options');
2629

2730
expect(function () {
2831
configure({});
29-
}).to.throw('Please verify options.request');
32+
}).toThrow('Please verify options.request');
3033

3134
expect(function () {
3235
configure({
3336
request: 'not a function'
3437
});
35-
}).to.throw('Please verify options.request');
38+
}).toThrow('Please verify options.request');
3639

3740
expect(function () {
3841
configure({
3942
request: request
4043
});
41-
}).to.throw('Please verify options.expose');
44+
}).toThrow('Please verify options.expose');
4245

4346
expect(function () {
4447
configure({
4548
request: request,
4649
expose: 'not an array'
4750
});
48-
}).to.throw('Please verify options.expose');
51+
}).toThrow('Please verify options.expose');
4952

5053
expect(function () {
5154
configure({
5255
request: request,
5356
expose: []
5457
});
55-
}).to.throw('Please verify options.expose');
58+
}).toThrow('Please verify options.expose');
5659

5760
expect(function () {
5861
configure({
5962
request: request,
6063
expose: ['then', 'promise']
6164
});
62-
}).to.throw('Please verify options.PromiseImpl');
65+
}).toThrow('Please verify options.PromiseImpl');
6366

6467
expect(function () {
6568
configure({
6669
request: request,
6770
expose: ['then', 'promise'],
6871
PromiseImpl: 'not a function'
6972
});
70-
}).to.throw('Please verify options.PromiseImpl');
73+
}).toThrow('Please verify options.PromiseImpl');
7174

7275
expect(function () {
7376
configure({
7477
request: request,
7578
expose: ['then', 'promise'],
7679
PromiseImpl: Bluebird
7780
});
78-
}).not.to.throw();
81+
}).not.toThrow();
82+
});
7983

80-
request = stealthyRequire(require.cache, function () {
84+
it('validates custom promises implementation', function () {
85+
const request = stealthyRequire(require.cache, function () {
8186
return require('request');
8287
});
8388

@@ -87,7 +92,7 @@ describe('Promise-Core for Request@2', function () {
8792
expose: ['promise'],
8893
PromiseImpl: Bluebird
8994
});
90-
}).to.throw('Please expose "then"');
95+
}).toThrow('Please expose "then"');
9196

9297
});
9398

@@ -110,7 +115,7 @@ describe('Promise-Core for Request@2', function () {
110115

111116
return request('http://localhost:4000') // not started yet so expecting ECONNREFUSED
112117
.catch(function () {
113-
expect(mixinCalled).to.eql(true);
118+
expect(mixinCalled).toEqual(true);
114119
});
115120

116121
});
@@ -129,15 +134,20 @@ describe('Promise-Core for Request@2', function () {
129134
*/
130135
describe('doing requests', function () {
131136

132-
var request = null, stopServer = null;
137+
var request = null, stopServer = null, freshErrors = null;
138+
139+
beforeAll(function (done) {
133140

134-
before(function (done) {
141+
jest.resetModules();
135142

136143
request = stealthyRequire(require.cache, function () {
137144
return require('request');
138145
});
139146

140-
configure({
147+
var configureFn = require('../../configure/request2.js');
148+
freshErrors = require('../../errors');
149+
150+
configureFn({
141151
request: request,
142152
PromiseImpl: Bluebird,
143153
expose: [
@@ -155,7 +165,7 @@ describe('Promise-Core for Request@2', function () {
155165

156166
});
157167

158-
after(function (done) {
168+
afterAll(function (done) {
159169

160170
stopServer(done);
161171

@@ -165,7 +175,7 @@ describe('Promise-Core for Request@2', function () {
165175

166176
request('http://localhost:4000/200')
167177
.then(function (body) {
168-
expect(body).to.eql('GET /200');
178+
expect(body).toEqual('GET /200');
169179
done();
170180
})
171181
.catch(function (err) {
@@ -186,7 +196,7 @@ describe('Promise-Core for Request@2', function () {
186196
transform2xxOnly: true
187197
})
188198
.then(function (response) {
189-
expect(response.body).to.eql('GET /404');
199+
expect(response.body).toEqual('GET /404');
190200
done();
191201
})
192202
.catch(function (err) {
@@ -206,7 +216,7 @@ describe('Promise-Core for Request@2', function () {
206216
json: true
207217
})
208218
.then(function (body) {
209-
expect(body).to.eql('POST /200 - {"a":"b"}');
219+
expect(body).toEqual('POST /200 - {"a":"b"}');
210220
done();
211221
})
212222
.catch(function (err) {
@@ -229,7 +239,7 @@ describe('Promise-Core for Request@2', function () {
229239
}
230240
})
231241
.then(function (body) {
232-
expect(body).to.eql('POST /200 - {"a":"b"}'.split('').reverse().join(''));
242+
expect(body).toEqual('POST /200 - {"a":"b"}'.split('').reverse().join(''));
233243
done();
234244
})
235245
.catch(function (err) {
@@ -242,7 +252,7 @@ describe('Promise-Core for Request@2', function () {
242252

243253
request('http://localhost:4000/301')
244254
.then(function (body) {
245-
expect(body).to.eql('GET /200');
255+
expect(body).toEqual('GET /200');
246256
done();
247257
})
248258
.catch(function (err) {
@@ -258,7 +268,7 @@ describe('Promise-Core for Request@2', function () {
258268
done(new Error('Expected promise to be rejected.'));
259269
})
260270
.catch(function (err) {
261-
expect(err instanceof errors.RequestError).to.eql(true);
271+
expect(err instanceof freshErrors.RequestError).toEqual(true);
262272
done();
263273
});
264274

@@ -271,7 +281,7 @@ describe('Promise-Core for Request@2', function () {
271281
done(new Error('Expected promise to be rejected.'));
272282
})
273283
.catch(function (err) {
274-
expect(err instanceof errors.StatusCodeError).to.eql(true);
284+
expect(err instanceof freshErrors.StatusCodeError).toEqual(true);
275285
done();
276286
});
277287

@@ -285,8 +295,8 @@ describe('Promise-Core for Request@2', function () {
285295
callbackWasCalled = true;
286296
})
287297
.then(function (body) {
288-
expect(body).to.eql('GET /200');
289-
expect(callbackWasCalled).to.eql(true);
298+
expect(body).toEqual('GET /200');
299+
expect(callbackWasCalled).toEqual(true);
290300
done();
291301
})
292302
.catch(function (err) {
@@ -301,13 +311,17 @@ describe('Promise-Core for Request@2', function () {
301311

302312
var request = null, stopServer = null;
303313

304-
before(function (done) {
314+
beforeAll(function (done) {
315+
316+
jest.resetModules();
305317

306318
request = stealthyRequire(require.cache, function () {
307319
return require('request');
308320
});
309321

310-
configure({
322+
var configureFn = require('../../configure/request2.js');
323+
324+
configureFn({
311325
request: request,
312326
PromiseImpl: Bluebird,
313327
expose: [
@@ -325,10 +339,8 @@ describe('Promise-Core for Request@2', function () {
325339

326340
});
327341

328-
after(function (done) {
329-
342+
afterAll(function (done) {
330343
stopServer(done);
331-
332344
});
333345

334346
it('method shortcuts', function (done) {
@@ -342,7 +354,7 @@ describe('Promise-Core for Request@2', function () {
342354
simple: false // <-- ensures that parameter is forwarded
343355
})
344356
.then(function (body) {
345-
expect(body).to.eql('POST /404 - {"a":"b"}');
357+
expect(body).toEqual('POST /404 - {"a":"b"}');
346358
done();
347359
})
348360
.catch(function (err) {
@@ -360,7 +372,7 @@ describe('Promise-Core for Request@2', function () {
360372
resolveWithFullResponse: true
361373
})
362374
.then(function (response) {
363-
expect(response.body).to.eql('GET /404');
375+
expect(response.body).toEqual('GET /404');
364376
done();
365377
})
366378
.catch(function (err) {
@@ -376,7 +388,7 @@ describe('Promise-Core for Request@2', function () {
376388

377389
rpSimpleOffWithFullResp('http://localhost:4000/404')
378390
.then(function (response) {
379-
expect(response.body).to.eql('GET /404');
391+
expect(response.body).toEqual('GET /404');
380392
done();
381393
})
382394
.catch(function (err) {
@@ -390,8 +402,8 @@ describe('Promise-Core for Request@2', function () {
390402

391403
request('http://localhost:4000/200')
392404
.on('complete', function (httpResponse, body) {
393-
expect(httpResponse.statusCode).to.eql(200);
394-
expect(body).to.eql('GET /200');
405+
expect(httpResponse.statusCode).toEqual(200);
406+
expect(body).toEqual('GET /200');
395407
done();
396408
});
397409

@@ -401,7 +413,7 @@ describe('Promise-Core for Request@2', function () {
401413

402414
request('http://localhost:4000/200', { method: 'POST', json: { foo: 'bar' } })
403415
.then(function (body) {
404-
expect(body).to.eql('POST /200 - {"foo":"bar"}');
416+
expect(body).toEqual('POST /200 - {"foo":"bar"}');
405417
done();
406418
})
407419
.catch(function (err) {

0 commit comments

Comments
 (0)