-
Notifications
You must be signed in to change notification settings - Fork 190
/
test-utils.js
39 lines (33 loc) · 1.05 KB
/
test-utils.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
/* eslint-disable no-use-before-define */
const { readFileSync } = require('fs');
module.exports = (exerciseName, read = false, taskExt = 'js') => {
const dirWithTask = `${__dirname}/src/${exerciseName}`;
return {
task: (number, cb) => {
const oneParam = typeof number === 'function';
const taskCb = oneParam ? number : cb;
const taskNo = oneParam ? '' : number;
const pathToTask = `${dirWithTask}/task${taskNo ? `-${taskNo}` : taskNo}.${taskExt}`;
const moduleExport = read ? readCode(pathToTask) : softRequire(pathToTask);
const conditionalDescribe = moduleExport ? describe : xdescribe;
conditionalDescribe(`Task ${taskNo}`, () => {
taskCb(moduleExport);
});
},
};
};
const readCode = (absolutePath) => {
try {
return readFileSync(absolutePath).toString();
} catch (err) {
return null;
}
};
const softRequire = (modulePath) => {
try {
// eslint-disable-next-line import/no-dynamic-require, global-require
return require(modulePath);
} catch (err) {
return null;
}
};