Skip to content

Commit

Permalink
test(consoleStream): rewrite to better test process.stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
jpwilliams committed Jul 9, 2020
1 parent 9c12aeb commit 7d81c14
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/consoleStream.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import consoleStream from './consoleStream'
import { mockProcessStdout } from 'jest-mock-process'

describe("Console stream", () => {
let consoleLogData = ""
const storeLog = (inputs) => (consoleLogData += inputs)
it("streams correctly", async (done) => {
console["log"] = jest.fn(storeLog)
describe('Console stream', () => {
let consoleStream
let mockStdout

const stream = consoleStream({})
stream.write(JSON.stringify({id: 1, name: `item first`}))
beforeAll(() => {
mockStdout = mockProcessStdout()
consoleStream = require('./consoleStream').default
})

it('streams correctly', () => {
const stream = consoleStream()
stream.write(
JSON.stringify({ id: 1, name: `item first`, time: 1594310416073 })
)
stream.end()

const parsedLogData = JSON.parse(consoleLogData)

expect(parsedLogData).toMatchObject({
"metadata": {
"context": {},
"id": 1,
"name": "item first",
"level": "info"
},
"message": "info"
}
expect(mockStdout).toHaveBeenCalledWith(
'{"metadata":{"id":1,"name":"item first","context":{},"level":"info"},"message":"info","timestamp":1594310416073}\n'
)
done()
})
})

0 comments on commit 7d81c14

Please sign in to comment.