Skip to content

Commit

Permalink
feat: mockTypes for easy mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 3, 2018
1 parent 5f63dde commit a332890
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Redirect `console` and `stdout/stderr` to the consola and easily restore redirect.
- Browser support
- Pause/Resume support
- Mocking support

## Installation

Expand Down Expand Up @@ -101,6 +102,34 @@ Benefit of this function is that things like `console.info` will be correctly re

Consola will enqueue all logs when paused and then sends them to the reported when resumed.

#### `mockTypes`

Mock all types. Useful for using with tests.

The first argument passed to `mockTypes` should be a callback function accepting `(typeName, type)` and returning the mocked value:

```js
consola.mockTypes((typeName, type) => jest.fn())
```

Please note that with the example above, everything is mocked independently for each type. If you need one mocked fn create it outside:

```js
const fn = jest.fn()
consola.mockTypes(() => fn)
```

If callback function returns a _falsy_ value, that type won't be mocked.

For example if you just need to mock `consola.fatal`:

```js
consola.mockTypes((typeName) => typeName === 'fatal' && jest.fn())
```

**NOTE:** Any instance of consola that inherits the mocked instance, will apply provided callback again.
This way, mocking works for `withTag` scopped logers without need to extra efforts.

## Fields

#### `reporters`
Expand Down
20 changes: 20 additions & 0 deletions examples/mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node -r esm

import { consola } from './utils'

function mockFn (type) {
if (type === 'info') {
return () => this.log('INFO INFO INFO')
}
}

consola.info('before')

consola.mockTypes(mockFn)

const tagged = consola.withTag('newTag')

consola.log('log is not mocked!')

consola.info('Dont see me')
tagged.info('Dont see me too')
21 changes: 20 additions & 1 deletion src/consola.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default class Consola {
this._async = typeof options.async !== 'undefined' ? options.async : null
this._stdout = options.stdout
this._stderr = options.stdout
this._mockFn = options.mockFn

// Create logger functions for current instance
for (const type in this._types) {
Expand All @@ -22,6 +23,11 @@ export default class Consola {
this._defaults
))
}

// Use _mockFn if is set
if (this._mockFn) {
this.mockTypes()
}
}

get level () {
Expand Down Expand Up @@ -59,7 +65,8 @@ export default class Consola {
types: this._types,
defaults: this._defaults,
stdout: this._stdout,
stderr: this._stderr
stderr: this._stderr,
mockFn: this._mockFn
}, options))
}

Expand Down Expand Up @@ -180,6 +187,18 @@ export default class Consola {
}
}

mockTypes (mockFn) {
this._mockFn = mockFn || this._mockFn

if (typeof this._mockFn !== 'function') {
return
}

for (const type in this._types) {
this[type] = this._mockFn(type, this._types[type]) || this[type]
}
}

_wrapLogFn (defaults) {
function logFn () {
if (paused) {
Expand Down

0 comments on commit a332890

Please sign in to comment.