-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmock.js
32 lines (25 loc) · 775 Bytes
/
mock.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
const path = require('path');
const nock = require('nock');
const validate = (item) => {
if (item.mocks === undefined || !item.mocks.length) {
throw new Error('Offline HTTP Mock: No mocks defined!');
}
if (item.hostname === undefined) {
throw new Error('Offline HTTP Mock: No hostname defined!');
}
return true;
};
const start = ({ item, servicePath, filename }) => {
const { hostname, directory = null } = item;
const file = path.join(servicePath, directory, filename);
// eslint-disable-next-line import/no-dynamic-require
const fn = require(file);
if (typeof fn !== 'function') {
throw new Error(`Offline HTTP Mock: ${file} did not return a function!`);
}
return fn(nock, hostname);
};
module.exports = {
validate,
start,
};