@vanillaes/mock-console
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

Mock-Console

Mock-Console is a small ES module implementation of a console mock. It allows enabling, disabling, and capturing the output of the built-in console methods.

GitHub Release NPM Release Bundlephobia Latest Status Release Status

Discord

Features

  • ECMAScript Module
  • Typescript Compatible

Imports

This package works isomorphically in browser and server-side JavaScript

Browser

Import directly from the local path or a CDN

<script type="module">
import { MockConsole } from 'path/to/mock-console/index.js'
</script>

The minified version can be imported from

<script type="module">
import { MockConsole } from 'path/to/mock-console/index.min.js'
</script>

Node

Install the package

npm install @vanillaes/mock-console

Import using the module path

import { MockConsole } from '@vanillaes/mock-console'

Usage

Creation

const logger = new MockConsole()

Note: The mock is a singleton. Every time new is called on the mock it'll return the same instance

MockConsole.disable()

Disables the built-in console methods (ie log, info, error)

const logger = new MockConsole();
logger.disable();
console.log('This will NOT print to the console');

MockConsole.restore()

Restores the built-in console methods after they've been disabled

const logger = new MockConsole();
logger.disable();
console.log('This will NOT print to the console');
logger.restore();
console.log('This WILL print to the console');
> This WILL print to the console

MockConsole.capture()

Capture is used to store the console output so it can be retrieved later for testing

const logger = new MockConsole();
logger.capture();
console.log('This message will be captured');
logger.restore();
console.log(logger.logs);
> [ 'This message will be captured' ]

Captured logs are stored in an array

  • console.log -> MockConsole.logs[]
  • console.info -> MockConsole.infos[]
  • console.error -> MockConsole.errors[]

MockConsole.flush()

Flush removes all previously captured logs

const logger = new MockConsole();
logger.capture();
console.log('This message will be captured');
logger.restore();
console.log(logger.logs);
> [ 'This message will be captured' ]
logger.flush();
console.log(logger.logs);
> []

Typings

Typings are generated from JSDoc using Typescript. They are 100% compatible with VSCode Intellisense and will work seamlessly with Typescript.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.0.02latest

Version History

VersionDownloads (Last 7 Days)Published
3.0.02
2.0.80
2.0.70
2.0.60
2.0.50
2.0.40
2.0.30
2.0.20
2.0.10
2.0.00
1.1.50
1.1.40
1.1.30
1.1.20
1.1.10
1.1.00

Package Sidebar

Install

npm i @vanillaes/mock-console

Weekly Downloads

2

Version

3.0.0

License

MIT

Unpacked Size

8.29 kB

Total Files

6

Last publish

Collaborators

  • evanplaice