Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change argument type #11

Merged
merged 2 commits into from
Oct 16, 2019
Merged
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
60 changes: 36 additions & 24 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
# jest-mock-console

Jest utility to mock the console

[![codecov][codecov-badge]][codecov]
[![build][build-badge]][build]
[![Dependencies][dependencyci-badge]][dependencyci]
[![MIT License][license-badge]][license]


## Table of Contents

* [Problem](#the-problem)
* [Solution](#the-solution)
* [Installation](#installation)
* [Basic Example](#basic-example)
* [Advanced Example](#advanced-example)
* [Analyze all logs](#analyze-all-logs)
* [mockConsole(mocks)](#mockconsolemocks)
* [default](#mock-default)
* [string](#mock-string)
* [array](#mock-array)
* [object](#mock-object)
- [Problem](#the-problem)
- [Solution](#the-solution)
- [Installation](#installation)
- [Basic Example](#basic-example)
- [Advanced Example](#advanced-example)
- [Analyze all logs](#analyze-all-logs)
- [mockConsole(mocks)](#mockconsolemocks)
- [default](#mock-default)
- [string](#mock-string)
- [array](#mock-array)
- [object](#mock-object)

## The problem

If you use console or prop-types in your app, and you use jest then you end up with tests that look like:

<img
Expand All @@ -34,6 +35,7 @@ If you use console or prop-types in your app, and you use jest then you end up w
This is not helpful as all of the tests have passed, but you are seeing red. It is especially unhelpful when there is an actual failure as you have to search through all the red to find the actual failed test.

## The solution

This allows you to mock and unmock the console at will, so your tests look like:

<img
Expand All @@ -58,10 +60,11 @@ npm install --save-dev jest-mock-console
At the top of your test file:

```javascript
import mockConsole from 'jest-mock-console';
import mockConsole from "jest-mock-console";
```

Then your tests

```javascript
describe(...
it(...
Expand All @@ -75,7 +78,6 @@ describe(...

However you always need to restore the console after each test or you will break jest. This is where the [setupTestFramework](#setuptestframework) file comes in.


## Advanced Example

If you don't want to worry about accidentally forgetting to `restoreConsole()` after your tests you can modify jest to unmock after every `it(...)`.
Expand Down Expand Up @@ -129,16 +131,26 @@ the test looks like:

## mockConsole(mocks)

* **`mocks[string,array,object]`**: The properties of the console you want to mock. Defaults to ['log','warn','error']
* <a id='mock-default'></a> default - Will mock console.log, console.warn, and console.error
* `mockConsole()` same as `mockConsole(['log','warn','error'])`
* <a id='mock-string'></a> string - You can mock a single function
* `mockConsole('error')`
* <a id='mock-array'></a> array - You can mock multiple functions
* `mockConsole(['log', 'info'])`
* <a id='mock-object'></a> object - You can set custom functions for console
* ``mockConsole({error: (string) => console.log(`console.error of: ${string}`)})``

- **`mocks[string,array,object]`**: The properties of the console you want to mock. Defaults to ['log','warn','error']
- <a id='mock-default'></a> default - Will mock console.log, console.warn, and console.error
```
mockConsole() // same as `mockConsole(['log','warn','error'])
```
- <a id='mock-string'></a> string - You can mock a single function
```
mockConsole('error')
```
- <a id='mock-array'></a> array - You can mock multiple functions
```
mockConsole(['log', 'info'])
```
- <a id='mock-object'></a> object - You can set custom functions for console
```
const originalConsole = window.console;
mockConsole({
error: (...args) => originalConsole.log(`console.error of: ${...args}`)
})
```

## LICENSE

Expand Down