You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
734 B
Markdown

# Jest mocks are ...different
If you want to mock an imported function in Jest in a way that allows you to check if it has been called, you can not do the seemingly straighforward:
```js
// mock prefix necessary btw
const mockThatFunction = jest.fn(() => 'stuff')
jest.mock('@/path/to/module', () => ({
thatFunction: mockThatFunction,
}))
// ...in test descriptions
expect(mockThatFunction).toHaveBeenCalled()
```
This way thatFunction will be undefined without anyone telling you why.
What you need to do instead:
```js
import { thatFunction } from '@/path/to/module'
jest.mock('@/path/to/module', () => ({
thatFunction: jest.fn(() => 'stuff'),
}))
// ...in test descriptions
expect(thatFunction).toHaveBeenCalled()
```