-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(consoleStream): rewrite to better test process.stdout
- Loading branch information
1 parent
9c12aeb
commit 7d81c14
Showing
1 changed file
with
15 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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
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() | ||
}) | ||
}) | ||
|