https://github.com/lbenie/ts-jest-mock
A type definition and helper for jest mocks in TypeScript
https://github.com/lbenie/ts-jest-mock
jest testing typescript
Last synced: 10 months ago
JSON representation
A type definition and helper for jest mocks in TypeScript
- Host: GitHub
- URL: https://github.com/lbenie/ts-jest-mock
- Owner: lbenie
- License: mit
- Created: 2021-03-18T16:36:27.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-08-25T14:07:25.000Z (10 months ago)
- Last Synced: 2025-08-25T16:12:11.097Z (10 months ago)
- Topics: jest, testing, typescript
- Language: TypeScript
- Homepage:
- Size: 5.03 MB
- Stars: 13
- Watchers: 2
- Forks: 2
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/funding.yml
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/lbenie/ts-jest-mock/actions/workflows/format.yml)
[](https://github.com/lbenie/ts-jest-mock/actions/workflows/release.yml)
[](https://github.com/lbenie/ts-jest-mock/actions/workflows/test.yml)
[](https://dashboard.stryker-mutator.io/reports/github.com/lbenie/ts-jest-mock/main)
[](http://commitizen.github.io/cz-cli/)
[](https://github.com/semantic-release/semantic-release)
[](https://github.com/lbenie/ts-jest-mock/blob/master/LICENSE)
[](https://www.npmjs.com/package/ts-jest-mock)
[](https://www.npmjs.com/package/ts-jest-mock)
[](#contributors-)
[](https://forthebadge.com)
[](https://forthebadge.com)
# ts-jest-mock
A way to get jest type definition infered automagically.
## Pre-requisites
This library takes for granted that you have your tests setup with `jest`.
## Usage
Directly in a test
```ts
import { createMock } from 'ts-jest-mock'
import { someFn } from 'module_a'
jest.mock('module_a')
describe('something', () => {
it('some test', () => {
const someFnMock = createMock(someFn)
someFnMock.mockReturnValue('') <-- Type inference yay :)
})
})
```
Or with a beforeEach pattern
```ts
import { createMock } from 'ts-jest-mock'
import { someFn } from 'module_a'
jest.mock('module_a')
const someFnMock = createMock(someFn)
describe('something', () => {
beforeEach(() => {
someFnMock.mockReturnValue('') <-- Type inference yay :)
})
it('some test', () => {
expect(someFnMock).toBe('')
})
})
```
## Contributors ✨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

Lucien Bénié
💻 📖

Sébastien Dufour-Beauséjour
💻

Ryan Ong
💻
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!