UNPKG

1.11 kBJavaScriptView Raw
1#!/usr/bin/env node
2import { promises as fs } from 'fs';
3import { basename, dirname, join } from 'path';
4async function* walk(dir) {
5 for await (const d of await fs.opendir(dir)) {
6 const entry = join(dir, d.name);
7 if (d.isDirectory())
8 yield* walk(entry);
9 else if (d.isFile())
10 yield entry;
11 }
12}
13async function runTestFile(file) {
14 for (const value of Object.values(await import(join(process.cwd(), file)))) {
15 if (typeof value === 'function') {
16 try {
17 await value();
18 }
19 catch (e) {
20 if (e instanceof Error)
21 console.error(e.stack);
22 }
23 }
24 }
25}
26async function run(arg = '.') {
27 if ((await fs.lstat(arg)).isFile()) {
28 return runTestFile(arg);
29 }
30 for await (const file of walk(arg)) {
31 if (!dirname(file).includes('node_modules') &&
32 (basename(file) === 'test.js' || file.endsWith('.test.js'))) {
33 console.log(file);
34 await runTestFile(file);
35 }
36 }
37}
38void run(process.argv[2]);