-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.js
60 lines (50 loc) · 1.56 KB
/
runner.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
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const render = require('./render');
const forbiddenDirs = ['node_modules'];
class Runner {
constructor() {
this.testFiles = [];
}
async runTests() {
for (let file of this.testFiles) {
console.log(chalk.gray(`---- ${file.shortName}`));
const beforeEaches = [];
global.render = render;
global.beforeEach = (fn) => {
beforeEaches.push(fn);
};
global.it = async (desc, fn) => {
beforeEaches.forEach(func => func());
try {
await fn();
console.log(chalk.green(`\tok - ${desc}`));
} catch (err) {
const message = err.message.replace(/\n/g, '\n\t\t')
console.log(chalk.red(`\tX - ${desc}`));
console.log(chalk.red('\t', message));
}
};
try {
require(file.name);
} catch (err) {
console.log(chalk.red(err));
}
}
}
async collectFiles(targetPath) {
const files = await fs.promises.readdir(targetPath);
for (let file of files) {
const filepath = path.join(targetPath, file);
const stats = await fs.promises.lstat(filepath);
if (stats.isFile() && file.includes('.test.js')) {
this.testFiles.push({ name: filepath, shortName: file });
} else if (stats.isDirectory() && !forbiddenDirs.includes(file)) {
const childFiles = await fs.promises.readdir(filepath);
files.push(...childFiles.map(f => path.join(file, f)));
}
}
}
}
module.exports = Runner;