-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
81 lines (65 loc) · 2.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* eslint-disable class-methods-use-this */
const request = require('supertest');
const nock = require('nock');
const path = require('path');
const fs = require('fs');
const getDefaultOptions = require('./lib/options');
const defaultConfig = {
localEnvironment: true,
fixtureName: 'fixture.json',
fixtureDir: null,
whitelistedHosts: /(localhost|127\.0\.0\.1)/,
healthcheck: null,
customFilter: () => true,
};
class NockBackCI {
constructor(userConfig) {
this.config = { ...defaultConfig, ...userConfig };
this.defaultOptions = getDefaultOptions(this.config.whitelistedHosts, this.config.customFilter);
nock.back.fixtures = this.config.fixtureDir;
if (this.config.localEnvironment) {
const testFixtureFile = path.join(this.config.fixtureDir, this.config.fixtureName);
if (fs.existsSync(testFixtureFile)) {
fs.unlinkSync(testFixtureFile);
}
const bootFixtureFile = path.join(this.config.fixtureDir, 'boot.json');
if (fs.existsSync(bootFixtureFile)) {
fs.unlinkSync(bootFixtureFile);
}
}
}
async bootServer(appProvider) {
let healthChecked = false;
nock.back.setMode('record');
const { nockDone } = await nock.back('boot.json', this.defaultOptions);
nock.enableNetConnect(this.config.whitelistedHosts);
const app = await appProvider();
const server = request.agent(app);
while (this.config.healthcheck && !healthChecked) {
// eslint-disable-next-line no-await-in-loop
const response = await server.get(this.config.healthcheck);
if (response && response.statusCode === 200) {
healthChecked = true;
}
}
nockDone();
nock.back.setMode('wild');
return server;
}
async testCaseInit() {
nock.back.setMode('record');
const { nockDone } = await nock.back(this.config.fixtureName, this.defaultOptions);
nock.enableNetConnect(this.config.whitelistedHosts);
return nockDone;
}
testCaseEnd(nockDone) {
nockDone();
nock.back.setMode('wild');
}
killServer(server, done) {
nock.back.setMode('wild');
server.app.close(done);
nock.restore();
}
}
module.exports = NockBackCI;