Last active
December 26, 2022 13:38
-
-
Save tzafrirben/8ad9b17b1fc97ef6a55d60633611db01 to your computer and use it in GitHub Desktop.
Test child process with dependency injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('Test execute commands in a spawn child process', () => { | |
test('execCommand should return command stdout/stderr', async () => { | |
// create a "fake" child process | |
const process = mockChildProcess(); | |
// note that execCommand returns a promise, so this fake child process was | |
// not spawned yet | |
const cmdExec = execCommand(process); | |
// since we control the fake child process, we can emit the data events on | |
// stdout/stderr streams | |
process.stdout?.emit('data', 'Process stdout'); | |
process.stderr?.emit('data', 'Process stderr'); | |
// since we control the fake child process, we can end it | |
process.emit('close', 0); | |
// "execute" the command: spawn a child process and wait for it to end | |
const { stderr, stdout, code } = await cmdExec; | |
// validate the command output | |
expect(code).toEqual(0); | |
expect(stderr).toEqual('Process stderr'); | |
expect(stdout).toEqual('Process stdout'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment