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.

Tests example in Readme doesn't work in 3.0.1

See original GitHub issue

Hello! Test example in Readme doesn’t work for me in redux-api-middleware 3.0.1. FetchMock not mocking request and server send real response. I tried to run the test in version 2.3.0 and the request was mocked. How to fix it in version 3.0.1?

import configureMockStore from "redux-mock-store"
import { apiMiddleware } from "redux-api-middleware"
import thunk from "redux-thunk"
import fetchMock from "fetch-mock"
 
import {getUser} from "./user"
 
const middlewares = [ thunk, apiMiddleware ]
const mockStore = configureMockStore(middlewares)
 
describe("async user actions", () => {
  afterEach(() => {
    fetchMock.reset()
    fetchMock.restore()
  })
 
  it("should dispatch USER_SUCCESS when getUser is called", () => {
    const store = mockStore({});
    const body = {
      email: "EMAIL",
      username: "USERNAME"
    }
   //fetchMock doesn't work in 3.0.1
    fetchMock.getOnce("https://hostname/api/users/", {body: body, headers: {"content-type": "application/json"}}) 
    const expectedActions = [
      {type: actions.USER_REQUEST},
      {type: actions.USER_SUCCESS, payload: body}
    ]
    return store.dispatch(actions.getUser()).then(() => {
      expect(store.getActions()).toEqual(expectedActions)
    })
  })
})

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:5

github_iconTop GitHub Comments

3reactions
katliger-hcommented, Feb 25, 2020

@GusevDV I faced the same issue and found this solution: https://stackoverflow.com/questions/60171894/fetch-mock-dont-work-in-version-3-2-0-redux-api-middleware-test You need to add “fetch:fetch” inside the action creator.

1reaction
iamandrewlucacommented, Feb 25, 2020

Or set a global fetch, how we do in tests

global.fetch = require('jest-fetch-mock')
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I test what my readme.md file will look like before ...
If the readme.html is in the same directory as README.md, you don't even need to serve readme.html over HTTP: you can just open...
Read more >
[BUG] Autosaving response examples still does not work #511
The code example (in the README section 'Enable auto generation examples from responses') is not valid Ruby and is missing commas and has...
Read more >
ReadMe: OpenAPI and Swagger for API Documentation
This guide explains: what OpenAPI and Swagger do, how to use OpenAPI spec, how to write an OpenAPI description, OpenAPI template, ...
Read more >
File: README.rdoc [Ruby 3.0.0]
minitest/test is meant to have a clean implementation for language implementors that need a minimal set of methods to bootstrap a working test...
Read more >
OpenSSL 3.0 - OpenSSLWiki
For example algorithms to encode and decode keys to files. If you do not load the default provider then you should always load...
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