Skip to content

Commit 7a884b1

Browse files
committed
Test: Inline cli-watch.js test cases and remove "expected/" directory
* Improve and simplify test case naming. * Inline the test case "expected" output for improved comprehension of the purpose of the test, and to make it easier to edit in the future. This was the last remnant of the expected/ directory that we used to use for cli-main.js as well. (The latter has mostly been moved to inline cases as well, plus many using the `.tap.txt` files for simple cases where what's being tested is all in the fixture file, and not in cli-main.js itself.) * Use QUnit.test.each() to make the difference between the first two tests easier to spot. This isn't code de-duplication per-se, since I don't mind that much, but it's to clarify what's difference since they looked nearly the same.
1 parent 81730d8 commit 7a884b1

2 files changed

Lines changed: 165 additions & 202 deletions

File tree

test/cli/cli-watch.js

Lines changed: 165 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ const fs = require('fs');
44
const path = require('path');
55
const fixturify = require('fixturify');
66

7-
const expectedWatchOutput = require('./fixtures/expected/watch-tap-outputs.js');
87
const { executeIpc } = require('./helpers/execute.js');
98

109
const fixturePath = path.join(__dirname, 'fixtures', 'watching');
1110
const isWindows = (process.platform === 'win32');
1211

13-
// TODO: Make watch tests work on Windows. https://github.com/qunitjs/qunit/issues/1359
12+
// TODO: Make watch tests pass on Windows. https://github.com/qunitjs/qunit/issues/1359
1413
QUnit.module.if('CLI Watch', !isWindows, function (hooks) {
1514
hooks.before(function () {
1615
fs.rmSync(fixturePath, { recursive: true, force: true });
@@ -27,7 +26,10 @@ QUnit.module.if('CLI Watch', !isWindows, function (hooks) {
2726
fs.rmSync(fixturePath, { recursive: true, force: true });
2827
});
2928

30-
QUnit.test('runs tests and waits until SIGTERM', async assert => {
29+
QUnit.test.each('no change', [
30+
'SIGTERM',
31+
'SIGINT'
32+
], async (assert, signal) => {
3133
fixturify.writeSync(fixturePath, {
3234
'foo.js': "QUnit.test('foo', function(assert) { assert.true(true); });"
3335
});
@@ -38,40 +40,25 @@ QUnit.module.if('CLI Watch', !isWindows, function (hooks) {
3840
execution => {
3941
execution.on('message', data => {
4042
assert.step(data);
41-
process.kill(execution.pid, 'SIGTERM');
43+
process.kill(execution.pid, signal);
4244
});
4345
}
4446
);
4547

4648
assert.verifySteps(['runEnd']);
4749
assert.equal(result.code, 0);
4850
assert.equal(result.stderr, '');
49-
assert.equal(result.stdout, expectedWatchOutput['no-change']);
51+
assert.equal(result.stdout, `TAP version 13
52+
ok 1 foo
53+
1..1
54+
# pass 1
55+
# skip 0
56+
# todo 0
57+
# fail 0
58+
Stopping QUnit...`);
5059
});
5160

52-
QUnit.test('runs tests and waits until SIGINT', async assert => {
53-
fixturify.writeSync(fixturePath, {
54-
'foo.js': "QUnit.test('foo', function(assert) { assert.true(true); });"
55-
});
56-
57-
const command = ['qunit', '--watch', 'watching'];
58-
const result = await executeIpc(
59-
command,
60-
execution => {
61-
execution.on('message', data => {
62-
assert.step(data);
63-
process.kill(execution.pid);
64-
});
65-
}
66-
);
67-
68-
assert.verifySteps(['runEnd']);
69-
assert.equal(result.code, 0);
70-
assert.equal(result.stderr, '');
71-
assert.equal(result.stdout, expectedWatchOutput['no-change']);
72-
});
73-
74-
QUnit.test('re-runs tests on file changed', async assert => {
61+
QUnit.test('change file once', async assert => {
7562
fixturify.writeSync(fixturePath, {
7663
'foo.js': "QUnit.test('foo', function(assert) { assert.true(true); });"
7764
});
@@ -97,10 +84,26 @@ QUnit.module.if('CLI Watch', !isWindows, function (hooks) {
9784
assert.verifySteps(['runEnd', 'runEnd']);
9885
assert.equal(result.code, 0);
9986
assert.equal(result.stderr, '');
100-
assert.equal(result.stdout, expectedWatchOutput['change-file']);
87+
assert.equal(result.stdout, `TAP version 13
88+
ok 1 foo
89+
1..1
90+
# pass 1
91+
# skip 0
92+
# todo 0
93+
# fail 0
94+
File update: watching/foo.js
95+
Restarting...
96+
TAP version 13
97+
ok 1 bar
98+
1..1
99+
# pass 1
100+
# skip 0
101+
# todo 0
102+
# fail 0
103+
Stopping QUnit...`);
101104
});
102105

103-
QUnit.test('re-runs tests on file added', async assert => {
106+
QUnit.test('add file once [js]', async assert => {
104107
fixturify.writeSync(fixturePath, {
105108
'foo.js': "QUnit.test('foo', function(assert) { assert.true(true); });"
106109
});
@@ -126,13 +129,29 @@ QUnit.module.if('CLI Watch', !isWindows, function (hooks) {
126129
assert.verifySteps(['runEnd', 'runEnd']);
127130
assert.equal(result.code, 0);
128131
assert.equal(result.stderr, '');
129-
assert.equal(result.stdout, expectedWatchOutput['add-file']);
132+
assert.equal(result.stdout, `TAP version 13
133+
ok 1 foo
134+
1..1
135+
# pass 1
136+
# skip 0
137+
# todo 0
138+
# fail 0
139+
File update: watching/bar.js
140+
Restarting...
141+
TAP version 13
142+
ok 1 bar
143+
ok 2 foo
144+
1..2
145+
# pass 2
146+
# skip 0
147+
# todo 0
148+
# fail 0
149+
Stopping QUnit...`);
130150
});
131151

132-
// NOTE: This is known to fail on Linux with Node.js 20.12.
133-
// The regression was fixed in Node.js 20.13.
152+
// NOTE: This is known to fail on Linux with Node.js 20.12. Fixed in Node.js 20.13.
134153
// https://github.com/nodejs/node/issues/52018
135-
QUnit.test('re-runs tests on file removed', async assert => {
154+
QUnit.test('remove file', async assert => {
136155
fixturify.writeSync(fixturePath, {
137156
'foo.js': "QUnit.test('foo', function(assert) { assert.true(true); });",
138157
'bar.js': "QUnit.test('bar', function(assert) { assert.true(true); });"
@@ -159,11 +178,28 @@ QUnit.module.if('CLI Watch', !isWindows, function (hooks) {
159178
assert.verifySteps(['runEnd', 'runEnd']);
160179
assert.equal(result.code, 0);
161180
assert.equal(result.stderr, '');
162-
assert.equal(result.stdout, expectedWatchOutput['remove-file']);
181+
assert.equal(result.stdout, `TAP version 13
182+
ok 1 bar
183+
ok 2 foo
184+
1..2
185+
# pass 2
186+
# skip 0
187+
# todo 0
188+
# fail 0
189+
File remove: watching/bar.js
190+
Restarting...
191+
TAP version 13
192+
ok 1 foo
193+
1..1
194+
# pass 1
195+
# skip 0
196+
# todo 0
197+
# fail 0
198+
Stopping QUnit...`);
163199
});
164200

165201
// Skip in coverage mode since NYC adds non-default extensions
166-
QUnit.test.if('default file extensions', !process.env.NYC_PROCESS_ID, async assert => {
202+
QUnit.test.if('add file once [other file extensions]', !process.env.NYC_PROCESS_ID, async assert => {
167203
fixturify.writeSync(fixturePath, {
168204
tests: {
169205
'setup.js': "QUnit.on('runEnd', function() { process.send('runEnd'); });",
@@ -208,11 +244,32 @@ QUnit.module.if('CLI Watch', !isWindows, function (hooks) {
208244

209245
assert.equal(result.code, 0);
210246
assert.equal(result.stderr, '');
211-
assert.equal(result.stdout, expectedWatchOutput['file-extensions']);
247+
assert.equal(result.stdout, `TAP version 13
248+
ok 1 foo
249+
1..1
250+
# pass 1
251+
# skip 0
252+
# todo 0
253+
# fail 0
254+
File update: watching/x.cjs
255+
File update: watching/x.js
256+
File update: watching/x.json
257+
File update: watching/x.mjs
258+
File update: watching/tests/foo.js
259+
File update: watching/tests/setup.js
260+
Restarting...
261+
TAP version 13
262+
ok 1 foo2
263+
1..1
264+
# pass 1
265+
# skip 0
266+
# todo 0
267+
# fail 0
268+
Stopping QUnit...`);
212269
});
213270

214271
// Skip in coverage mode since NYC adds non-default extensions
215-
QUnit.test.if('TypeScript file extension', !process.env.NYC_PROCESS_ID, async assert => {
272+
QUnit.test.if('add file once [TypeScript]', !process.env.NYC_PROCESS_ID, async assert => {
216273
fixturify.writeSync(fixturePath, {
217274

218275
// Simulate what ts-node/register does
@@ -246,12 +303,32 @@ QUnit.module.if('CLI Watch', !isWindows, function (hooks) {
246303

247304
assert.equal(result.code, 0);
248305
assert.equal(result.stderr, '');
249-
assert.equal(result.stdout, expectedWatchOutput['file-extension-ts']);
306+
assert.equal(result.stdout, `TAP version 13
307+
ok 1 foo
308+
1..1
309+
# pass 1
310+
# skip 0
311+
# todo 0
312+
# fail 0
313+
File update: watching/x.js
314+
File update: watching/x.ts
315+
File update: watching/tests/foo.js
316+
File update: watching/tests/setup.js
317+
Restarting...
318+
TAP version 13
319+
ok 1 foo2
320+
1..1
321+
# pass 1
322+
# skip 0
323+
# todo 0
324+
# fail 0
325+
Stopping QUnit...`);
250326
});
251327

252-
QUnit.test('aborts and restarts when in middle of run', async assert => {
253-
// A proper abort finishes the currently running test and runs any remaining
254-
// afterEach/after hooks to ensure cleanup happens.
328+
QUnit.test('change file', async assert => {
329+
// An abort should finish the currently running test and any afterEach/after
330+
// hooks to ensure cleanup. It then ends in the middle of run, skipping any
331+
// other tests.
255332

256333
fixturify.writeSync(fixturePath, {
257334
tests: {
@@ -324,10 +401,27 @@ QUnit.module.if('CLI Watch', !isWindows, function (hooks) {
324401
]);
325402
assert.equal(result.code, 0);
326403
assert.equal(result.stderr, '');
327-
assert.equal(result.stdout, expectedWatchOutput['change-file-mid-run']);
404+
assert.equal(result.stdout, `TAP version 13
405+
File update: watching/bar.js
406+
Finishing current test and restarting...
407+
ok 1 Foo > one
408+
1..2
409+
# pass 2
410+
# skip 0
411+
# todo 0
412+
# fail 0
413+
TAP version 13
414+
ok 1 Foo > one
415+
ok 2 Foo > two
416+
1..2
417+
# pass 2
418+
# skip 0
419+
# todo 0
420+
# fail 0
421+
Stopping QUnit...`);
328422
});
329423

330-
QUnit.test('properly watches files after initial run', async assert => {
424+
QUnit.test('add file then change newly added file', async assert => {
331425
fixturify.writeSync(fixturePath, {
332426
tests: {
333427
'setup.js': "QUnit.on('runEnd', function() { process.send('runEnd'); });",
@@ -387,6 +481,32 @@ QUnit.module.if('CLI Watch', !isWindows, function (hooks) {
387481
]);
388482
assert.equal(result.code, 0);
389483
assert.equal(result.stderr, '');
390-
assert.equal(result.stdout, expectedWatchOutput['add-file-after-run']);
484+
assert.equal(result.stdout, `TAP version 13
485+
ok 1 Module > Test
486+
1..1
487+
# pass 1
488+
# skip 0
489+
# todo 0
490+
# fail 0
491+
File update: watching/tests/foo.js
492+
File update: watching/bar.js
493+
Restarting...
494+
TAP version 13
495+
ok 1 Module > Test
496+
1..1
497+
# pass 1
498+
# skip 0
499+
# todo 0
500+
# fail 0
501+
File update: watching/bar.js
502+
Restarting...
503+
TAP version 13
504+
ok 1 Module > Test
505+
1..1
506+
# pass 1
507+
# skip 0
508+
# todo 0
509+
# fail 0
510+
Stopping QUnit...`);
391511
});
392512
});

0 commit comments

Comments
 (0)