/
run.ts
165 lines (141 loc) · 5.05 KB
/
run.ts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { readdirSync, existsSync } from 'node:fs';
import { resolve, sep } from 'node:path';
import { run } from 'node:test';
import { EOL } from 'node:os'
const SIGN = '\u001b[';
const style = (msg: string, code: number[]) => SIGN + code[0] + 'm' + msg + SIGN + code[1] + 'm';
const colorRed = (msg: string) => style(msg, [31, 39]);
const colorGreen = (msg: string) => style(msg, [32, 39]);
const colorYellow = (msg: string) => style(msg, [33, 39]);
const colorGray = (msg: string) => style(msg, [90, 39]);
const bgYellow = (msg: string) => style(msg, [43, 49]);
// const RunOptions = Pat
export type RunTestOptions = {
/**
* SourceCode root dir
* @default './src' if exists nor `./`
*/
root?: string
/**
* Testing file name
* @default '.test.ts'
*/
suffix?: string;
/**
* Testing file name Filter
* @default (fileName:string)=>true
*/
filter?: (fileName: string) => boolean;
/**
* A number of milliseconds the test will fail after.
* If unspecified, subtests inherit this value from their parent.
* @default Infinity
*/
timeout?: number
// '--root'
// '--testNamePatterns'
// '--fileNamePatterns'
}
export const watch = (options?: RunTestOptions, files?: string[]) => {
const [opt, testFiles] = files ? [options, files] as [Required<RunTestOptions>, string[]] : getOptions(options);
if (testFiles.length == 0) {
console.log(`${bgYellow(colorRed(' There is no file to test '))}`);
console.log(colorGray(`Source : `) + colorYellow(opt.root));
console.log(colorGray(`Suffix : `) + colorYellow(opt.suffix));
console.log(colorGray(`Filter : `) + colorYellow(opt.filter == null ? 'none' : opt.filter.toString()));
return;
}
const TESTER = run({
files: testFiles,
watch: true,
setup: (test) => {
}
});
TESTER.on('test:fail', (info) => {
process.stdout.write(`${colorYellow('✖')} ${colorRed(info.name)} : ${info.details.error.message}` + EOL)
})
TESTER.on('test:pass', (info) => {
process.stdout.write(`${colorGray('✔')} ${colorGreen(info.name)} ${colorGray('(' + info.details.duration_ms.toFixed(4) + 'ms)')}` + EOL)
})
TESTER.on('test:stderr', (err) => {
process.stderr.write(err.message)
})
TESTER.on('test:stdout', (info) => {
process.stdout.write(info.message)
})
}
export const start = (options?: RunTestOptions, files?: string[]) => {
const testFiles = files ? files : getOptions(options)[1];
for (let file of testFiles) require(file);
}
const getOptions = (options?: RunTestOptions): [Required<RunTestOptions>, string[]] => {
const opt: Required<RunTestOptions> = Object.assign({}, options) as any
if (opt.root == undefined) {
if (existsSync(resolve('./src'))) {
opt.root = resolve('./src')
} else {
opt.root = resolve('.');
}
}
if (opt.suffix == undefined) {
opt.suffix = process.execArgv.indexOf('ts-node/') >= 0 ? '.test.ts' : '.test.js';
}
const FILTER = opt.filter instanceof Function ? opt.filter : (name: string) => true;
const files = (readdirSync(opt.root, { recursive: true }) as string[])
.filter(item => item.endsWith(opt.suffix))
.filter(FILTER)
.map(item => resolve(opt.root as string, item))
;
return [opt, files];
}
const AutoStart = () => {
if (process.argv.length < 2) return;
const CMD = 'tsest' + sep + 'run';
if (!(process.argv[1].endsWith(CMD) || process.argv[1].endsWith(CMD + '.js'))) return;
const ExecArg = new Set(process.execArgv);
const CmdArg = new Set(process.argv);
const runOptions: RunTestOptions = {
suffix: (ExecArg.has('ts-node/register') ||
ExecArg.has('ts-node/esm'))
? '.test.ts' : '.test.js',
}
for (let arg of process.argv.slice(2)) {
if (!arg.startsWith('--')) continue;
let idx = arg.indexOf('=');
if (idx <= 0) continue;
const name = arg.substring(0, idx);
const value = arg.substring(idx + 1);
if (value.length == 0) continue;
switch (name.substring(2)) {
case 'root':
runOptions.root = value;
continue;
case 'timeout':
try {
runOptions.timeout = parseInt(value);
} catch (e) {
}
continue;
case 'suffix':
runOptions.suffix = value;
continue;
case 'testName':
continue;
case 'fileName':
runOptions.filter = (name) => name.indexOf(value) >= 0;
continue;
default:
continue;
}
}
const [options, files] = getOptions(runOptions)
const isWatch = CmdArg.has('--watch') || CmdArg.has('-w');
if (isWatch) {
watch(options, files)
console.log('Start Watch Mode')
} else {
start(options, files)
console.log('Strat Run Mode')
}
}
AutoStart();