All files / tests/helpers spawn-stub.ts

100% Statements 34/34
100% Branches 5/5
100% Functions 3/3
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 351x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x  
import { type ExecutionContext } from 'ava'
 
import sinon from 'sinon'
 
import childProcessModule, { ChildProcess } from 'node:child_process'
import EventEmitter from 'node:events'
 
type FakeProcessBehaviour = (proc: FakeProcess) => void
 
class FakeProcess extends EventEmitter {
  stdout = new EventEmitter()
  stderr = new EventEmitter()
 
  constructor(behaviour?: FakeProcessBehaviour) {
    super()
 
    if (behaviour) {
      setTimeout(() => behaviour(this), 0)
    }
  }
}
 
export function stubSpawn(
  t: ExecutionContext<unknown>,
  behaviour?: FakeProcessBehaviour
) {
  let spawnStub = sinon
    .stub(childProcessModule, 'spawn')
    .returns(new FakeProcess(behaviour) as ChildProcess)
 
  t.teardown(() => spawnStub.restore())
 
  return spawnStub
}