| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -412,8 +412,8 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) { | |||
| 412 | 412 | return subtest.start(); | |
| 413 | 413 | } | |
| 414 | 414 | ||
| 415 | - function watchFiles(testFiles, root, inspectPort, testNamePatterns) { | ||
| 416 | - const filesWatcher = new FilesWatcher({ throttle: 500, mode: 'filter' }); | ||
| 415 | + function watchFiles(testFiles, root, inspectPort, signal, testNamePatterns) { | ||
| 416 | + const filesWatcher = new FilesWatcher({ throttle: 500, mode: 'filter', signal }); | ||
| 417 | 417 | filesWatcher.on('changed', ({ owners }) => { | |
| 418 | 418 | filesWatcher.unfilterFilesOwnedBy(owners); | |
| 419 | 419 | PromisePrototypeThen(SafePromiseAllReturnVoid(testFiles, async (file) => { | |
@@ -435,6 +435,7 @@ function watchFiles(testFiles, root, inspectPort, testNamePatterns) { | |||
| 435 | 435 | triggerUncaughtException(error, true /* fromPromise */); | |
| 436 | 436 | })); | |
| 437 | 437 | }); | |
| 438 | + signal?.addEventListener('abort', () => root.postRun(), { __proto__: null, once: true }); | ||
| 438 | 439 | return filesWatcher; | |
| 439 | 440 | } | |
| 440 | 441 | ||
@@ -477,7 +478,7 @@ function run(options) { | |||
| 477 | 478 | let postRun = () => root.postRun(); | |
| 478 | 479 | let filesWatcher; | |
| 479 | 480 | if (watch) { | |
| 480 | - filesWatcher = watchFiles(testFiles, root, inspectPort, testNamePatterns); | ||
| 481 | + filesWatcher = watchFiles(testFiles, root, inspectPort, signal, testNamePatterns); | ||
| 481 | 482 | postRun = undefined; | |
| 482 | 483 | } | |
| 483 | 484 | const runFiles = () => { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,14 +30,18 @@ class FilesWatcher extends EventEmitter { | |||
| 30 | 30 | #ownerDependencies = new SafeMap(); | |
| 31 | 31 | #throttle; | |
| 32 | 32 | #mode; | |
| 33 | + #signal; | ||
| 33 | 34 | ||
| 34 | - constructor({ throttle = 500, mode = 'filter' } = kEmptyObject) { | ||
| 35 | + constructor({ throttle = 500, mode = 'filter', signal } = kEmptyObject) { | ||
| 35 | 36 | super(); | |
| 36 | 37 | ||
| 37 | 38 | validateNumber(throttle, 'options.throttle', 0, TIMEOUT_MAX); | |
| 38 | 39 | validateOneOf(mode, 'options.mode', ['filter', 'all']); | |
| 39 | 40 | this.#throttle = throttle; | |
| 40 | 41 | this.#mode = mode; | |
| 42 | + this.#signal = signal; | ||
| 43 | + | ||
| 44 | + signal?.addEventListener('abort', () => this.clear(), { __proto__: null, once: true }); | ||
| 41 | 45 | } | |
| 42 | 46 | ||
| 43 | 47 | #isPathWatched(path) { | |
@@ -89,7 +93,7 @@ class FilesWatcher extends EventEmitter { | |||
| 89 | 93 | if (this.#isPathWatched(path)) { | |
| 90 | 94 | return; | |
| 91 | 95 | } | |
| 92 | - const watcher = watch(path, { recursive }); | ||
| 96 | + const watcher = watch(path, { recursive, signal: this.#signal }); | ||
| 93 | 97 | watcher.on('change', (eventType, fileName) => this | |
| 94 | 98 | .#onChange(recursive ? resolve(path, fileName) : path)); | |
| 95 | 99 | this.#watchers.set(path, { handle: watcher, recursive }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -117,4 +117,19 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { | |||
| 117 | 117 | assert.strictEqual(result[2], 'ok 1 - this should be skipped # SKIP test name does not match pattern\n'); | |
| 118 | 118 | assert.strictEqual(result[5], 'ok 2 - this should be executed\n'); | |
| 119 | 119 | }); | |
| 120 | + | ||
| 121 | + it('should stop watch mode when abortSignal aborts', async () => { | ||
| 122 | + const controller = new AbortController(); | ||
| 123 | + const result = await run({ files: [join(testFixtures, 'test/random.cjs')], watch: true, signal: controller.signal }) | ||
| 124 | + .compose(async function* (source) { | ||
| 125 | + for await (const chunk of source) { | ||
| 126 | + if (chunk.type === 'test:pass') { | ||
| 127 | + controller.abort(); | ||
| 128 | + yield chunk.data.name; | ||
| 129 | + } | ||
| 130 | + } | ||
| 131 | + }) | ||
| 132 | + .toArray(); | ||
| 133 | + assert.deepStrictEqual(result, ['this should pass']); | ||
| 134 | + }); | ||
| 120 | 135 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments