nested.js
This file
Path
test/helpers/nested.js
53 lines · 2.4 KiB · JavaScript
Last changed by
Upgrade xo (#980)ehmickybefore adoptionccfa412
History of this fileTake it
Add tests for `node:worker_threads` (#964)ehmickybefore adoptiona1cd3cf
lines 1–3why, one step back1import {once} from 'node:events';2import {Worker} from 'node:worker_threads';3import {execa} from '../../index.js';Upgrade xo (#980)ehmickybefore adoptionccfa412
line 4why, one step back4import {FIXTURES_DIRECTORY_URL} from './fixtures-directory.js';Add tests for `node:worker_threads` (#964)ehmickybefore adoptiona1cd3cf
line 5why, one step back5Upgrade xo (#980)ehmickybefore adoptionccfa412
line 6why, one step back6const WORKER_URL = new URL('worker.js', FIXTURES_DIRECTORY_URL);Add tests for `node:worker_threads` (#964)ehmickybefore adoptiona1cd3cf
line 7why, one step back7Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 8–12why, one step back8const runWorker = (nodeFile, commandArguments, options) => {9 [commandArguments, options] = Array.isArray(commandArguments)10 ? [commandArguments, options]11 : [[], commandArguments];12 return new Worker(WORKER_URL, {workerData: {nodeFile, commandArguments, options}});Add tests for `node:worker_threads` (#964)ehmickybefore adoptiona1cd3cf
lines 13–15why, one step back13};1415// eslint-disable-next-line max-paramsUpgrade xo (#980)ehmickybefore adoptionccfa412
lines 16–20why, one step back16const nestedCall = (isWorker, fixtureName, execaMethod, file, commandArguments, options, parentOptions) => {17 [commandArguments, options = {}, parentOptions = {}] = Array.isArray(commandArguments)18 ? [commandArguments, options, parentOptions]19 : [[], commandArguments, options];20 const subprocessOrWorker = execaMethod(fixtureName, [JSON.stringify(options), file, ...commandArguments], {...parentOptions, ipc: true});Add tests for `node:worker_threads` (#964)ehmickybefore adoptiona1cd3cf
lines 21–45why, one step back21 const onMessage = once(subprocessOrWorker, 'message');22 const promise = getNestedResult(onMessage);23 promise.parent = isWorker ? getParentResult(onMessage) : subprocessOrWorker;24 return promise;25};2627const getNestedResult = async onMessage => {28 const {result} = await getMessage(onMessage);29 return result;30};3132const getParentResult = async onMessage => {33 const {parentResult} = await getMessage(onMessage);34 return parentResult;35};3637const getMessage = async onMessage => {38 const [{error, parentError = error, result, parentResult}] = await onMessage;39 if (parentError) {40 throw parentError;41 }4243 return {result, parentResult};44};45Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 46–53why, one step back46export const nestedWorker = (...commandArguments) => nestedCall(true, 'nested.js', runWorker, ...commandArguments);47const nestedExeca = (fixtureName, ...commandArguments) => nestedCall(false, fixtureName, execa, ...commandArguments);48export const nestedExecaAsync = (...commandArguments) => nestedExeca('nested.js', ...commandArguments);49export const nestedExecaSync = (...commandArguments) => nestedExeca('nested-sync.js', ...commandArguments);50export const parentWorker = (...commandArguments) => nestedWorker(...commandArguments).parent;51export const parentExeca = (...commandArguments) => nestedExeca(...commandArguments).parent;52export const parentExecaAsync = (...commandArguments) => nestedExecaAsync(...commandArguments).parent;53export const parentExecaSync = (...commandArguments) => nestedExecaSync(...commandArguments).parent;