Skip to content

anmiles/jest-extensions

Repository files navigation

@anmiles/jest-extensions

Extension functions and utils for jest


Installation

npm install --save-dev @anmiles/jest-extensions

Usage examples

equals

Checks that object equals another object. Actually exposes built-in toEqual matcher.

  • matcher
import '@anmiles/jest-extensions';

expect(object).equals(anotherObject);
  • extension
import '@anmiles/jest-extensions';

expect(spy).toHaveBeenCalledWith(expect.equals(obj));
  • default export
import { equals } from '@anmiles/jest-extensions';

function customMatcher<T>(received: T, expected: T) : jest.CustomMatcherResult {
	const receivedTransformed = someTransformFunction(received);
	return equals(receivedTransformed, expected);
}

toBeFunction

Checks that function being called with specified arguments will return expected value

  • matcher
import '@anmiles/jest-extensions';

expect(func).toBeFunction((arg1, arg2) => expectedReturnValue);
  • extension
import '@anmiles/jest-extensions';

expect(invoker).toHaveBeenCalledWith(expect.toBeFunction((arg1, arg2) => expectedReturnValue));
  • default export
import { toBeFunction } from '@anmiles/jest-extensions';

function customMatcher(received: (key: string, value: string) => Record<string, string>, expectedValue: string) : jest.CustomMatcherResult {
	return toBeFunction(received, [ 'key', 'value' ], { key : expectedValue });
}

toEqualStructure

Compares objects with structure properties only (without functions). Type of expected object should not consist of any function-like properties.

  • matcher
import '@anmiles/jest-extensions';

expect(obj).toEqualStructure(objWithoutFunctions);
  • extension
import '@anmiles/jest-extensions';

expect(func).toHaveBeenCalledWith(expect.toEqualStructure(obj));
  • default export
import { toEqualStructure, Structure } from '@anmiles/jest-extensions';

function customMatcher<T extends Record<any, any>>(received: T, expected: Structure<T>) : jest.CustomMatcherResult {
	return toEqualStructure(received, expected);
}
  • toStructure function
import { toStructure } from '@anmiles/jest-extensions';

const objWithoutFunctions = toStructure(obj);

mockFS

Removed in favor of mock-fs package. Refer to this diff for example of replacement.