Socket
Socket
Sign inDemoInstall

mock-console-es

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mock-console-es

Mock and trap the console for testing


Version published
Weekly downloads
9
increased by350%
Maintainers
1
Install size
9.26 kB
Created
Weekly downloads
 

Readme

Source

Mock-Console-ES

Mock-Console-ES 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 Releases NPM Release Bundlephobia MIT License Latest Status Release Status

Features

  • ECMAScript Module
  • CommonJS Bundle Included
  • Typescript Compatible

Installation

npm install mock-console-es
import MockConsole from 'mock-console-es';

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);
> []

CommonJS

A .cjs bundle is included for CommonJS compatibility

const { MockConsole } = require('mock-console-es');

Typings

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

Keywords

FAQs

Last updated on 23 May 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc