> For the complete documentation index, see [llms.txt](https://wiki.christianpoplawski.de/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.christianpoplawski.de/javascript/jest.md).

# Jest

## Spying in automaitc `node_module` mocks

When you want to spy on methods of a module that was automatically mocked (via a file in the `__mocks__` folder that was named after the module), jest will complain. To be able to do so, you can export the functions you want to spy on from said file.

Following is an exmple of a mock for `react-native-keychain`:

```javascript
// __mocks__/react-native-keychain.js
export const setInternetCredentials = jest.fn()
export const getInternetCredentials = jest.fn()
```

This would allow us to do something like `expect(Keychain.setInternetCredentials).toHaveBeenCalledTimes(1)`

**Note:** `react-native-keychain` does not have a default export. This is why we are exporting seperate `const`s here. For a module with a default export, we would have to adapt accordingly.
