Skip to content

Commit

Permalink
fixup added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecastelli committed May 25, 2024
1 parent 2a75eef commit 1f0ae63
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions test/parallel/test-runner-watch-mode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as common from '../common/index.mjs';
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { spawn } from 'node:child_process';
import { writeFileSync } from 'node:fs';
import { writeFileSync, renameSync, unlinkSync, existsSync } from 'node:fs';
import util from 'internal/util';
import tmpdir from '../common/tmpdir.js';

Expand All @@ -30,7 +30,7 @@ const fixturePaths = Object.keys(fixtureContent)
Object.entries(fixtureContent)
.forEach(([file, content]) => writeFileSync(fixturePaths[file], content));

async function testWatch({ fileToUpdate, file }) {
async function testWatch({ fileToUpdate, file, action = 'update' }) {
const ran1 = util.createDeferredPromise();
const ran2 = util.createDeferredPromise();
const child = spawn(process.execPath,
Expand All @@ -48,6 +48,7 @@ async function testWatch({ fileToUpdate, file }) {
if (testRuns?.length >= 2) ran2.resolve();
});

if (action === 'update') {
await ran1.promise;

Check failure on line 52 in test/parallel/test-runner-watch-mode.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 4 spaces but found 2
runs.push(currentRun);

Check failure on line 53 in test/parallel/test-runner-watch-mode.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 4 spaces but found 2
currentRun = '';

Check failure on line 54 in test/parallel/test-runner-watch-mode.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 4 spaces but found 2
Expand All @@ -58,11 +59,51 @@ async function testWatch({ fileToUpdate, file }) {
runs.push(currentRun);

Check failure on line 59 in test/parallel/test-runner-watch-mode.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 4 spaces but found 2
clearInterval(interval);

Check failure on line 60 in test/parallel/test-runner-watch-mode.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 4 spaces but found 2
child.kill();

Check failure on line 61 in test/parallel/test-runner-watch-mode.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 4 spaces but found 2
for (const run of runs) {
assert.match(run, /# tests 1/);
assert.match(run, /# pass 1/);
assert.match(run, /# fail 0/);
assert.match(run, /# cancelled 0/);
}
} else if (action === 'rename') {
await ran1.promise;
runs.push(currentRun);
currentRun = '';
const fileToRenamePath = tmpdir.resolve(fileToUpdate);
const newFileNamePath = tmpdir.resolve(`test-renamed-${fileToUpdate}`);
const interval = setInterval(() => renameSync(fileToRenamePath, newFileNamePath), common.platformTimeout(1000));
await ran2.promise;
runs.push(currentRun);
clearInterval(interval);
child.kill();

for (const run of runs) {
assert.match(run, /# tests 1/);
assert.match(run, /# pass 1/);
assert.match(run, /# fail 0/);
assert.match(run, /# cancelled 0/);
}
} else if (action === 'delete') {
await ran1.promise;
runs.push(currentRun);
currentRun = '';
const fileToDeletePath = tmpdir.resolve(fileToUpdate);
const interval = setInterval(() => {
if(existsSync(fileToDeletePath)) {
unlinkSync(fileToDeletePath)
} else {
ran2.resolve();
}
}, common.platformTimeout(1000));
await ran2.promise;

runs.push(currentRun);
clearInterval(interval);
child.kill();

for (const run of runs) {
assert.doesNotMatch(run, /MODULE_NOT_FOUND/)
}
}
}

Expand All @@ -82,4 +123,12 @@ describe('test runner watch mode', () => {
it('should support running tests without a file', async () => {
await testWatch({ fileToUpdate: 'test.js' });
});

it('should support a watched test file rename', async () => {
await testWatch({ fileToUpdate: 'test.js', action: 'rename' });
});

it('should not throw when delete a watched test file', async () => {
await testWatch({ fileToUpdate: 'test.js', action: 'delete' });
});
});

0 comments on commit 1f0ae63

Please sign in to comment.