xpack / mock-console-ts

A Node.js CommonJS/ES6 module with a mock console class

Home Page:https://xpack.github.io/mock-console-ts/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GitHub package.json version npm (scoped) license

A Node.js CommonJS/ES6 module with a mock console class

This project provides a TypeScript Node.js CommonJS/ES6 module with a mock console which logs to string arrays, for testing purposes.

The open source project is hosted on GitHub as xpack/mock-console-ts.

Maintainer & developer info

This page documents how to use this module in an user application. For maintainer information, see the separate README-MAINTAINER page.

Prerequisites

A recent Node.js (>=16.0.0), since the TypeScript code is compiled into ECMAScript 2020 code, and the tests use ES6 modules.

Install

The module is available as @xpack/mock-console from the public npmjs repository; it can be added as a dependency to any TypeScript or JavaScript project with npm install:

npm install --save @xpack/mock-console@latest

The module does not provide any executables, and generally there are no reasons to install it globally.

User info

This section is intended for those who plan to use this module in their own projects.

The @xpack/mock-console module can be imported into both TypeScript and JavaScript Node.js code

In TypeScript and ECMAScript modules, use import:

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

In JavaScript with CommonJS, use require():

const { MockConsole } = request('@xpack/mock-console')

The module can be included in any application and the class can be used directly or a custom class can be derived from it for a custom behaviour.

The typical use case is to create an instance of the Logger object, then log messages at different levels:

const mockConsole = new MockConsole()

const log = new Logger({
  console: mockConsole,
  level: 'info'
})

The logged lines are available in the outLines and errLines public members:

t.ok(mockConsole.outLines.length > 0, 'has stdout')
t.match(mockConsole.outLines[1], 'Multiple subcommands', 'has title')

t.equal(mockConsole.errLines.length, 0, 'stderr is empty')

For debug purposes, there is also a function that can be used to display string arrays with the lines prefixed by a line number; this is useful in tests to decide what lines to check:

import { dumpLines } from '@xpack/mock-console'

dumpLines(mockConsole.outLines)

Reference

For more details on the available class definitions, including all methods, accessors, properties, etc, please see the TypeDoc reference pages.

Known problems

  • none

Status

The @xpack/mock-console module is currently under development.

Tests

The module is tested with 100% coverage and CI tested on every push via GitHub Actions.

Compatibility notices

According to semver rules:

Major version X (X.y.z | X > 0) MUST be incremented if any backwards incompatible changes are introduced to the public API.

v1.0.0

The initial version, extracted from logger-ts and cli-start-options-ts.

License

The original content is released under the MIT License, with all rights reserved to Liviu Ionescu.

About

A Node.js CommonJS/ES6 module with a mock console class

https://xpack.github.io/mock-console-ts/

License:MIT License


Languages

Language:TypeScript 94.8%Language:JavaScript 5.1%Language:CSS 0.1%