Skip to content
Myr

shared.js

This file

Path

test/convert/shared.js

56 lines · 2.0 KiB · JavaScript

Last changed by

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

History of this file

Take it

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 1–56why, one step back
1import test from 'ava';
2import {execa} from '../../index.js';3import {setFixtureDir} from '../helpers/fixtures-dir.js';4import {5 finishedStream,6 assertWritableAborted,7 assertStreamError,8 assertSubprocessError,9 getReadWriteSubprocess,10} from '../helpers/convert.js';11import {foobarString} from '../helpers/input.js';1213setFixtureDir();1415const testSubprocessFail = async (t, methodName) => {16 const subprocess = getReadWriteSubprocess();17 const stream = subprocess[methodName]();1819 const cause = new Error(foobarString);20 subprocess.kill(cause);2122 const error = await assertStreamError(t, stream, {cause});23 assertWritableAborted(t, subprocess.stdin);24 t.true(subprocess.stdout.readableEnded);25 t.true(subprocess.stderr.readableEnded);2627 await assertSubprocessError(t, subprocess, error);28};2930test('subprocess fail -> .readable() error', testSubprocessFail, 'readable');31test('subprocess fail -> .writable() error', testSubprocessFail, 'writable');32test('subprocess fail -> .duplex() error', testSubprocessFail, 'duplex');3334const testErrorEvent = async (t, methodName) => {35 const subprocess = execa('empty.js');36 const stream = subprocess[methodName]();37 t.is(stream.listenerCount('error'), 0);38 stream.destroy();39 await t.throwsAsync(finishedStream(stream));40};4142test('.readable() requires listening to "error" event', testErrorEvent, 'readable');43test('.writable() requires listening to "error" event', testErrorEvent, 'writable');44test('.duplex() requires listening to "error" event', testErrorEvent, 'duplex');4546const testSubprocessError = async (t, methodName) => {47 const subprocess = getReadWriteSubprocess();48 const stream = subprocess[methodName]();49 const cause = new Error(foobarString);50 subprocess.kill(cause);51 await assertStreamError(t, stream, {cause});52};5354test('Do not need to await subprocess with .readable()', testSubprocessError, 'readable');55test('Do not need to await subprocess with .writable()', testSubprocessError, 'writable');56test('Do not need to await subprocess with .duplex()', testSubprocessError, 'duplex');