Options
All
  • Public
  • Public/Protected
  • All
Menu

Provides functionality useful during unit testing.

// esm
import { spy } from '@paychex/core/test';

// commonjs
const { spy } = require('@paychex/core/test');

Index

Functions

  • Creates a new Spy instance for unit tests. A Spy is a method that takes the place of an existing method and has additional properties that can be queried to verify that certain test conditions have been met.

    example
    import * as expect from 'expect';
    import { test } from '@paychex/core';
    import someFactoryMethod from '../path/to/test/file';

    const { spy } = test;

    describe('some factory method', () => {

    let instance, dependency;

    beforeEach(() => {
    dependency = {
    methodA: spy().returns('a'),
    methodB: spy().returns('b')
    };
    instance = someFactoryMethod(dependency);
    });

    it('invokes dependency method a', () => {
    instance.method();
    expect(dependency.methodA.called).toBe(true);
    });

    it('handles error', async () => {
    const handler = spy();
    const err = new Error('test');
    dependency.methodB.throws(err);
    await instance.anotherMethod().catch(handler);
    expect(handler.called).toBe(true);
    expect(handler.args[0]).toBe(err);
    });

    it('retries on error', () => {
    dependency.methodA.onCall(0).throws(new Error());
    dependency.methodA.onCall(1).returns(123);
    const result = instance.method();
    expect(dependency.methodA.callCount).toBe(2);
    expect(result).toBe(123);
    });

    });

    Returns Spy

    A new Spy instance for unit testing.