@chubbyts/chubbyts-function-mock
Version:
A function mock helper.
127 lines (95 loc) • 5.71 kB
Markdown
# chubbyts-function-mock
[](https://github.com/chubbyts/chubbyts-function-mock/actions?query=workflow%3ACI)
[](https://coveralls.io/github/chubbyts/chubbyts-function-mock?branch=master)
[](https://dashboard.stryker-mutator.io/reports/github.com/chubbyts/chubbyts-function-mock/master)
[](https://www.npmjs.com/package/@chubbyts/chubbyts-function-mock)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-function-mock)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-function-mock)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-function-mock)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-function-mock)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-function-mock)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-function-mock)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-function-mock)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-function-mock)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-function-mock)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-function-mock)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-function-mock)
## Description
A function mock helper.
**IMPORTANT**: `deepStrictEqual` is used for parameter comparsion, `===` if you pass `strict: true`
## Requirements
* node: 18
## Installation
Through [NPM](https://www.npmjs.com) as [@chubbyts/chubbyts-function-mock][1].
```sh
npm i @chubbyts/chubbyts-function-mock@2.0.2
```
## Usage
### createFunctionMock
```ts
import { expect, test } from '@jest/globals'; // or 'vitest'
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`!.
```ts
import { expect, test } from '@jest/globals'; // or 'vitest'
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
2025 Dominik Zogg
[1]: https://www.npmjs.com/package/@chubbyts/chubbyts-function-mock