UNPKG

2.17 kBJavaScriptView Raw
1#!/usr/bin/env node
2import { dirname, resolve } from 'node:path';
3import { fileURLToPath, pathToFileURL } from 'node:url';
4import process from 'node:process';
5import { promisify } from 'node:util';
6import { createReadStream, readFile as baseReadFile } from 'node:fs';
7
8const readFile = promisify(baseReadFile);
9
10import arg from 'arg';
11import { globby } from 'globby';
12import {
13 createDiffReporter,
14 createJSONReporter,
15 createTAPReporter,
16} from 'zora-reporters';
17
18const __filename = fileURLToPath(import.meta.url);
19const __dirname = dirname(__filename);
20
21const reporterMap = {
22 diff: createDiffReporter(),
23 tap: createTAPReporter(),
24 json: createJSONReporter(),
25 log: createJSONReporter(), // alias for "json"
26};
27
28const DEFAULT_FILE_PATTERNS = [
29 '**/test.js',
30 '**/*.spec.js',
31 '**/*.test.js',
32 '**/test/**/*.js',
33 '**/tests/**/*.js',
34 '**/__tests__/**/*.js',
35 '!**/node_modules',
36 '!node_modules',
37];
38
39const {
40 ['--reporter']: reporter = 'diff',
41 ['--only']: only = false,
42 ['--help']: help = false,
43 _: filePatterns,
44} = arg({
45 ['--reporter']: String,
46 ['--only']: Boolean,
47 ['--help']: Boolean,
48 // --module-loader is now ignored.
49 // kept in schema to avoid breaking user's existing usage.
50 ['--module-loader']: String,
51 ['-R']: '--reporter',
52});
53
54(async () => {
55 if (help) {
56 createReadStream(resolve(__dirname, './usage.txt')).pipe(process.stdout);
57 return;
58 }
59
60 // we set the env var before loading zora
61 if (only) {
62 process.env.ZORA_ONLY = true;
63 }
64
65 // loading zora to hold the singleton
66 const { hold, report } = await import('zora');
67 hold();
68
69 const reporterInstance = reporterMap[reporter] || reporter.diff;
70 const files = await globby(
71 filePatterns.length ? filePatterns : DEFAULT_FILE_PATTERNS
72 );
73
74 if (!files.length) {
75 console.warn(`no file matching the patterns: ${filePatterns.join(', ')}`);
76 return;
77 }
78
79 for (const file of files) {
80 const filePath = resolve(process.cwd(), file);
81 await import(pathToFileURL(filePath)); // load file in sequence so any top level await allows the tests to run sequentially if needed
82 }
83
84 await report({
85 reporter: reporterInstance,
86 });
87})();