Skip to content

Commit 469de7f

Browse files
watch: added tests for worker in -- watch mode to include worker file and nested dependencies
1 parent 74e86f9 commit 469de7f

2 files changed

Lines changed: 235 additions & 68 deletions

File tree

test/parallel/test-watch-mode-worker.mjs

Lines changed: 0 additions & 67 deletions
This file was deleted.

test/sequential/test-watch-mode.mjs

Lines changed: 235 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,4 +943,238 @@ process.on('message', (message) => {
943943
await done();
944944
}
945945
});
946-
});
946+
947+
it('should watch changes to worker - cjs', async () => {
948+
const dir = tmpdir.resolve(`watch-worker-cjs-${Date.now()}`);
949+
mkdirSync(dir);
950+
951+
const worker = path.join(dir, 'worker.js');
952+
953+
writeFileSync(worker, `
954+
console.log("worker running");
955+
`);
956+
957+
const file = createTmpFile(`
958+
const { Worker } = require('node:worker_threads');
959+
960+
const w = new Worker(${JSON.stringify(worker)});
961+
w.on('exit', () => {
962+
console.log('running');
963+
});
964+
`, '.js', dir);
965+
966+
const { stderr, stdout } = await runWriteSucceed({
967+
file,
968+
watchedFile: worker,
969+
});
970+
971+
assert.strictEqual(stderr, '');
972+
assert.deepStrictEqual(stdout, [
973+
'worker running',
974+
'running',
975+
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
976+
`Restarting ${inspect(file)}`,
977+
'worker running',
978+
'running',
979+
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
980+
]);
981+
});
982+
983+
it('should watch changes to worker dependencies - cjs', async () => {
984+
const dir = tmpdir.resolve(`watch-worker-dep-cjs-${Date.now()}`);
985+
mkdirSync(dir);
986+
987+
const dep = path.join(dir, 'dep.js');
988+
const worker = path.join(dir, 'worker.js');
989+
990+
writeFileSync(dep, `
991+
module.exports = 'dep v1';
992+
`);
993+
994+
writeFileSync(worker, `
995+
const dep = require('./dep.js');
996+
console.log(dep);
997+
`);
998+
999+
const file = createTmpFile(`
1000+
const { Worker } = require('node:worker_threads');
1001+
1002+
const w = new Worker(${JSON.stringify(worker)});
1003+
w.on('exit', () => {
1004+
console.log('running');
1005+
});
1006+
`, '.js', dir);
1007+
1008+
const { stderr, stdout } = await runWriteSucceed({
1009+
file,
1010+
watchedFile: dep,
1011+
});
1012+
1013+
assert.strictEqual(stderr, '');
1014+
assert.deepStrictEqual(stdout, [
1015+
'dep v1',
1016+
'running',
1017+
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
1018+
`Restarting ${inspect(file)}`,
1019+
'dep v1',
1020+
'running',
1021+
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
1022+
]);
1023+
});
1024+
1025+
it('should watch changes to nested worker dependencies - cjs', async () => {
1026+
const dir = tmpdir.resolve(`watch-worker-dep-cjs-${Date.now()}`);
1027+
mkdirSync(dir);
1028+
1029+
const subDep = path.join(dir, 'sub-dep.js');
1030+
const dep = path.join(dir, 'dep.js');
1031+
const worker = path.join(dir, 'worker.js');
1032+
1033+
writeFileSync(subDep, `
1034+
module.exports = 'sub-dep v1';
1035+
`);
1036+
1037+
writeFileSync(dep, `
1038+
const subDep = require('./sub-dep.js');
1039+
console.log(subDep);
1040+
module.exports = 'dep v1';
1041+
`);
1042+
1043+
writeFileSync(worker, `
1044+
const dep = require('./dep.js');
1045+
`);
1046+
1047+
const file = createTmpFile(`
1048+
const { Worker } = require('node:worker_threads');
1049+
1050+
const w = new Worker(${JSON.stringify(worker)});
1051+
w.on('exit', () => {
1052+
console.log('running');
1053+
});
1054+
`, '.js', dir);
1055+
1056+
const { stderr, stdout } = await runWriteSucceed({
1057+
file,
1058+
watchedFile: subDep,
1059+
});
1060+
1061+
assert.strictEqual(stderr, '');
1062+
assert.deepStrictEqual(stdout, [
1063+
'sub-dep v1',
1064+
'running',
1065+
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
1066+
`Restarting ${inspect(file)}`,
1067+
'sub-dep v1',
1068+
'running',
1069+
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
1070+
]);
1071+
});
1072+
1073+
it('should watch changes to worker - esm', async () => {
1074+
const dir = tmpdir.resolve(`watch-worker-esm-${Date.now()}`);
1075+
mkdirSync(dir);
1076+
1077+
const worker = path.join(dir, 'worker.mjs');
1078+
1079+
writeFileSync(worker, `
1080+
console.log("worker running");
1081+
`);
1082+
1083+
const file = createTmpFile(`
1084+
import { Worker } from 'node:worker_threads';
1085+
new Worker(new URL(${JSON.stringify(pathToFileURL(worker))}));
1086+
`, '.mjs', dir);
1087+
1088+
const { stderr, stdout } = await runWriteSucceed({
1089+
file,
1090+
watchedFile: worker,
1091+
});
1092+
1093+
assert.strictEqual(stderr, '');
1094+
assert.deepStrictEqual(stdout, [
1095+
'worker running',
1096+
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
1097+
`Restarting ${inspect(file)}`,
1098+
'worker running',
1099+
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
1100+
]);
1101+
});
1102+
1103+
it('should watch changes to worker dependencies - esm', async () => {
1104+
const dir = tmpdir.resolve(`watch-worker-dep-esm-${Date.now()}`);
1105+
mkdirSync(dir);
1106+
1107+
const dep = path.join(dir, 'dep.mjs');
1108+
const worker = path.join(dir, 'worker.mjs');
1109+
1110+
writeFileSync(dep, `
1111+
export default 'dep v1';
1112+
`);
1113+
1114+
writeFileSync(worker, `
1115+
import dep from ${JSON.stringify(pathToFileURL(dep))};
1116+
console.log(dep);
1117+
`);
1118+
1119+
const file = createTmpFile(`
1120+
import { Worker } from 'node:worker_threads';
1121+
new Worker(new URL(${JSON.stringify(pathToFileURL(worker))}));
1122+
`, '.mjs', dir);
1123+
1124+
const { stderr, stdout } = await runWriteSucceed({
1125+
file,
1126+
watchedFile: dep,
1127+
});
1128+
1129+
assert.strictEqual(stderr, '');
1130+
assert.deepStrictEqual(stdout, [
1131+
'dep v1',
1132+
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
1133+
`Restarting ${inspect(file)}`,
1134+
'dep v1',
1135+
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
1136+
]);
1137+
});
1138+
1139+
it('should watch changes to nested worker dependencies - esm', async () => {
1140+
const dir = tmpdir.resolve(`watch-worker-dep-esm-${Date.now()}`);
1141+
mkdirSync(dir);
1142+
1143+
const subDep = path.join(dir, 'sub-dep.mjs');
1144+
const dep = path.join(dir, 'dep.mjs');
1145+
const worker = path.join(dir, 'worker.mjs');
1146+
1147+
writeFileSync(subDep, `
1148+
export default 'sub-dep v1';
1149+
`);
1150+
1151+
writeFileSync(dep, `
1152+
import subDep from ${JSON.stringify(pathToFileURL(subDep))};
1153+
console.log(subDep);
1154+
export default 'dep v1';
1155+
`);
1156+
1157+
writeFileSync(worker, `
1158+
import dep from ${JSON.stringify(pathToFileURL(dep))};
1159+
`);
1160+
1161+
const file = createTmpFile(`
1162+
import { Worker } from 'node:worker_threads';
1163+
new Worker(new URL(${JSON.stringify(pathToFileURL(worker))}));
1164+
`, '.mjs', dir);
1165+
1166+
const { stderr, stdout } = await runWriteSucceed({
1167+
file,
1168+
watchedFile: subDep,
1169+
});
1170+
1171+
assert.strictEqual(stderr, '');
1172+
assert.deepStrictEqual(stdout, [
1173+
'sub-dep v1',
1174+
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
1175+
`Restarting ${inspect(file)}`,
1176+
'sub-dep v1',
1177+
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
1178+
]);
1179+
});
1180+
});

0 commit comments

Comments
 (0)