Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/janhesters/mock-function
A simple mock function to mock functions.
https://github.com/janhesters/mock-function
jest mocking mocking-utility riteway testing testing-tool
Last synced: 4 months ago
JSON representation
A simple mock function to mock functions.
- Host: GitHub
- URL: https://github.com/janhesters/mock-function
- Owner: janhesters
- License: mit
- Created: 2020-12-13T11:35:49.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-03-29T07:01:26.000Z (almost 2 years ago)
- Last Synced: 2024-09-17T02:20:43.767Z (5 months ago)
- Topics: jest, mocking, mocking-utility, riteway, testing, testing-tool
- Language: TypeScript
- Homepage:
- Size: 860 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# mock-function
A simple mock function to mock functions.
Inspired by
[`jest.fn`](https://jestjs.io/docs/en/jest-object#jestfnimplementation).## Installation
Install the package.
```
# NPM
npm i --save-dev mock-function
# yarn
yarn add --dev mock-function
```Import it in your tests.
```ts
import fn from 'mock-function';
```## Usage
```ts
import mockFunction from 'mock-function';const add = (a: number, b: number) => a + b;
const mockedAdd = mockFunction(add);mockedAdd.hasBeenCalled();
// ↵ falsemockedAdd.hasBeenCalledWith(21, 21);
// ↵ falsemockedAdd.hasBeenCalledTimes;
// ↵ 0
mockedAdd.calls;
// ↵ []mockedAdd(21, 21);
// ↵ 42
mockedAdd(9000, 1);
// ↵ 9001 (😱 OVER 9000)mockedAdd.hasBeenCalled();
// ↵ truemockedAdd.hasBeenCalledWith(2000, 12);
// ↵ false
mockedAdd.hasBeenCalledWith(21, 21);
// ↵ truemockedAdd.hasBeenCalledTimes;
// ↵ 2
mockedAdd.calls;
// ↵ [[21, 21], [9000, 1]]
```## Caveats
[Mocking is a code smell](https://medium.com/javascript-scene/mocking-is-a-code-smell-944a70c90a6a)
for tight coupling and as such a surface indication that you might be able to
improve your tests and / or underlying code. Think about whether you can isolate
your side-effects or non-deterministic functions and make your code more
modular. Mocking can be okay, for example in integration tests.For example, the pure function `add` in [Usage](#usage) should be tested using
plain unit tests that
[assert the actual and expected output](https://medium.com/javascript-scene/rethinking-unit-test-assertions-55f59358253f).
I chose `add` to simplify the example, but it is generally a bad use-case for
mocking.