Skip to content
Myr

nested.js

This file

Path

test/helpers/nested.js

69 lines · 2.4 KiB · JavaScript

Last changed by

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 1–6why, one step back
1import {once} from 'node:events';
2import {Worker} from 'node:worker_threads';3import {execa} from '../../index.js';4import {FIXTURES_DIRECTORY_URL} from './fixtures-directory.js';56const WORKER_URL = new URL('worker.js', FIXTURES_DIRECTORY_URL);

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

line 7why, one step back
7const NESTED_URL = new URL('nested/', FIXTURES_DIRECTORY_URL);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 8why, one step back
8

Refactor test helpers for the `verbose` option (#1128)ehmickybefore adoption18d320f

lines 9–18why, one step back
9// Like `execa(file, commandArguments, options)` but spawns inside another parent process.
10// This is useful when testing logic where Execa modifies the global state.11// For example, when it prints to the console, with the `verbose` option.12// The parent process calls `execa(options.parentFixture, parentOptions)`13// When `options.isSync` is `true`, `execaSync()` is called instead.14// When `options.worker` is `true`, the whole flow happens inside a Worker.15export const nestedSubprocess = async (file, commandArguments, options, parentOptions) => {16 const result = await nestedInstance(file, commandArguments, options, parentOptions);17 const nestedResult = result.ipcOutput[0];18 return {...result, nestedResult};

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 19–20why, one step back
19};
20

Refactor test helpers for the `verbose` option (#1128)ehmickybefore adoption18d320f

line 21why, one step back
21export const nestedInstance = (file, commandArguments, options, parentOptions) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 22–24why, one step back
22 [commandArguments, options = {}, parentOptions = {}] = Array.isArray(commandArguments)
23 ? [commandArguments, options, parentOptions]24 : [[], commandArguments, options];

Refactor test helpers for the `verbose` option (#1128)ehmickybefore adoption18d320f

lines 25–45why, one step back
25 const {
26 parentFixture = 'nested.js',27 worker = false,28 isSync = false,29 optionsFixture,30 optionsInput = {},31 ...otherOptions32 } = options;33 const normalizedArguments = {34 parentFixture,35 parentOptions,36 isSync,37 file,38 commandArguments,39 options: otherOptions,40 optionsFixture,41 optionsInput,42 };43 return worker44 ? spawnWorker(normalizedArguments)45 : spawnParentProcess(normalizedArguments);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 46–47why, one step back
46};
47

Refactor test helpers for the `verbose` option (#1128)ehmickybefore adoption18d320f

lines 48–52why, one step back
48const spawnWorker = async workerData => {
49 const worker = new Worker(WORKER_URL, {workerData});50 const [result] = await once(worker, 'message');51 if (result instanceof Error) {52 throw result;

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 53–54why, one step back
53 }
54

Refactor test helpers for the `verbose` option (#1128)ehmickybefore adoption18d320f

line 55why, one step back
55 return result;

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 56–57why, one step back
56};
57

Refactor test helpers for the `verbose` option (#1128)ehmickybefore adoption18d320f

line 58why, one step back
58export const spawnParentProcess = ({parentFixture, parentOptions, ...ipcInput}) => execa(parentFixture, {...parentOptions, ipcInput});

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 59–69why, one step back
59
60// Some subprocess options cannot be serialized between processes.61// For those, we pass a fixture filename instead, which dynamically creates the options.62export const getNestedOptions = async (options, optionsFixture, optionsInput) => {63 if (optionsFixture === undefined) {64 return options;65 }6667 const {getOptions} = await import(new URL(optionsFixture, NESTED_URL));68 return {...options, ...getOptions({...options, ...optionsInput})};69};