https://github.com/vanillaes/mock-console
Mock the 'console' (ie log, info, error) for testing
https://github.com/vanillaes/mock-console
console esm esmodules mock testing
Last synced: 6 months ago
JSON representation
Mock the 'console' (ie log, info, error) for testing
- Host: GitHub
- URL: https://github.com/vanillaes/mock-console
- Owner: vanillaes
- License: mit
- Created: 2020-02-11T06:16:18.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2022-01-24T14:41:19.000Z (over 4 years ago)
- Last Synced: 2025-09-14T04:28:27.584Z (10 months ago)
- Topics: console, esm, esmodules, mock, testing
- Language: JavaScript
- Homepage:
- Size: 271 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
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.
## 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
```html
import { MockConsole } from 'path/to/mock-console/index.js'
```
The minified version can be imported from
```html
import { MockConsole } from 'path/to/mock-console/index.min.js'
```
### Node
Install the package
```sh
npm install @vanillaes/mock-console
```
Import using the module path
```javascript
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`)
```javascript
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
```javascript
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
```javascript
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
```javascript
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.