-
Notifications
You must be signed in to change notification settings - Fork 29.1k
/
test-runner-watch-mode.mjs
50 lines (44 loc) · 1.94 KB
/
test-runner-watch-mode.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Flags: --expose-internals
import '../common/index.mjs';
import { describe, it } from 'node:test';
import { spawn } from 'node:child_process';
import { writeFileSync, readFileSync } from 'node:fs';
import util from 'internal/util';
import * as fixtures from '../common/fixtures.mjs';
async function testWatch({ files, fileToUpdate }) {
const ran1 = util.createDeferredPromise();
const ran2 = util.createDeferredPromise();
const child = spawn(process.execPath, ['--watch', '--test', '--no-warnings', ...files], { encoding: 'utf8' });
let stdout = '';
child.stdout.on('data', (data) => {
stdout += data.toString();
const testRuns = stdout.match(/ - test has ran/g);
if (testRuns?.length >= 1) ran1.resolve();
if (testRuns?.length >= 2) ran2.resolve();
});
await ran1.promise;
const content = readFileSync(fileToUpdate, 'utf8');
const interval = setInterval(() => writeFileSync(fileToUpdate, content), 10);
await ran2.promise;
clearInterval(interval);
child.kill();
}
describe('test runner watch mode', () => {
it('should run tests repeatedly', async () => {
const file1 = fixtures.path('test-runner/index.test.js');
const file2 = fixtures.path('test-runner/dependent.js');
await testWatch({ files: [file1, file2], fileToUpdate: file2 });
});
it('should run tests with dependency repeatedly', async () => {
const file1 = fixtures.path('test-runner/index.test.js');
const dependent = fixtures.path('test-runner/dependent.js');
const dependency = fixtures.path('test-runner/dependency.js');
await testWatch({ files: [file1, dependent], fileToUpdate: dependency });
});
it('should run tests with ESM dependency', async () => {
const file1 = fixtures.path('test-runner/index.test.js');
const dependent = fixtures.path('test-runner/dependent.js');
const dependency = fixtures.path('test-runner/dependency.mjs');
await testWatch({ files: [file1, dependent], fileToUpdate: dependency });
});
});