@chubbyts/chubbyts-function-mock

1.4.1 • Public • Published

chubbyts-function-mock

CI Coverage Status Infection MSI npm-version

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

Description

A function mock helper.

IMPORTANT: deepStrictEqual is used for parameter comparsion, === if you pass strict: true

Requirements

  • node: 16

Installation

Through NPM as @chubbyts/chubbyts-function-mock.

npm i @chubbyts/chubbyts-function-mock@1.4.1

Usage

createFunctionMock

import { expect, test } from '@jest/globals';
import { useFunctionMock } from '@chubbyts/chubbyts-function-mock/dist/function-mock';

type MyFunction = (string: string, start: number, stop: number) => string;

test('my random test', () => {
  const [myFunction, myFunctionMocks] = useFunctionMock<MyFunction>([
    { parameters: ['test', 0, 2], return: 'te' },
    {
      callback: (string: string, start: number, stop: number): string => {
        expect(string).toBe('test');
        expect(start).toBe(1);
        expect(stop).toBe(2);

        return 'es';
      }
    },
    { parameters: ['test', 0, 2], error: new Error('test') },
  ]);

  expect(myFunction('test', 0, 2)).toBe('te');
  expect(myFunction('test', 1, 2)).toBe('es');

  try {
    expect(myFunction('test', 2, 2)).toBe('st');
    throw new Error('Expect fail');
  } catch (e) {
    expect(e).toMatchInlineSnapshot('[Error: test]');
  }

  // if you want to be sure, that all mocks are called
  expect(myFunctionMocks.length).toBe(0);
});

createObjectMock

IMPORTANT: Do not use with spread operator ...myObject!.

import { expect, test } from '@jest/globals';
import { useObjectMock } from '@chubbyts/chubbyts-function-mock/dist/object-mock';

type MyType = {
  substring: (string: string, start: number, stop: number) => string;
  uppercase: (string: string) => string;
};

test('my random test', () => {
  const [myObject, myObjectMocks] = useObjectMock<MyType>([
    { name: 'substring', parameters: ['test', 0, 2], return: 'te' },
    {
      name: 'substring',
      callback: (string: string, start: number, stop: number): string => {
        expect(string).toBe('test');
        expect(start).toBe(1);
        expect(stop).toBe(2);

        return 'es';
      }
    },
    { name: 'uppercase', parameters: ['test'], error: new Error('test') },
  ]);

  expect(myObject.substring('test', 0, 2)).toBe('te');
  expect(myObject.substring('test', 1, 2)).toBe('es');

  try {
    expect(myObject.uppercase('test')).toBe('st');
    throw new Error('Expect fail');
  } catch (e) {
    expect(e).toMatchInlineSnapshot('[Error: test]');
  }

  // if you want to be sure, that all mocks are called
  expect(myObjectMocks.length).toBe(0);
});

Copyright

2023 Dominik Zogg

Readme

Keywords

Package Sidebar

Install

npm i @chubbyts/chubbyts-function-mock

Weekly Downloads

377

Version

1.4.1

License

MIT

Unpacked Size

23.9 kB

Total Files

9

Last publish

Collaborators

  • dominikzogg