1
1
// Flags: --expose-internals
2
2
import '../common/index.mjs' ;
3
+ import path from 'node:path' ;
3
4
import { describe , it } from 'node:test' ;
4
5
import { spawn } from 'node:child_process' ;
5
- import { writeFileSync , readFileSync } from 'node:fs' ;
6
+ import { writeFileSync } from 'node:fs' ;
6
7
import util from 'internal/util' ;
7
- import * as fixtures from '../common/fixtures.mjs ' ;
8
+ import tmpdir from '../common/tmpdir.js ' ;
8
9
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 } ) {
10
30
const ran1 = util . createDeferredPromise ( ) ;
11
31
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' } ) ;
13
35
let stdout = '' ;
14
36
15
37
child . stdout . on ( 'data' , ( data ) => {
@@ -20,31 +42,27 @@ async function testWatch({ files, fileToUpdate }) {
20
42
} ) ;
21
43
22
44
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 ) ;
25
51
await ran2 . promise ;
26
52
clearInterval ( interval ) ;
27
53
child . kill ( ) ;
28
54
}
29
55
30
56
describe ( 'test runner watch mode' , ( ) => {
31
57
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' } ) ;
35
59
} ) ;
36
60
37
61
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' } ) ;
42
63
} ) ;
43
64
44
65
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' } ) ;
49
67
} ) ;
50
68
} ) ;
0 commit comments