Skip to content
Open
Changes from all commits
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
21 changes: 16 additions & 5 deletions test/sequential/test-watch-mode-worker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import path from 'node:path';
import { execPath } from 'node:process';
import { describe, it } from 'node:test';
import { spawn } from 'node:child_process';
import { writeFileSync, readFileSync } from 'node:fs';
import { appendFileSync, mkdirSync, writeFileSync } from 'node:fs';
import { inspect } from 'node:util';
import { pathToFileURL } from 'node:url';
import { createInterface } from 'node:readline';

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

function restart(file, content = readFileSync(file)) {
writeFileSync(file, content);
const timer = setInterval(() => writeFileSync(file, content), common.platformTimeout(250));
function restart(file) {
appendFileSync(file, '\n');
const timer = setInterval(() => appendFileSync(file, '\n'), common.platformTimeout(250));
return () => clearInterval(timer);
}

Expand All @@ -26,6 +26,12 @@ function createTmpFile(content = 'console.log(\'running\');', ext = '.js', basen
return file;
}

function createTmpDir() {
const dir = path.join(tmpdir.path, `${tmpFiles++}`);
mkdirSync(dir);
return dir;
}

async function runWriteSucceed({
file,
watchedFile,
Expand Down Expand Up @@ -78,10 +84,10 @@ async function runWriteSucceed({
}

tmpdir.refresh();
const dir = tmpdir.path;

describe('watch mode', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_000 }, () => {
it('should watch changes to worker - cjs', async () => {
const dir = createTmpDir();
const worker = path.join(dir, 'worker.js');

writeFileSync(worker, `
Expand Down Expand Up @@ -109,6 +115,7 @@ const w = new Worker(${JSON.stringify(worker)});
});

it('should watch changes to worker dependencies - cjs', async () => {
const dir = createTmpDir();
const dep = path.join(dir, 'dep.js');
const worker = path.join(dir, 'worker.js');

Expand Down Expand Up @@ -142,6 +149,7 @@ const w = new Worker(${JSON.stringify(worker)});
});

it('should watch changes to nested worker dependencies - cjs', async () => {
const dir = createTmpDir();
const subDep = path.join(dir, 'sub-dep.js');
const dep = path.join(dir, 'dep.js');
const worker = path.join(dir, 'worker.js');
Expand Down Expand Up @@ -181,6 +189,7 @@ const w = new Worker(${JSON.stringify(worker)});
});

it('should watch changes to worker - esm', async () => {
const dir = createTmpDir();
const worker = path.join(dir, 'worker.mjs');

writeFileSync(worker, `
Expand Down Expand Up @@ -208,6 +217,7 @@ new Worker(new URL(${JSON.stringify(pathToFileURL(worker))}));
});

it('should watch changes to worker dependencies - esm', async () => {
const dir = createTmpDir();
const dep = path.join(dir, 'dep.mjs');
const worker = path.join(dir, 'worker.mjs');

Expand Down Expand Up @@ -241,6 +251,7 @@ new Worker(new URL(${JSON.stringify(pathToFileURL(worker))}));
});

it('should watch changes to nested worker dependencies - esm', async () => {
const dir = createTmpDir();
const subDep = path.join(dir, 'sub-dep.mjs');
const dep = path.join(dir, 'dep.mjs');
const worker = path.join(dir, 'worker.mjs');
Expand Down
Loading