Sitelet https://github.com/nodejs/node/pull/53533/commits/3c0dcb2796158c7e42a135f3fda12addda189495
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test_runner: fix delete test file cause dependency file not watched
When a watched test file is being deleted then the referenced dependency
file(s) will be updated incorrect when `unfilterFilesOwnedBy` method is
called, which will cause tests not being rerun when its referenced
dependency changed. To prevent this case, we can simply `return` when we
detect a watched test file being deleted.
  • Loading branch information
jakecastelli committed Aug 7, 2024
commit 3c0dcb2796158c7e42a135f3fda12addda189495
6 changes: 6 additions & 0 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,12 @@ function watchFiles(testFiles, opts) {
watcher.filterFile(resolve(newFileName), owners);
}

// When file deleted
if (!newFileName && previousFileName) {
testFiles = updatedTestFiles;
return;
}

testFiles = updatedTestFiles;
Comment thread
jakecastelli marked this conversation as resolved.
Outdated
}

Expand Down
100 changes: 100 additions & 0 deletions test/parallel/test-runner-watch-mode-complex.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Flags: --expose-internals

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used only for util.createDeferredPromise?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it only used for util.createDeferredPromise

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, unlinkSync } from 'node:fs';
import util from 'internal/util';
import tmpdir from '../common/tmpdir.js';


if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');

tmpdir.refresh();

// This test updates these files repeatedly,
// Reading them from disk is unreliable due to race conditions.
const fixtureContent = {
'dependency.js': 'module.exports = {};',
'dependency.mjs': 'export const a = 1;',
// Test 1
'test.js': `
const test = require('node:test');
require('./dependency.js');
import('./dependency.mjs');
import('data:text/javascript,');
test('first test has ran');`,
// Test 2
'test-2.mjs': `
import test from 'node:test';
import './dependency.js';
import { a } from './dependency.mjs';
import 'data:text/javascript,';
test('second test has ran');`,
// Test file to be deleted
'test-to-delete.mjs': `
import test from 'node:test';
import './dependency.js';
import { a } from './dependency.mjs';
import 'data:text/javascript,';
test('test to delete has ran');`,
};

const fixturePaths = Object.keys(fixtureContent)
.reduce((acc, file) => ({ ...acc, [file]: tmpdir.resolve(file) }), {});
Comment thread
jakecastelli marked this conversation as resolved.
Outdated
Object.entries(fixtureContent)
.forEach(([file, content]) => writeFileSync(fixturePaths[file], content));

describe('test runner watch mode with more complex setup', () => {
it('should run tests when a dependency changed after a watched test file being deleted', async () => {
const ran1 = util.createDeferredPromise();
const ran2 = util.createDeferredPromise();
const child = spawn(process.execPath,
['--watch', '--test'].filter(Boolean),
Comment thread
jakecastelli marked this conversation as resolved.
Outdated
{ encoding: 'utf8', stdio: 'pipe', cwd: tmpdir.path });
let stdout = '';
let currentRun = '';
const runs = [];

child.stdout.on('data', (data) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could just be .toArray()on child.stdout which would also save the util.createDeferredPromise and would make it easier to listen to stdout

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(just make sure to not await it before you trigger the watch)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I get a little bit of help here? I tried to use child.stdout.toArray() - but I cannot seem to be able to control the test runs, I cannot manage to get 2 runs (get result for initial run 3/3 pass, delete a test file modify another test to get the result of second run 2/2 pass) successfully, I can only get result for 1 run

I must be doing something incorrectly.

Would you mind explaining not await to what?

(just make sure to not await it before you trigger the watch)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I may have understood what you mean - use child.stdout.toArray() to get a promise then delete a test file and update a file in order to trigger the rerun (watch), after that resolve the promise to get all the std out results, am I understanding correctly? 👀

stdout += data.toString();
currentRun += data.toString();
const testRuns = stdout.match(/# duration_ms\s\d+/g);

if (testRuns?.length >= 1) ran1.resolve();
if (testRuns?.length >= 2) ran2.resolve();
Comment thread
jakecastelli marked this conversation as resolved.
Outdated
});

await ran1.promise;
runs.push(currentRun);
currentRun = '';
const fileToDeletePathLocal = tmpdir.resolve('test-to-delete.mjs');
unlinkSync(fileToDeletePathLocal);

const content = fixtureContent['dependency.mjs'];
const path = fixturePaths['dependency.mjs'];
const interval = setInterval(() => writeFileSync(path, content), common.platformTimeout(1000));
await ran2.promise;
runs.push(currentRun);
currentRun = '';
clearInterval(interval);
child.kill();

assert.strictEqual(runs.length, 2);

for (let i = 0; i < runs.length; i++) {
if (i === 0) {
assert.match(runs[i], /# tests 3/);
assert.match(runs[i], /# pass 3/);
assert.match(runs[i], /# fail 0/);
assert.match(runs[i], /# cancelled 0/);
} else {
assert.match(runs[i], /# tests 2/);
assert.match(runs[i], /# pass 2/);
assert.match(runs[i], /# fail 0/);
assert.match(runs[i], /# cancelled 0/);
}
}
Comment thread
jakecastelli marked this conversation as resolved.
Outdated
});
});