Best JavaScript code snippet using stryker-parent
child_process.ts
Source:child_process.ts
1import * as childProcess from 'node:child_process';2import * as net from 'node:net';3import * as fs from 'node:fs';4import assert = require('node:assert');5import { promisify } from 'node:util';6import { Writable, Readable, Pipe } from 'node:stream';7import { URL } from 'node:url';8{9 childProcess.exec("echo test");10 const abortController = new AbortController();11 childProcess.exec("echo test", { windowsHide: true, signal: abortController.signal });12 childProcess.spawn("echo");13 childProcess.spawn("echo", { windowsHide: true, signal: new AbortSignal(), killSignal: "SIGABRT", timeout: 123 });14 childProcess.spawn("echo", ["test"], { windowsHide: true });15 childProcess.spawn("echo", ["test"], { windowsHide: true, argv0: "echo-test" });16 childProcess.spawn("echo", ["test"], { stdio: [0xdeadbeef, "inherit", undefined, "pipe"] });17 childProcess.spawnSync("echo test");18 childProcess.spawnSync("echo test", {windowsVerbatimArguments: false});19 childProcess.spawnSync("echo test", {windowsVerbatimArguments: false, argv0: "echo-test"});20 childProcess.spawnSync("echo test", {input: new Uint8Array([])});21 childProcess.spawnSync("echo test", {input: new DataView(new ArrayBuffer(1))});22 childProcess.spawnSync("echo test", { encoding: 'utf-8' });23 childProcess.spawnSync("echo test", { encoding: 'buffer' });24 childProcess.spawnSync("echo test", { cwd: new URL('file://aaaaaaaa')});25 childProcess.spawnSync("echo test").output; // $ExpectType (Buffer | null)[]26 childProcess.spawnSync("echo test", {}).output; // $ExpectType (Buffer | null)[]27 childProcess.spawnSync("echo test", { encoding: 'buffer' }).output; // $ExpectType (Buffer | null)[]28 childProcess.spawnSync("echo test", { encoding: 'utf-8' }).output; // $ExpectType (string | null)[]29 childProcess.spawnSync("echo", ['test']).output; // $ExpectType (Buffer | null)[]30 childProcess.spawnSync("echo test", ['test'], {}).output; // $ExpectType (Buffer | null)[]31 childProcess.spawnSync("echo test", ['test'], { encoding: 'buffer' }).output; // $ExpectType (Buffer | null)[]32 childProcess.spawnSync("echo test", ['test'], { encoding: 'utf-8' }).output; // $ExpectType (string | null)[]33 ((opts?: childProcess.SpawnSyncOptions) => childProcess.spawnSync("echo test", opts))().output; // $ExpectType (string | Buffer | null)[]34}35{36 childProcess.execSync("echo test"); // $ExpectType Buffer37 childProcess.execSync("echo test", {}); // $ExpectType Buffer38 childProcess.execSync("echo test", { encoding: 'buffer' }); // $ExpectType Buffer39 childProcess.execSync("echo test", { encoding: 'utf-8' }); // $ExpectType string40 ((opts?: childProcess.ExecSyncOptions) => childProcess.execSync('echo test', opts))(); // $ExpectType string | Buffer41 childProcess.execSync("git status", { // $ExpectType string42 cwd: 'test',43 input: 'test',44 stdio: 'pipe',45 env: {},46 shell: 'hurr',47 uid: 1,48 gid: 1,49 timeout: 123,50 killSignal: 1,51 maxBuffer: 123,52 encoding: "utf8",53 windowsHide: true54 });55}56{57 childProcess.execFile("npm", () => {});58 childProcess.execFile("npm", { windowsHide: true, signal: new AbortSignal(), }, () => {});59 childProcess.execFile("npm", { shell: true }, () => {});60 childProcess.execFile("npm", { shell: '/bin/sh' }, () => {});61 childProcess.execFile("npm", ["-v"] as ReadonlyArray<string>, () => {});62 childProcess.execFile("npm", ["-v"] as ReadonlyArray<string>, { windowsHide: true, encoding: 'utf-8' }, (stdout, stderr) => { assert(stdout instanceof String); });63 childProcess.execFile("npm", ["-v"] as ReadonlyArray<string>, { windowsHide: true, encoding: 'buffer' }, (stdout, stderr) => { assert(stdout instanceof Buffer); });64 childProcess.execFile("npm", { encoding: 'utf-8' }, (stdout, stderr) => { assert(stdout instanceof String); });65 childProcess.execFile("npm", { encoding: 'buffer' }, (stdout, stderr) => { assert(stdout instanceof Buffer); });66 childProcess.execFile("npm", (err) => {67 if (err && err.errno) assert(err.code === 'ENOENT');68 });69}70{71 const y: childProcess.ChildProcess = childProcess.spawn('echo', ['test']);72 const x = y instanceof childProcess.ChildProcess;73}74{75 childProcess.execFileSync("echo test", {input: new Uint8Array([])});76 childProcess.execFileSync("echo test", {input: new DataView(new ArrayBuffer(1))});77 childProcess.execFileSync("echo test"); // $ExpectType Buffer78 childProcess.execFileSync("echo test", {}); // $ExpectType Buffer79 childProcess.execFileSync("echo test", {encoding: 'buffer'}); // $ExpectType Buffer80 childProcess.execFileSync("echo test", {encoding: 'utf8'}); // $ExpectType string81 childProcess.execFileSync("echo test", ['test']); // $ExpectType Buffer82 childProcess.execFileSync("echo test", ['test'], {}); // $ExpectType Buffer83 childProcess.execFileSync("echo test", ['test'], {encoding: 'buffer'}); // $ExpectType Buffer84 childProcess.execFileSync("echo test", ['test'], {encoding: 'utf8'}); // $ExpectType string85 ((opts?: childProcess.ExecFileSyncOptions) => childProcess.execFileSync('echo test', ['args'], opts))(); // $ExpectType string | Buffer86}87{88 const forked = childProcess.fork('./', ['asd'] as ReadonlyArray<string>, {89 windowsVerbatimArguments: true,90 silent: false,91 stdio: "inherit",92 execPath: '',93 execArgv: ['asda'],94 signal: new AbortSignal(),95 killSignal: "SIGABRT",96 timeout: 123,97 });98 const ipc: Pipe = forked.channel!;99 const hasRef: boolean = ipc.hasRef();100 ipc.close();101 ipc.unref();102 ipc.ref();103}104{105 const forked = childProcess.fork('./', {106 windowsVerbatimArguments: true,107 silent: false,108 stdio: ["inherit"],109 execPath: '',110 execArgv: ['asda']111 });112}113{114 const forked = childProcess.fork('./');115}116async function testPromisify() {117 const execFile = promisify(childProcess.execFile);118 let r: { stdout: string | Buffer, stderr: string | Buffer } = await execFile("npm");119 r = await execFile("npm", ["-v"] as ReadonlyArray<string>);120 r = await execFile("npm", ["-v"] as ReadonlyArray<string>, { encoding: 'utf-8' });121 r = await execFile("npm", ["-v"] as ReadonlyArray<string>, { encoding: 'buffer' });122 r = await execFile("npm", { encoding: 'utf-8' });123 r = await execFile("npm", { encoding: 'buffer' });124 const prom: childProcess.PromiseWithChild<{ stdout: string, stderr: string }> = execFile('test');125 prom.child;126}127{128 let cp = childProcess.fork('asd');129 const _socket: net.Socket = net.createConnection(1);130 const _server: net.Server = net.createServer();131 let _boolean: boolean;132 let _string: string;133 let _stringArray: string[];134 let _maybeNumber: number | null;135 let _maybeSignal: NodeJS.Signals | null;136 _boolean = cp.send(1);137 _boolean = cp.send('one');138 _boolean = cp.send({139 type: 'test'140 });141 _boolean = cp.send(1, (error) => {142 const _err: Error | null = error;143 });144 _boolean = cp.send('one', (error) => {145 const _err: Error | null = error;146 });147 _boolean = cp.send({148 type: 'test'149 }, (error) => {150 const _err: Error | null = error;151 });152 _boolean = cp.send(1, _socket);153 _boolean = cp.send('one', _socket);154 _boolean = cp.send({155 type: 'test'156 }, _socket);157 _boolean = cp.send(1, _socket, (error) => {158 const _err: Error | null = error;159 });160 _boolean = cp.send('one', _socket, (error) => {161 const _err: Error | null = error;162 });163 _boolean = cp.send({164 type: 'test'165 }, _socket, (error) => {166 const _err: Error | null = error;167 });168 _boolean = cp.send(1, _socket, {169 keepOpen: true170 });171 _boolean = cp.send('one', _socket, {172 keepOpen: true173 });174 _boolean = cp.send({175 type: 'test'176 }, _socket, {177 keepOpen: true178 });179 _boolean = cp.send(1, _socket, {180 keepOpen: true181 }, (error) => {182 const _err: Error | null = error;183 });184 _boolean = cp.send('one', _socket, {185 keepOpen: true186 }, (error) => {187 const _err: Error | null = error;188 });189 _boolean = cp.send({190 type: 'test'191 }, _socket, {192 keepOpen: true193 }, (error) => {194 const _err: Error | null = error;195 });196 _boolean = cp.send(1, _server);197 _boolean = cp.send('one', _server);198 _boolean = cp.send({199 type: 'test'200 }, _server);201 _boolean = cp.send(1, _server, (error) => {202 const _err: Error | null = error;203 });204 _boolean = cp.send('one', _server, (error) => {205 const _err: Error | null = error;206 });207 _boolean = cp.send({208 type: 'test'209 }, _server, (error) => {210 const _err: Error | null = error;211 });212 _boolean = cp.send(1, _server, {213 keepOpen: true214 });215 _boolean = cp.send('one', _server, {216 keepOpen: true217 });218 _boolean = cp.send({219 type: 'test'220 }, _server, {221 keepOpen: true222 });223 _boolean = cp.send(1, _server, {224 keepOpen: true225 }, (error) => {226 const _err: Error | null = error;227 });228 _boolean = cp.send('one', _server, {229 keepOpen: true230 }, (error) => {231 const _err: Error | null = error;232 });233 _boolean = cp.send({234 type: 'test'235 }, _server, {236 keepOpen: true237 }, (error) => {238 const _err: Error | null = error;239 });240 const stdin: Writable | null = cp.stdio[0];241 const stdout: Readable | null = cp.stdio[1];242 const stderr: Readable | null = cp.stdio[2];243 const fd4: Readable | Writable | null = cp.stdio[3]!;244 const fd5: Readable | Writable | null = cp.stdio[4]!;245 cp = cp.addListener("close", (code, signal) => {246 const _code: number | null = code;247 const _signal: NodeJS.Signals | null = signal;248 });249 cp = cp.addListener("disconnect", () => { });250 cp = cp.addListener("error", (err) => {251 const _err: Error = err;252 });253 cp = cp.addListener("exit", (code, signal) => {254 const _code: number | null = code;255 const _signal: NodeJS.Signals | null = signal;256 });257 cp = cp.addListener("message", (message, sendHandle) => {258 const _message: any = message;259 const _sendHandle: net.Socket | net.Server = sendHandle;260 });261 cp = cp.addListener("spawn", () => {262 });263 _boolean = cp.emit("close", () => { });264 _boolean = cp.emit("disconnect", () => { });265 _boolean = cp.emit("error", () => { });266 _boolean = cp.emit("exit", () => { });267 _boolean = cp.emit("message", () => { });268 _boolean = cp.emit("spawn", () => { });269 cp = cp.on("close", (code, signal) => {270 const _code: number | null = code;271 const _signal: NodeJS.Signals | null = signal;272 });273 cp = cp.on("disconnect", () => { });274 cp = cp.on("error", (err) => {275 const _err: Error = err;276 });277 cp = cp.on("exit", (code, signal) => {278 const _code: number | null = code;279 const _signal: NodeJS.Signals | null = signal;280 });281 cp = cp.on("message", (message, sendHandle) => {282 const _message: any = message;283 const _sendHandle: net.Socket | net.Server = sendHandle;284 });285 cp = cp.once("close", (code, signal) => {286 const _code: number | null = code;287 const _signal: NodeJS.Signals | null = signal;288 });289 cp = cp.once("disconnect", () => { });290 cp = cp.once("error", (err) => {291 const _err: Error = err;292 });293 cp = cp.once("exit", (code, signal) => {294 const _code: number | null = code;295 const _signal: NodeJS.Signals | null = signal;296 });297 cp = cp.once("message", (message, sendHandle) => {298 const _message: any = message;299 const _sendHandle: net.Socket | net.Server = sendHandle;300 });301 cp = cp.prependListener("close", (code, signal) => {302 const _code: number | null = code;303 const _signal: NodeJS.Signals | null = signal;304 });305 cp = cp.prependListener("disconnect", () => { });306 cp = cp.prependListener("error", (err) => {307 const _err: Error = err;308 });309 cp = cp.prependListener("exit", (code, signal) => {310 const _code: number | null = code;311 const _signal: NodeJS.Signals | null = signal;312 });313 cp = cp.prependListener("message", (message, sendHandle) => {314 const _message: any = message;315 const _sendHandle: net.Socket | net.Server = sendHandle;316 });317 cp = cp.prependOnceListener("close", (code, signal) => {318 const _code: number | null = code;319 const _signal: NodeJS.Signals | null = signal;320 });321 cp = cp.prependOnceListener("disconnect", () => { });322 cp = cp.prependOnceListener("error", (err) => {323 const _err: Error = err;324 });325 cp = cp.prependOnceListener("exit", (code, signal) => {326 const _code: number | null = code;327 const _signal: NodeJS.Signals | null = signal;328 });329 cp = cp.prependOnceListener("message", (message, sendHandle) => {330 const _message: any = message;331 const _sendHandle: net.Socket | net.Server = sendHandle;332 });333 _boolean = cp.kill();334 _boolean = cp.kill(9);335 _boolean = cp.kill("SIGTERM");336 _maybeNumber = cp.exitCode;337 _maybeSignal = cp.signalCode;338 _string = cp.spawnfile;339 _stringArray = cp.spawnargs;340 function expectNonNull(cp: {341 readonly stdin: Writable;342 readonly stdout: Readable;343 readonly stderr: Readable;344 readonly stdio: [345 Writable,346 Readable,347 Readable,348 any,349 any350 ];351 }): void {352 return undefined;353 }354 expectNonNull(childProcess.spawn('command'));355 expectNonNull(childProcess.spawn('command', {}));356 expectNonNull(childProcess.spawn('command', { stdio: undefined }));357 expectNonNull(childProcess.spawn('command', { stdio: 'pipe' }));358 expectNonNull(childProcess.spawn('command', { stdio: [undefined, undefined, undefined] }));359 expectNonNull(childProcess.spawn('command', { stdio: [null, null, null] }));360 expectNonNull(childProcess.spawn('command', { stdio: 'overlapped' }));361 expectNonNull(childProcess.spawn('command', { stdio: ['overlapped', 'overlapped', 'overlapped'] }));362 expectNonNull(childProcess.spawn('command', { stdio: ['pipe', 'pipe', 'pipe'] }));363 expectNonNull(childProcess.spawn('command', { stdio: ['pipe', 'pipe', 'pipe'] }));364 expectNonNull(childProcess.spawn('command', ['a', 'b', 'c']));365 expectNonNull(childProcess.spawn('command', ['a', 'b', 'c'], {}));366 expectNonNull(childProcess.spawn('command', ['a', 'b', 'c'], { stdio: undefined }));367 expectNonNull(childProcess.spawn('command', ['a', 'b', 'c'], { stdio: 'pipe' }));368 expectNonNull(childProcess.spawn('command', ['a', 'b', 'c'], { stdio: [undefined, undefined, undefined] }));369 expectNonNull(childProcess.spawn('command', ['a', 'b', 'c'], { stdio: [null, null, null] }));370 expectNonNull(childProcess.spawn('command', ['a', 'b', 'c'], { stdio: ['pipe', 'pipe', 'pipe'] }));371 function expectStdio<Stdin, Stdout, Stderr>(...cps: Array<{372 stdin: Stdin,373 stdout: Stdout,374 stderr: Stderr,375 stdio: [Stdin, Stdout, Stderr, any, any]376 }>): void {377 return undefined;378 }379 expectStdio<Writable, Readable, Readable>(380 childProcess.spawn('command', { stdio: ['pipe', 'pipe', 'pipe'] }),381 childProcess.spawn('command', { stdio: [null, null, null] }),382 childProcess.spawn('command', { stdio: [undefined, undefined, undefined] }),383 childProcess.spawn('command', { stdio: ['pipe', null, undefined] }),384 );385 expectStdio<Writable, Readable, null>(386 childProcess.spawn('command', { stdio: ['pipe', 'pipe', 'ignore'] }),387 childProcess.spawn('command', { stdio: [null, null, 'inherit'] }),388 childProcess.spawn('command', { stdio: [undefined, undefined, process.stdout] }),389 childProcess.spawn('command', { stdio: ['pipe', null, process.stderr] }),390 );391 expectStdio<null, null, null>(392 childProcess.spawn('command', { stdio: ['ignore', 'ignore', 'ignore'] }),393 childProcess.spawn('command', { stdio: ['inherit', 'inherit', 'inherit'] }),394 childProcess.spawn('command', { stdio: [process.stdin, process.stdout, process.stderr] }),395 childProcess.spawn('command', { stdio: ['ignore', 'inherit', process.stderr] }),396 );397 expectStdio<Writable, Readable, Readable>(398 childProcess.spawn('command', ['a', 'b', 'c'], { stdio: ['pipe', 'pipe', 'pipe'] }),399 childProcess.spawn('command', ['a', 'b', 'c'], { stdio: [null, null, null] }),400 childProcess.spawn('command', ['a', 'b', 'c'], { stdio: [undefined, undefined, undefined] }),401 childProcess.spawn('command', ['a', 'b', 'c'], { stdio: ['pipe', null, undefined] }),402 );403 expectStdio<Writable, Readable, null>(404 childProcess.spawn('command', ['a', 'b', 'c'], { stdio: ['pipe', 'pipe', 'ignore'] }),405 childProcess.spawn('command', ['a', 'b', 'c'], { stdio: [null, null, 'inherit'] }),406 childProcess.spawn('command', ['a', 'b', 'c'], { stdio: [undefined, undefined, process.stdout] }),407 childProcess.spawn('command', ['a', 'b', 'c'], { stdio: ['pipe', null, process.stderr] }),408 );409 expectStdio<null, null, null>(410 childProcess.spawn('command', ['a', 'b', 'c'], { stdio: ['ignore', 'ignore', 'ignore'] }),411 childProcess.spawn('command', ['a', 'b', 'c'], { stdio: ['inherit', 'inherit', 'inherit'] }),412 childProcess.spawn('command', ['a', 'b', 'c'], { stdio: [process.stdin, process.stdout, process.stderr] }),413 childProcess.spawn('command', ['a', 'b', 'c'], { stdio: ['ignore', 'inherit', process.stderr] }),414 );415 function expectChildProcess(cp: childProcess.ChildProcess): void {416 return undefined;417 }418 expectChildProcess(childProcess.spawn('command'));419 expectChildProcess(childProcess.spawn('command', {}));420 expectChildProcess(childProcess.spawn('command', { stdio: undefined }));421 expectChildProcess(childProcess.spawn('command', { stdio: 'pipe' }));422 expectChildProcess(childProcess.spawn('command', { stdio: [undefined, undefined, undefined] }));423 expectChildProcess(childProcess.spawn('command', { stdio: [null, null, null] }));424 expectChildProcess(childProcess.spawn('command', { stdio: ['pipe', 'pipe', 'pipe'] }));425 expectChildProcess(childProcess.spawn('command', ['a', 'b', 'c']));426 expectChildProcess(childProcess.spawn('command', ['a', 'b', 'c'], {}));427 expectChildProcess(childProcess.spawn('command', ['a', 'b', 'c'], { stdio: undefined }));428 expectChildProcess(childProcess.spawn('command', ['a', 'b', 'c'], { stdio: 'pipe' }));429 expectChildProcess(childProcess.spawn('command', ['a', 'b', 'c'], { stdio: [undefined, undefined, undefined] }));430 expectChildProcess(childProcess.spawn('command', ['a', 'b', 'c'], { stdio: [null, null, null] }));431 expectChildProcess(childProcess.spawn('command', ['a', 'b', 'c'], { stdio: ['pipe', 'pipe', 'pipe'] }));432}433{434 process.stdin.setEncoding('utf8');435 process.stdin.on('readable', () => {436 const chunk = process.stdin.read();437 if (chunk !== null) {438 process.stdout.write(`data: ${chunk}`);439 }440 });441 process.stdin.on('end', () => {442 process.stdout.write('end');443 });444 process.stdin.pipe(process.stdout);445 console.log(process.stdin.isTTY);446 console.log(process.stdout.isTTY);447 console.log(process.stdin instanceof net.Socket);448 console.log(process.stdout instanceof fs.ReadStream);449 const stdin: NodeJS.ReadableStream = process.stdin;450 console.log(stdin instanceof net.Socket);451 console.log(stdin instanceof fs.ReadStream);452 const stdout: NodeJS.WritableStream = process.stdout;453 console.log(stdout instanceof net.Socket);454 console.log(stdout instanceof fs.WriteStream);...
cluster.mocha.js
Source:cluster.mocha.js
1'use strict';2var inspector = require('../index')();3var cluster = require('cluster');4var fork = require('child_process').fork;5var exec = require('child_process').exec;6var assert = require('assert');7var utils = require('./utils');8var path = require('path');9describe('Child process', function () {10 it('should get dump after cluster/worker init', function (done) {11 cluster.setupMaster({12 exec: './test/fixtures/worker.js'13 });14 var worker = cluster.fork();15 var pid = null;16 worker.on('online', function () {17 var dump = inspector.dump();18 utils.testCommon(dump);19 assert.strictEqual(dump.handles.hasOwnProperty('ChildProcess'), true);20 assert.strictEqual(dump.handles.ChildProcess[0].connected, true);21 assert.strictEqual(dump.handles.ChildProcess[0].killed, false);22 assert.strictEqual(dump.handles.ChildProcess[0].hasOwnProperty('pid'), true);23 assert.strictEqual(Number.isInteger(dump.handles.ChildProcess[0].pid), true);24 var argsLength = dump.handles.ChildProcess[0].args.length;25 assert.strictEqual(argsLength >= 3, true);26 assert.strictEqual(dump.handles.ChildProcess[0].args[argsLength - 2], './test/fixtures/worker.js');27 assert.strictEqual(dump.handles.ChildProcess[0].args[argsLength - 1], './test/**/*.mocha.js');28 assert.strictEqual(dump.handles.ChildProcess[0].hasOwnProperty('spawnfile'), true);29 assert.strictEqual(dump.handles.ChildProcess[0].spawnfile, dump.handles.ChildProcess[0].args[0]);30 pid = dump.handles.ChildProcess[0].pid;31 });32 worker.on('disconnect', function () {33 var dump = inspector.dump();34 utils.testCommon(dump);35 assert.strictEqual(dump.handles.hasOwnProperty('ChildProcess'), true);36 assert.strictEqual(dump.handles.ChildProcess[0].connected, false);37 assert.strictEqual(dump.handles.ChildProcess[0].hasOwnProperty('pid'), true);38 assert.strictEqual(dump.handles.ChildProcess[0].pid, pid);39 assert.strictEqual(dump.handles.ChildProcess[0].killed, false);40 worker.kill();41 });42 worker.on('exit', function () {43 var dump = inspector.dump();44 utils.testCommon(dump);45 assert.strictEqual(dump.handles.hasOwnProperty('ChildProcess'), true);46 assert.strictEqual(dump.handles.ChildProcess[0].connected, false);47 assert.strictEqual(dump.handles.ChildProcess[0].hasOwnProperty('pid'), true);48 assert.strictEqual(dump.handles.ChildProcess[0].pid, pid);49 assert.strictEqual(dump.handles.ChildProcess[0].killed, true);50 done();51 });52 });53 it('should get dump after fork', function (done) {54 const child = fork('./test/fixtures/childFork.js');55 child.kill('SIGINT');56 child.on('exit', function () {57 var dump = inspector.dump();58 utils.testCommon(dump);59 assert.strictEqual(dump.handles.hasOwnProperty('ChildProcess'), true);60 assert.strictEqual(dump.handles.ChildProcess[0].connected, false);61 assert.strictEqual(dump.handles.ChildProcess[0].hasOwnProperty('pid'), true);62 assert.strictEqual(dump.handles.ChildProcess[0].killed, true);63 done();64 });65 });66 it('should get dump after exec', function (done) {67 const child = exec('./test/fixtures/childFork.js');68 child.kill('SIGINT');69 child.on('exit', function () {70 var dump = inspector.dump();71 utils.testCommon(dump);72 assert.strictEqual(dump.handles.hasOwnProperty('ChildProcess'), true);73 assert.strictEqual(dump.handles.ChildProcess[0].connected, false);74 assert.strictEqual(dump.handles.ChildProcess[0].hasOwnProperty('pid'), true);75 assert.strictEqual(dump.handles.ChildProcess[0].args[2], './test/fixtures/childFork.js');76 assert.strictEqual(dump.handles.ChildProcess[0].killed, true);77 done();78 });79 });80});81if (require('semver').satisfies(process.version, '>= 10.5.0')) {82 const {83 Worker, MessageChannel84 } = require('worker_threads');85 describe('Threads', function () {86 it('should get dump after worker/thread', function (done) {87 const worker = new Worker(path.join(__dirname, '/fixtures/childFork.js'));88 const subChannel = new MessageChannel();89 worker.postMessage({ hereIsYourPort: subChannel.port1 }, [subChannel.port1]);90 subChannel.port2.on('message', (value) => {91 assert.strictEqual(value, 'the worker is sending this');92 const dump = inspector.dump();93 assert.strictEqual(dump.handles.hasOwnProperty('MessagePort'), true);94 assert.strictEqual(dump.handles.MessagePort[0].listeners.length, 3);95 assert.strictEqual(dump.handles.MessagePort[0].listeners[2], 'message');96 subChannel.port2.close();97 worker.terminate();98 });99 worker.on('online', function () {100 const dump = inspector.dump();101 assert.strictEqual(dump.handles.hasOwnProperty('MessagePort'), true);102 });103 worker.on('exit', function () {104 // check MessagePort is cleaned after a certain time105 const timer = setInterval(function () {106 const dump = inspector.dump();107 if (!dump.handles.hasOwnProperty('MessagePort') || dump.handles.MessagePort.length === 0) {108 clearInterval(timer);109 done();110 }111 }, 200);112 });113 });114 });...
spawn-child-from-source.ts
Source:spawn-child-from-source.ts
1import {2 ChildProcess,3 Serializable,4 spawn,5 SpawnOptions,6 StdioNull,7 StdioPipe8} from 'child_process';9import { once } from 'events';10export async function kill(11 childProcess: ChildProcess,12 code: NodeJS.Signals | number = 'SIGTERM'13) {14 childProcess.kill(code);15 if (childProcess.exitCode === null && childProcess.signalCode === null) {16 await once(childProcess, 'exit');17 }18}19export default function spawnChildFromSource(20 src: string,21 spawnOptions: Omit<SpawnOptions, 'stdio'> = {},22 timeoutMs?: number,23 _stdout: StdioNull | StdioPipe = 'inherit',24 _stderr: StdioNull | StdioPipe = 'inherit'25): Promise<ChildProcess> {26 return new Promise(async(resolve, reject) => {27 const readyToken = Date.now().toString(32);28 const childProcess = spawn(process.execPath, {29 stdio: ['pipe', _stdout, _stderr, 'ipc'],30 ...spawnOptions31 });32 if (!childProcess.stdin) {33 await kill(childProcess);34 return reject(35 new Error("Can't write src to the spawned process, missing stdin")36 );37 }38 // eslint-disable-next-line prefer-const39 let timeoutId: NodeJS.Timeout | null;40 function cleanupListeners() {41 if (timeoutId) {42 clearTimeout(timeoutId);43 }44 if (childProcess.stdin) {45 childProcess.stdin.off('error', onWriteError);46 }47 childProcess.off('message', onMessage);48 childProcess.off('exit', onExit);49 }50 function onExit(exitCode: number | null) {51 if (exitCode && exitCode > 0) {52 cleanupListeners();53 reject(new Error('Child process exited with error before starting'));54 }55 }56 /* really hard to reproduce in tests and coverage is not happy */57 /* istanbul ignore next */58 async function onWriteError(error: Error) {59 cleanupListeners();60 await kill(childProcess);61 reject(error);62 }63 async function onTimeout() {64 cleanupListeners();65 await kill(childProcess);66 reject(new Error('Timed out while waiting for child process to start'));67 }68 function onMessage(data: Serializable) {69 if (data === readyToken) {70 cleanupListeners();71 resolve(childProcess);72 }73 }74 childProcess.on('message', onMessage);75 childProcess.on('exit', onExit);76 childProcess.stdin.on('error', onWriteError);77 childProcess.stdin.write(src);78 childProcess.stdin.write(`;process.send(${JSON.stringify(readyToken)})`);79 childProcess.stdin.end();80 timeoutId =81 timeoutMs !== undefined ? setTimeout(onTimeout, timeoutMs) : null;82 });...
Using AI Code Generation
1var childProcess = require('child_process');2var child = childProcess.fork('child.js');3child.on('message', function(m) {4 console.log('PARENT got message:', m);5});6child.send({ hello: 'world' });7process.on('message', function(m) {8 console.log('CHILD got message:', m);9});10process.send({ foo: 'bar' });11var childProcess = require('child_process');12var child = childProcess.fork('child.js');13child.on('message', function(m) {14 console.log('PARENT got message:', m);15});16child.send({ hello: 'world' });17process.on('message', function(m) {18 console.log('CHILD got message:', m);19});20process.send({ foo: 'bar' });21var childProcess = require('child_process');22var child = childProcess.fork('child.js');23child.on('message', function(m) {24 console.log('PARENT got message:', m);25});26child.send({ hello: 'world' });27process.on('message', function(m) {28 console.log('CHILD got message:', m);29});30process.send({ foo: 'bar' });31var childProcess = require('child_process');32var child = childProcess.fork('child.js');33child.on('message', function(m) {34 console.log('PARENT got message:', m);35});36child.send({ hello: 'world' });37process.on('message', function(m) {38 console.log('CHILD got message:', m);39});40process.send({ foo: 'bar' });41var childProcess = require('child_process');42var child = childProcess.fork('child.js');43child.on('message', function(m) {44 console.log('PARENT got message:', m);45});46child.send({ hello: 'world' });47process.on('message', function(m) {48 console.log('CHILD got message:', m);49});50process.send({ foo: 'bar' });51var childProcess = require('child_process');52var child = childProcess.fork('child.js');53child.on('message', function(m) {54 console.log('PARENT got message:', m);55});56child.send({
Using AI Code Generation
1const childProcess = require('stryker-parent').childProcess;2const child = childProcess.fork('./child.js');3child.on('message', function (m) {4 console.log('PARENT got message:', m);5});6child.send({ hello: 'world' });7const childProcess = require('stryker-parent').childProcess;8process.on('message', function (m) {9 console.log('CHILD got message:', m);10});11process.send({ foo: 'bar' });12PARENT got message: { foo: 'bar' }13CHILD got message: { hello: 'world' }
Using AI Code Generation
1const childProcess = require('child_process');2childProcess.exec('stryker run', (error, stdout, stderr) => { 3 if (error) {4 console.error(`exec error: ${error}`);5 return;6 }7 console.log(`stdout: ${stdout}`);8 console.log(`stderr: ${stderr}`);9});10"scripts": { 11}
Using AI Code Generation
1var childProcess = require('stryker-parent').childProcess;2var child = childProcess.fork('child.js');3child.send('hello world');4child.on('message', function (msg) {5 console.log('message from child: ' + msg);6});7var childProcess = require('stryker-parent').childProcess;8process.on('message', function (msg) {9 console.log('message from parent: ' + msg);10 process.send('hello back to you');11});12var childProcess = require('stryker-parent').childProcess;13childProcess.exec('npm install').then(function () {14 console.log('done');15});
Using AI Code Generation
1var childProcess = require('child_process');2var childProcess = require('child_process');3var child = childProcess.fork('./child.js', [], {4});5child.on('message', function (message) {6 console.log('Message from child', message);7});8child.send({ hello: 'world' });9process.on('message', function (message) {10 console.log('Message from parent:', message);11 process.send({ foo: 'bar' });12});13var childProcess = require('child_process');14var child = childProcess.fork('./child.js', [], {15});16child.on('message', function (message) {17 console.log('Message from child', message);18});19child.send({ hello: 'world' });20process.on('message', function (message) {21 console.log('Message from parent:', message);22 process.send({ foo: 'bar' });23});24var childProcess = require('child_process');25var child = childProcess.fork('./child.js', [], {26});27child.on('message', function (message) {28 console.log('Message from child', message);29});30child.send({ hello: 'world' });31process.on('message', function (message) {32 console.log('Message from parent:', message);33 process.send({ foo: 'bar' });34});
Using AI Code Generation
1var stryker = require('stryker-parent');2var child = stryker.childProcess('test.js', ['--port', 9000]);3child.on('message', function(message) {4 console.log(message);5});6child.send('some message');7var stryker = require('stryker-parent');8var child = stryker.childProcess('test.js', ['--port', 9000]);9child.on('message', function(message) {10 console.log(message);11});12child.send('some message');13var stryker = require('stryker-parent');14var child = stryker.childProcess('test.js', ['--port', 9000]);15child.on('message', function(message) {16 console.log(message);17});18child.send('some message');19var stryker = require('stryker-parent');20var child = stryker.childProcess('test.js', ['--port', 9000]);21child.on('message', function(message) {22 console.log(message);23});24child.send('some message');25var stryker = require('stryker-parent');26var child = stryker.childProcess('test.js', ['--port', 9000]);27child.on('message', function(message) {28 console.log(message);29});30child.send('some message');31var stryker = require('stryker-parent');32var child = stryker.childProcess('test.js', ['--port', 9000]);33child.on('message', function(message) {34 console.log(message);35});36child.send('some message');37var stryker = require('stryker-parent');38var child = stryker.childProcess('test.js', ['--port', 9000]);39child.on('message', function(message) {40 console.log(message);41});42child.send('some message');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!