doMockIf is not a function
See original GitHub issueI’m trying to write an utility function that will mock the response of the profile view and the auth refresh of my API user.
I followed the docs for version 3.0.0, which is installed
npm ls | grep jest-fetch-mock
+-- jest-fetch-mock@3.0.0
When I run my tests, I get
TypeError: fetch.doMockIf is not a function
1 | export function mockLoggedOut() {
> 2 | fetch.doMockIf('api/profile/').mockResponse('UNAUTHORIZED', { status: 401})
| ^
3 | fetch.doMockIf('api/auth/refresh/').mockResponse('UNAUTHORIZED', { status: 401})
4 | }
5 |
at mockLoggedOut (src/test-utils/auth.test.tsx:2:9)
at Object.<anonymous> (src/store/auth/actions.test.tsx:62:5)
fetch seems to be properly mocked, since doing
● Auth actions › checkAuth checks auth when logged out
function mockConstructor(_a, _b) {
return fn.apply(this, arguments);
}
1 | export function mockLoggedOut() {
> 2 | throw Error(fetch)
| ^
3 | fetch.doMockIf('api/profile/').mockResponse('UNAUTHORIZED', { status: 401})
4 | fetch.doMockIf('api/auth/refresh/').mockResponse('UNAUTHORIZED', { status: 401})
5 | }
at mockLoggedOut (src/test-utils/auth.test.tsx:2:9)
at Object.<anonymous> (src/store/auth/actions.test.tsx:62:5)
Shows that fetch is a mock (at least it looks like)
On the other hand, doMock is working
● Auth actions › checkAuth checks auth when logged out
TypeError: fetch.doMockIf is not a function
1 | export function mockLoggedOut() {
2 | fetch.doMock('api/profile/').mockResponse('UNAUTHORIZED', { status: 401})
> 3 | fetch.doMockIf('api/auth/refresh/').mockResponse('UNAUTHORIZED', { status: 401})
| ^
4 | }
5 |
at mockLoggedOut (src/test-utils/auth.test.tsx:3:9)
at Object.<anonymous> (src/store/auth/actions.test.tsx:62:5)
So, either I didn’t understand the docs, either it is not clear, either doMockIf is not working
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Confused about conditional mocking (doMockIf, doMockOnceIf)
The "Mock*If" functions determine if it mocks or not, not the content of the response. You're just looking for a single call to...
Read more >Jest 'TypeError: is not a function' in jest.mock - Stack Overflow
In my case, I had to mock a Node.js module. I'm using React and Redux in ES6, with Jest and Enzyme for unit...
Read more >jest-fetch-mock typescript
doMockIf or fetch.dontMockIf overrides the default behavior of mocking/not mocking all requests. fetch is not available in Node, which is where Jest is...
Read more >Jest-fetch-mock: Jest Mock for Fetch - Morioh
doMockIf or fetch.dontMockIf overrides the default behavior of mocking/not mocking all requests. fetch.dontMockOnce , fetch.doMockOnce , fetch.
Read more >The Jest Object
The new function has no formal parameters and when called will return undefined . This functionality also applies to async functions. Class ....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, it does. Thank you