11import path from 'node:path' ;
2- import { spawn } from 'node:child_process' ;
3- import { pathToFileURL } from 'node:url' ;
2+ import { exec , spawn } from 'node:child_process' ;
43import chalk from 'chalk' ;
4+ import { existsSync } from 'node:fs' ;
5+ import { rm } from 'node:fs/promises' ;
6+ import { promisify } from 'node:util' ;
7+
8+ const execAsync = promisify ( exec ) ;
9+
510/**
611 * Run all tests for the given library
712 * @param {object } options - The options object
813 * @param {string } options.library - The library to test
14+ * @param {string } [options.folder] - The folder to test
915 * @returns {Promise<void> }
1016 */
11- const test = async ( { library, folder, reporter } ) => {
17+ const test = async ( { library, folder } ) => {
1218 const start = performance . now ( ) ;
1319 console . log ( `Running tests for ${ library } ` ) ;
1420
1521 // Resolve paths in a cross-platform way
1622 const mainFolder = path . join ( process . cwd ( ) , 'lib' , library ) ;
17- const reporterPath = pathToFileURL (
18- path . join ( process . cwd ( ) , 'tools' , 'testing' , 'testReporter.mjs' ) ,
19- ) ;
2023 const flags = folder ? `--folder=${ folder } ` : '' ;
2124 const runnerPath = path . join (
2225 process . cwd ( ) ,
2326 'tools' ,
2427 'testing' ,
2528 'testRunner.mjs' ,
2629 ) ;
27- // "glob -c \"tsx --test\" \"./src/**/*.test.ts\""
30+
31+ if ( ! folder ?. endsWith ( '.test.ts' ) ) {
32+ if ( existsSync ( mainFolder + '/.tmp' ) ) {
33+ await rm ( mainFolder + '/.tmp' , { recursive : true } ) ;
34+ }
35+
36+ const build = await execAsync (
37+ `npx swc ./src -d ${ mainFolder } /.tmp` ,
38+ {
39+ cwd : mainFolder ,
40+ shell : true ,
41+ } ,
42+ ) ;
43+
44+
45+ if ( build . stderr ) {
46+ console . error ( build . stderr ) ;
47+ process . exit ( 1 ) ;
48+ }
49+ }
50+
2851 const spwn = spawn (
29- // `npx glob -c "tsx --test-reporter="${reporterPath.toString()}" --test" "./src/**/*.test.ts"`,
30- `node --import tsx "${ runnerPath . toString ( ) } " -- ${ flags } ` ,
52+ // `npx glob -c "tsx --test --experimental-test-coverage --test-reporter=lcov --test-reporter=spec" "./src/**/*.test.ts"`,
53+ ( folder ?. endsWith ( '.test.ts' ) )
54+ ? `node --import tsx "${ runnerPath . toString ( ) } " -- ${ flags } ` :
55+ `node "${ runnerPath . toString ( ) } " -- ${ flags } ` ,
3156 [ ] ,
3257 {
3358 stdio : 'inherit' ,
3459 // Set cwd to the project root
35- cwd : `${ mainFolder } ` ,
60+ cwd : ( ! folder ?. endsWith ( '.test.ts' ) )
61+ ? `${ mainFolder } /.tmp/src` : `${ mainFolder } /src` ,
3662 shell : true ,
3763 } ,
3864 ) ;
3965
40- spwn . on ( 'exit' , ( code ) => {
66+ spwn . on ( 'exit' , async ( code ) => {
4167 if ( code !== 0 ) {
4268 console . error ( `Failed to test ${ library } ` ) ;
69+
70+ if ( ( ! folder ?. endsWith ( '.test.ts' ) ) ) await rm ( mainFolder + '/.tmp' , { recursive : true } ) ;
4371 process . exit ( 1 ) ;
4472 }
4573 } ) ;
@@ -49,13 +77,15 @@ const test = async ({ library, folder, reporter }) => {
4977 process . exit ( 1 ) ;
5078 } ) ;
5179
52- spwn . on ( 'close' , ( ) => {
80+ spwn . on ( 'close' , async ( ) => {
5381 console . log ( `Tested ${ library } ` ) ;
5482 console . log (
5583 chalk . gray (
5684 `Duration: ${ ( ( performance . now ( ) - start ) / 1000 ) . toFixed ( 2 ) } s` ,
5785 ) ,
5886 ) ;
87+
88+ if ( ( ! folder ?. endsWith ( '.test.ts' ) ) ) await rm ( mainFolder + '/.tmp' , { recursive : true } ) ;
5989 } ) ;
6090} ;
6191
0 commit comments