11// Flags: --expose-internals
22import '../common/index.mjs' ;
3+ import path from 'node:path' ;
34import { describe , it } from 'node:test' ;
45import { spawn } from 'node:child_process' ;
5- import { writeFileSync , readFileSync } from 'node:fs' ;
6+ import { writeFileSync } from 'node:fs' ;
67import util from 'internal/util' ;
7- import * as fixtures from '../common/fixtures.mjs ' ;
8+ import tmpdir from '../common/tmpdir.js ' ;
89
9- async function testWatch ( { files, fileToUpdate } ) {
10+ tmpdir . refresh ( ) ;
11+
12+ // This test updates these files repeatedly,
13+ // Reading them from disk is unreliable due to race conditions.
14+ const fixtureContent = {
15+ 'dependency.js' : 'module.exports = {};' ,
16+ 'dependency.mjs' : 'export const a = 1;' ,
17+ 'dependent.js' : `
18+ const test = require('node:test');
19+ require('./dependency.js');
20+ import('./dependency.mjs');
21+ import('data:text/javascript,');
22+ test('test has ran');` ,
23+ } ;
24+ const fixturePaths = Object . keys ( fixtureContent )
25+ . reduce ( ( acc , file ) => ( { ...acc , [ file ] : path . join ( tmpdir . path , file ) } ) , { } ) ;
26+ Object . entries ( fixtureContent )
27+ . forEach ( ( [ file , content ] ) => writeFileSync ( fixturePaths [ file ] , content ) ) ;
28+
29+ async function testWatch ( { fileToUpdate } ) {
1030 const ran1 = util . createDeferredPromise ( ) ;
1131 const ran2 = util . createDeferredPromise ( ) ;
12- const child = spawn ( process . execPath , [ '--watch' , '--test' , '--no-warnings' , ...files ] , { encoding : 'utf8' } ) ;
32+ const child = spawn ( process . execPath ,
33+ [ '--watch' , '--test' , '--no-warnings' , fixturePaths [ 'dependent.js' ] ] ,
34+ { encoding : 'utf8' , stdio : 'pipe' } ) ;
1335 let stdout = '' ;
1436
1537 child . stdout . on ( 'data' , ( data ) => {
@@ -20,31 +42,27 @@ async function testWatch({ files, fileToUpdate }) {
2042 } ) ;
2143
2244 await ran1 . promise ;
23- const content = readFileSync ( fileToUpdate , 'utf8' ) ;
24- const interval = setInterval ( ( ) => writeFileSync ( fileToUpdate , content ) , 10 ) ;
45+ const content = fixtureContent [ fileToUpdate ] ;
46+ const path = fixturePaths [ fileToUpdate ] ;
47+ const interval = setInterval ( ( ) => {
48+ console . log ( `Updating ${ path } ` ) ;
49+ writeFileSync ( path , content ) ;
50+ } , 50 ) ;
2551 await ran2 . promise ;
2652 clearInterval ( interval ) ;
2753 child . kill ( ) ;
2854}
2955
3056describe ( 'test runner watch mode' , ( ) => {
3157 it ( 'should run tests repeatedly' , async ( ) => {
32- const file1 = fixtures . path ( 'test-runner/index.test.js' ) ;
33- const file2 = fixtures . path ( 'test-runner/dependent.js' ) ;
34- await testWatch ( { files : [ file1 , file2 ] , fileToUpdate : file2 } ) ;
58+ await testWatch ( { fileToUpdate : 'dependent.js' } ) ;
3559 } ) ;
3660
3761 it ( 'should run tests with dependency repeatedly' , async ( ) => {
38- const file1 = fixtures . path ( 'test-runner/index.test.js' ) ;
39- const dependent = fixtures . path ( 'test-runner/dependent.js' ) ;
40- const dependency = fixtures . path ( 'test-runner/dependency.js' ) ;
41- await testWatch ( { files : [ file1 , dependent ] , fileToUpdate : dependency } ) ;
62+ await testWatch ( { fileToUpdate : 'dependency.js' } ) ;
4263 } ) ;
4364
4465 it ( 'should run tests with ESM dependency' , async ( ) => {
45- const file1 = fixtures . path ( 'test-runner/index.test.js' ) ;
46- const dependent = fixtures . path ( 'test-runner/dependent.js' ) ;
47- const dependency = fixtures . path ( 'test-runner/dependency.mjs' ) ;
48- await testWatch ( { files : [ file1 , dependent ] , fileToUpdate : dependency } ) ;
66+ await testWatch ( { fileToUpdate : 'dependency.mjs' } ) ;
4967 } ) ;
5068} ) ;
0 commit comments