question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

doMockIf is not a function

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
yinzaracommented, Dec 26, 2019
fetch.doMockIf(/api\/(profile|auth/refresh)\//).mockResponse(req => {
   if (req.url.includes("profile")) {
      return {
       status: 401,
       body: "UNAUTHORIZED"
      }
   } else if (req.url.includes("auth")) {
      return {
          status: 403
          body: "FORBIDDEN"
       }
   }
})
0reactions
sieiracommented, Jan 8, 2020

Yes, it does. Thank you

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found