jest-console
Mocking console the right way.
Why
It's rare to have console
in your code, it's more often seen in libraries to provide helpful debugging warnings. When trying to mock console
in tests, we often just spyOn
the methods being used and observe the mock calls. This works great if your message is simple, but it can also have false-negative.
console;
This is a valid console.error
call with string substitution, which will output the message The error has a type named "Oops", expected "Success".
. Normally we would test it using something like toHaveBeenCalledWith
;
But it's not representing the actual output of the message, we're just repeating what we wrote in the source code. If some typo sneaked into it, we won't be able to easily notice it because of the malfunctioned tests.
Converting it to template literals could solve this issue, but sometimes the values are objects, which cannot be serialized to strings or would loose context when doing so. In some environments (like in browsers' console), they will even be represented as inspect-able and interactive-able results, which is not possible to achieve with strings.
A better option would be to get the actual output of the logs and test it against the expected output.
;
With jest-console
, we can easily do that without extra hassles.
; ;
Installation
yarn add -D jest-console
Usage
Just import it before calling console.log
or the family.
; ;
If you want to get the current logs, import the getLog
helper.
; ;
All the methods in console
are available and automatically mocked.
console; ;;;
All the methods in console
will also be automatically cleared and cleaned up after each tests.
; ;
Every log have a corresponding logging level, you can access each level's log via getLog().levels
, or access all of them in a list with getLog().logs
.
console;console;console;console; ; ;
Since the logs are patched, in order to log or debug in the tests will not output as expected. You can import originalConsole
to obtain the un-patched, un-mocked console
.
; console;originalConsole;
Usage without Jest
It is possible to use jest-console
without Jest, just that you have to manually mock the console yourself. We provide createConsole
and mockConsole
API for this.
; // Create a jestConsole instance. It's possible to create multiple instances if neededconst jestConsole = ; // Mock the global.console with the jestConsole we just created// It returns a restore function, which will swap back to the original consoleconst restore = ; console; // Calls restore function when it's done to restore it back to the original console;
Custom matchers
It's often recommended to use jest-console
with Jest's toMatchInlineSnapshot
matcher. It makes it really easy to test the console output with confidence.
;
We also support custom toMatchInlineSnapshot
matcher to test against mocked console[method]
and console
.
;// is essentially the same as// expect(getLog().log).toMatchInlineSnapshot(); ;// is basically `expect(console.log).toHaveBeenCalledWith` for actual output,// it will only get the logs calling from `log` method.
License
MIT