Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Mocked stdin should be closed to avoid hanging forever #26

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/stdmock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export const stdin = (input: string, delay = 0) => {
return {
run: () => {
stdin = require('mock-stdin').stdin()
setTimeout(() => stdin.send(input), delay)
setTimeout(() => {
stdin.send(input)
stdin.end()
}, delay)
},
finally() {
stdin.restore()
Expand Down
14 changes: 10 additions & 4 deletions test/stdmock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,25 @@ describe('stdin', () => {
.stdin('whoa there, broseph\n')
.stdout()
.it('mocks', (_, done) => {
let input: string
process.stdin.setEncoding('utf8')
process.stdin.once('data', data => {
done(data === 'whoa there, broseph\n' ? undefined : 'invalid stdin')
process.stdin.once("data", data => input = data)
process.stdin.on("end", () => {
expect(input).to.eq('whoa there, broseph\n')
done()
})
})

fancy
.stdin('whoa there again, broseph\n')
.stdout()
.it('mocks again', (_, done) => {
let input: string
process.stdin.setEncoding('utf8')
process.stdin.once('data', data => {
done(data === 'whoa there again, broseph\n' ? undefined : 'invalid stdin')
process.stdin.once("data", data => input = data)
process.stdin.on("end", () => {
expect(input).to.eq('whoa there again, broseph\n')
done()
})
})
})