|
| 1 | +// Flags: --expose-internals |
| 2 | +import * as common from '../common/index.mjs'; |
| 3 | +import { describe, it } from 'node:test'; |
| 4 | +import assert from 'node:assert'; |
| 5 | +import { spawn } from 'node:child_process'; |
| 6 | +import { writeFileSync } from 'node:fs'; |
| 7 | +import tmpdir from '../common/tmpdir.js'; |
| 8 | + |
| 9 | +if (common.isIBMi) |
| 10 | + common.skip('IBMi does not support `fs.watch()`'); |
| 11 | + |
| 12 | +if (common.isAIX) |
| 13 | + common.skip('folder watch capability is limited in AIX.'); |
| 14 | + |
| 15 | +tmpdir.refresh(); |
| 16 | + |
| 17 | +// Set up test files and dependencies |
| 18 | +const fixtureContent = { |
| 19 | + 'dependency.js': 'module.exports = {};', |
| 20 | + 'test.js': ` |
| 21 | +const test = require('node:test'); |
| 22 | +require('./dependency.js'); |
| 23 | +test('first test has ran');`, |
| 24 | + 'test-2.js': ` |
| 25 | +const test = require('node:test'); |
| 26 | +require('./dependency.js'); |
| 27 | +test('second test has ran');`, |
| 28 | +}; |
| 29 | + |
| 30 | +const fixturePaths = Object.fromEntries(Object.keys(fixtureContent) |
| 31 | + .map((file) => [file, tmpdir.resolve(file)])); |
| 32 | + |
| 33 | +Object.entries(fixtureContent) |
| 34 | + .forEach(([file, content]) => writeFileSync(fixturePaths[file], content)); |
| 35 | + |
| 36 | +describe('test runner watch mode with more complex setup', () => { |
| 37 | + it('should re-run appropriate tests when dependencies change', async () => { |
| 38 | + // Start the test runner in watch mode |
| 39 | + const child = spawn(process.execPath, |
| 40 | + ['--watch', '--test'], |
| 41 | + { encoding: 'utf8', stdio: 'pipe', cwd: tmpdir.path }); |
| 42 | + |
| 43 | + let currentRunOutput = ''; |
| 44 | + const testRuns = []; |
| 45 | + |
| 46 | + const firstRunCompleted = Promise.withResolvers(); |
| 47 | + const secondRunCompleted = Promise.withResolvers(); |
| 48 | + const thirdRunCompleted = Promise.withResolvers(); |
| 49 | + const fourthRunCompleted = Promise.withResolvers(); |
| 50 | + |
| 51 | + child.stdout.on('data', (data) => { |
| 52 | + const str = data.toString(); |
| 53 | + currentRunOutput += str; |
| 54 | + |
| 55 | + if (/duration_ms\s\d+/.test(str)) { |
| 56 | + // Test run has completed |
| 57 | + testRuns.push(currentRunOutput); |
| 58 | + currentRunOutput = ''; |
| 59 | + switch (testRuns.length) { |
| 60 | + case 1: |
| 61 | + firstRunCompleted.resolve(); |
| 62 | + break; |
| 63 | + case 2: |
| 64 | + secondRunCompleted.resolve(); |
| 65 | + break; |
| 66 | + case 3: |
| 67 | + thirdRunCompleted.resolve(); |
| 68 | + break; |
| 69 | + case 4: |
| 70 | + fourthRunCompleted.resolve(); |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + // Wait for the initial test run to complete |
| 77 | + await firstRunCompleted.promise; |
| 78 | + |
| 79 | + // Modify 'dependency.js' to trigger re-run of both tests |
| 80 | + writeFileSync(fixturePaths['dependency.js'], 'module.exports = { modified: true };'); |
| 81 | + |
| 82 | + // Wait for the second test run to complete |
| 83 | + await secondRunCompleted.promise; |
| 84 | + |
| 85 | + // Modify 'test.js' to trigger re-run of only 'test.js' |
| 86 | + writeFileSync(fixturePaths['test.js'], ` |
| 87 | +const test = require('node:test'); |
| 88 | +require('./dependency.js'); |
| 89 | +test('first test has ran again');`); |
| 90 | + |
| 91 | + // Wait for the third test run to complete |
| 92 | + await thirdRunCompleted.promise; |
| 93 | + |
| 94 | + // Modify 'dependency.js' again to trigger re-run of both tests |
| 95 | + writeFileSync(fixturePaths['dependency.js'], 'module.exports = { modified: true, again: true };'); |
| 96 | + |
| 97 | + // Wait for the fourth test run to complete |
| 98 | + await fourthRunCompleted.promise; |
| 99 | + |
| 100 | + // Kill the child process |
| 101 | + child.kill(); |
| 102 | + |
| 103 | + // Analyze the test runs |
| 104 | + assert.strictEqual(testRuns.length, 4); |
| 105 | + |
| 106 | + // First test run - Both tests should run |
| 107 | + const firstRunOutput = testRuns[0]; |
| 108 | + assert.match(firstRunOutput, /first test has ran/); |
| 109 | + assert.match(firstRunOutput, /second test has ran/); |
| 110 | + |
| 111 | + // Second test run - We have modified 'dependency.js' only, so both tests should re-run |
| 112 | + const secondRunOutput = testRuns[1]; |
| 113 | + assert.match(secondRunOutput, /first test has ran/); |
| 114 | + assert.match(secondRunOutput, /second test has ran/); |
| 115 | + |
| 116 | + // Third test run - We have modified 'test.js' only |
| 117 | + const thirdRunOutput = testRuns[2]; |
| 118 | + assert.match(thirdRunOutput, /first test has ran again/); |
| 119 | + assert.doesNotMatch(thirdRunOutput, /second test has ran/); |
| 120 | + |
| 121 | + // Fourth test run - We have modified 'dependency.js' again, so both tests should re-run |
| 122 | + const fourthRunOutput = testRuns[3]; |
| 123 | + assert.match(fourthRunOutput, /first test has ran again/); |
| 124 | + assert.match(fourthRunOutput, /second test has ran/); |
| 125 | + }); |
| 126 | +}); |
0 commit comments