Reputation: 431
I recently upgraded some packages in a previously-working app and found that changes in Jest made a bunch of tests fail after the upgrade, but ONLY in --coverage mode.
All my Redux API action tests work fine when I run them without the --coverage
flag. I am mocking all the API calls with nock
in the tests and all the tests that are supposed to mock a successful API response are failing, because nock
is failing to intercept the request.
Here's an example of a failing test and the code it's testing:
apiProfileActions.js
import { RSAA } from "redux-api-middleware";
const BASE_URL = process.env.REACT_APP_BASE_URL;
export const GET_PROFILE_REQUEST = "GET_PROFILE_REQUEST";
export const GET_PROFILE_SUCCESS = "GET_PROFILE_SUCCESS";
export const GET_PROFILE_FAILURE = "GET_PROFILE_FAILURE";
export function getProfile(token, userId) {
return {
[RSAA]: {
endpoint: `${BASE_URL}/api/user/${userId}`,
method: "GET",
types: [
GET_PROFILE_REQUEST,
GET_PROFILE_SUCCESS,
{
type: GET_PROFILE_FAILURE,
payload: (action, state, res) => {
return res.json().then(data => {
let message = "Sorry, something went wrong :(";
if (data && data.message) {
message = data.message;
}
return { message };
});
}
}
],
headers: { Authorization: `Bearer ${token}` }
}
};
}
apiProfileActions.test.js
/**
* @jest-environment node
*/
import nock from "nock";
import { apiMiddleware } from "redux-api-middleware";
import configureMockStore from "redux-mock-store";
import * as actions from "../../store/actions/apiProfileActions";
import * as profileReducer from "../../store/reducers/profile";
const BASE_URL = process.env.REACT_APP_BASE_URL;
console.log(BASE_URL);
const createStore = configureMockStore([apiMiddleware]);
const store = createStore(profileReducer.initialState);
const id = "1234";
const token = "1651a5d6-c2f7-453f-bdc7-13888041add6";
describe("apiProfileActions", () => {
describe("api actions", () => {
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
// expect at least one expect in async code:
expect.hasAssertions();
});
it("GET_PROFILE: Dispatches success action after successful GET", async () => {
const response = {
id,
name: "Emma Goldman",
avatar_url: "http://www.example.com/avatar.png"
};
nock(`${BASE_URL}`)
.get(`/api/user/${response.id}`)
.reply(200, response);
const expectedResult = {
payload: undefined,
type: "GET_PROFILE_SUCCESS",
meta: undefined
};
const result = await store.dispatch(
actions.getProfile(token, response.id)
);
expect(result).toEqual(expectedResult);
});
});
});
and here's the console output for the failed test:
● apiProfileActions › api actions › GET_PROFILE: Dispatches success action after successful GET
expect(received).toEqual(expected) // deep equality
- Expected - 2
+ Received + 3
Object {
+ "error": true,
"meta": undefined,
- "payload": undefined,
- "type": "GET_PROFILE_SUCCESS",
+ "payload": [InternalError: [RSAA].ok function failed],
+ "type": "GET_PROFILE_FAILURE",
}
111 | actions.getProfile(token, response.id)
112 | );
> 113 | expect(result).toEqual(expectedResult);
| ^
114 | });
115 |
116 | it("GET_PROFILE: Dispatches failure action after failed GET", async () => {
at Object.<anonymous> (src/__tests__/actions/apiProfileActions.test.js:113:22)
so all the tests that are supposed to mock successful api calls are returning failure responses instead, with error message [InternalError: [RSAA].ok function failed]
, because nock
is failing to intercept the request and it's trying to hit the real API endpoint. Again these exact same tests are passing fine if the --coverage
flag is off.
I have tried upgrading / downgrading nock
and redux-api-middleware
but this hasn't make any difference. Any ideas what might have changed to make nock
stop working, but only when run in --coverage
mode? Or any ideas where to start troubleshooting?
Upvotes: 2
Views: 862