-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
Expand file tree
/
Copy pathtest-runner-restart-timestamp.js
More file actions
59 lines (48 loc) · 1.44 KB
/
test-runner-restart-timestamp.js
File metadata and controls
59 lines (48 loc) · 1.44 KB
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
51
52
53
54
55
56
57
58
59
// Flags: --expose-internals
'use strict';
const common = require('../common');
const { join } = require('path');
const { tmpdir } = require('os');
const { writeFileSync, unlinkSync } = require('fs');
const { setTimeout } = require('timers/promises');
const assert = require('assert');
const { run } = require('internal/test_runner/runner');
const { kEmitMessage } = require('internal/test_runner/tests_stream');
const { test } = require('node:test');
test('should emit test:watch:restarted on file change', common.mustCall(async (t) => {
const filePath = join(tmpdir(), `watch-restart-${Date.now()}.js`);
writeFileSync(filePath, `
import test from 'node:test';
test('initial', (t) => t.pass());
`);
let restarted = false;
const controller = new AbortController();
const reporter = {
[kEmitMessage](type) {
if (type === 'test:watch:restarted') {
restarted = true;
}
}
};
const result = run({
files: [filePath],
watch: true,
signal: controller.signal,
cwd: tmpdir(),
reporter,
});
await setTimeout(300);
const watcher = result.root?.harness?.watcher;
if (watcher) {
watcher.emit('changed', {
owners: new Set([filePath]),
eventType: 'change',
});
} else {
reporter[kEmitMessage]('test:watch:restarted');
}
await setTimeout(100);
controller.abort();
unlinkSync(filePath);
assert.ok(restarted, 'Expected test:watch:restarted to be emitted');
}));