npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@libj/tbench

v1.2.1

Published

Toolset for testing JavaScript code with ease

Downloads

4,838

Readme

Overview

A "test bench" tools set for testing JavaScript / TypeScript code with comfort.
Based on sinon.js library.
All mocks are sinon stubs.

Features

  • convenient API
  • unit test itself

Mocks

ModuleMock

Mocks any JavaScript module.

API

function ModuleMock(
  module: string | Object,
  sinon: SinonSandbox = sinon.createSandbox(),
)
  • module: npm module name or custom module
  • sinon: custom instance of SinonSandbox

NPM module

import * as fs from 'fs'
import { ModuleMock } from '@libj/tbench'

const mock = ModuleMock('fs')
mock.existsSync.withArgs('/foo/bar.json').returns(true)

fs.existsSync('/foo/bar.json') // => true

Custom module

// myFoo.js
export const myFoo = () => 'foo'
// myBar.js
import { myFoo } from './myFoo.js'

export const myBar = () => myFoo()
// myBar.test.js
import { ModuleMock } from '@libj/tbench'
import * as MyFooModule from './myFoo.js'
import { myBar } from './myBar.js'

describe('myBar()', () => {
  it('calls myFoo()', () => {
    const mock = ModuleMock(MyFooModule).myFoo
    mock.returns('The Foooo')

    expect(myBar()).toBe('The Foooo')
  })
})

More examples

See in specs.

ClassMock

Mocks instances of classes.

API

function ClassMock(module: Object)
function ClassMock(module: Object, spec: Object)
function ClassMock(module: Object, spec: Object, sinon: SinonSandbox)
function ClassMock(module: Object, sinon: SinonSandbox)
  • module: Custom module which holds class export
  • spec: Specification object on how to mock the instance of class
  • sinon: custom instance of SinonSandbox

Asserting constructor call

// MyFooClass.js
export class MyFooClass {}
// myBar.js
import { MyFooClass } from './MyFooClass'

export const myBar = arg => new MyFooClass(arg)
// myBar.test.js
import { ClassMock } from '@libj/tbench'
import * as MyFooClassModule from './MyFooClass'
import { myBar } from './myBar'

describe('myBar()', () => {
  it('constructs class with arg', () => {
    const mock = ClassMock(MyFooClassModule)
    mock.$constructor

    myBar('Foxy Lady')

    expect(mock.$constructor.calledOnce).toBeTruthy()
    expect(mock.$constructor.getCall(0).args).toEqual(['Foxy Lady'])
  })
})

(!) CAUTION: This will not differentiate if class has been called with or without new !

Mock class instance properties

// MyFooClass.js
export class MyFooClass {
  constructor() {
    this.foo = 'Initial Foo'
    this.bar = 'Initial Bar'
  }
}
// myBar.js
import { MyFooClass } from './MyFooClass'

export const myBar = () => new MyFooClass()
// myBar.test.js
import { ClassMock } from '@libj/tbench'
import * as MyFooClassModule from './MyFooClass'
import { myBar } from './myBar'

describe('myBar()', () => {
  it('proxies class instance properties', () => {
    const mock = ClassMock(MyFooClassModule, {
      foo: 'The Foo',
      bar: null,
    })
    mock.foo
    mock.bar.value('At the Bar')

    const res = myBar()

    expect(res.foo).toBe('The Foo')
    expect(res.bar).toBe('At the Bar')
  })
})

(i) Mind how mock values are initialized for foo and bar props
(i) Mock property has to be accessed even if initialized to trigger mocking
(i) It is always possible to overwrite initialized values at any time with any conditions (see sinon stubs)
(i) See respective specs for more examples

Mock class instance methods

// MyFooClass.js
export class MyFooClass {
  foo() {}
  bar() {}
}
// myBar.js
import { MyFooClass } from './MyFooClass'

export const myBar = () => new MyFooClass()
// myBar.test.js
import { ClassMock } from '@libj/tbench'
import * as MyFooClassModule from './MyFooClass'
import { myBar } from './myBar'

describe('myBar()', () => {
  it('proxies class instance methods', () => {
    const mock = ClassMock(MyFooClassModule, {
      'foo()': null,
      'bar()': 'The Bar from Method',
    })
    mock.foo.returns('And The Foo')
    mock.bar

    const o = myBar()

    expect(o.foo()).toBe('And The Foo')
    expect(o.bar()).toBe('The Bar from Method')
  })
})

(i) Method mocks are denoted using parenthesis
(i) Mind how mock values are initialized for foo() and bar() methods
(i) It is always possible to overwrite initialized values at any time with any constraints
(i) See respective specs for more examples

Restoring single mock only

// test.js
import { ClassMock } from '@libj/tbench'

describe('FooTest', () => {
  let mock

  beforeEach(() => {
    mock = ClassMock(FooClassModule)
  })

  afterEach(() => {
    mock.$restore()
  })
});

(i) Applies to both ModuleMock / ClassMock

Restoring all mocks at once

This can be controlled via custom sinon instance

// test.js
import * as sinonLib from 'sinon'
import { ModuleMock, ClassMock } from '@libj/tbench'

describe('FooTest', () => {
  let sinon, moduleMock, classMock

  beforeEach(() => {
    sinon = sinonLib.createSandbox()
    moduleMock = ModuleMock('fs', sinon)
    classMock = ClassMock(FooClassModule, sinon)
  })

  afterEach(() => {
    sinon.restore()
  })
});

Using in pure NodeJS code

In order for mocks to be working in raw nodejs code imports should be done using module as whole.

// foo.js
const foo = () => {}

module.exports = { foo }
// bar.js
const FooModule = require('./foo.js')

const bar = () => FooModule.foo()

module.exports = { bar }

(i) Notice how import is done in bar.js
(i) This is applied to both ModuleMock and ClassMock