simple-fake-console
TypeScript icon, indicating that this package has built-in type declarations

0.1.8 • Public • Published

Simple Fake Console

Create fake consoles for testing purpose

Usage

Use it directly

const { ConsoleInstance, ActionType, getString } = require('simple-fake-console')
const console = new ConsoleInstance()
 
console.log('hello', 'world')
console.info(0, 1, 2, 3)
console.error(new Error('foo'))
console.warn('Warning')
console.clear()
 
// Get all actions
//
// Result: Array<
//   {
//     type: 'log' | 'info' | 'error' | 'warn',
//     data: any[]
//   } | {
//     type: 'clear'
//   }
// >
console.getActions()
 
// Get string from selected types of actions
//
// Result: string
getString({
  console,
  types: [ActionType.Log, ActionType.Info]
})

Use it in test via dependency injection

// main.ts
 
import { Console } from 'simple-fake-console'
 
export function main (console: Console) {
  console.log('hello', 'world')
}
// test.ts
 
import assert from 'assert'
import { ConsoleInstance, ActionType, getString } from 'simple-fake-console'
import { main } from './main'
 
const fakeConsole = new ConsoleInstance()
main(fakeConsole)
 
assert.deepStrictEqual(
  fakeConsole.getActions(),
  [{
    type: 'log',
    data: ['hello', 'world']
  }]
)
 
assert.strictEqual(
  getString({ console, types: [ActionType.Log] }),
  'hello world'
)

License

MIT © Hoàng Văn Khải

Readme

Keywords

Package Sidebar

Install

npm i simple-fake-console

Weekly Downloads

11

Version

0.1.8

License

MIT

Unpacked Size

14.6 kB

Total Files

17

Last publish

Collaborators

  • khai96_