Skip to main content

Class: MockConsole

Defined in: mock-console.ts:56

The MockConsole class extends a Node.js Console by capturing messages and storing them in internal arrays, rather than sending them to an output device.

Rationale: During testing, it is essential to verify the output of various commands. To achieve this, the console output must be intercepted.

This is accomplished by creating a Console object with two writable streams that capture the output into local arrays of strings, one line at a time.

If the messages contain /n, they are split into separate lines.

The line terminators are not stored in the arrays.

UTF-8 encodings are used for multi-character strings.

The arrays are available as public members (outLines for the standard output and errLines for the standard error).

Extends

  • Console

Constructors

new MockConsole()

new MockConsole(): MockConsole

Defined in: mock-console.ts:91

Create a MockConsole instance.

The constructor creates the two writable streams configured to decode utf-8 strings.

Example

const mockConsole = new MockConsole()

Returns

MockConsole

Overrides

Console.constructor

Properties

outLines

outLines: string[] = []

Defined in: mock-console.ts:66

An array of strings containing the lines written to stdout.

Example

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

errLines

errLines: string[] = []

Defined in: mock-console.ts:75

An array of strings containing the lines written to stderr.

Example

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

outBuffer

protected outBuffer: string = ''

Defined in: mock-console.ts:77


errBuffer

protected errBuffer: string = ''

Defined in: mock-console.ts:78

Methods

clear()

clear(): void

Defined in: mock-console.ts:188

Clear the content of the internal arrays and the content of the parent Console.

Example

mockConsole.clear()

Returns

void

Overrides

Console.clear