-
I'm trying to make a mock service worker for a simple API request like this: /src/api/Sectors.js: export const getSectors = async (token) => {
const requestOptions = {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
};
try {
const response = await fetch("/api/sector/list", requestOptions);
if (!response.ok) {
return { error: `Request failed with status ${response.status}` };
} else {
const data = await response.json();
return data;
}
} catch (error) {
return { error: "Request failed" };
}
}; My setup files are as follows: jest.setup.js: import "@testing-library/jest-dom";
import { server } from 'src/mocks/server.js';
beforeAll(() => {
server.listen();
console.log('server listening');
});
afterEach(() => server.resetHandlers());
afterAll(() => server.close()); jest.config.js: const TEST_REGEX = "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|js?|tsx?|ts?)$";
module.exports = {
testRegex: TEST_REGEX,
transform: {
// "^.+\\.tsx?$": "ts-jest",
"^.+\\.jsx?$": "babel-jest",
// "^.+\\.mjs$": "babel-jest",
},
testPathIgnorePatterns: ["<rootDir>/build/", "<rootDir>/node_modules/", "<rootDir>/dist/"],
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json"],
moduleNameMapper: {
"^~(.*)$": "<rootDir>/src/$1"
},
modulePaths: ["<rootDir>"],
testEnvironment: "node",
setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
}; My setup for MSW is as follows: /src/mocks/handlers.js: import { rest } from "msw";
export const handlers = [
rest.get("/api/sector/list", (req, res, ctx) => {
console.log('api sector list');
const token = req.headers.get("Authorization");
if (!token) {
return res(
ctx.status(401),
ctx.json({ error: "Authorization token is missing" })
);
}
return res(
ctx.status(200),
ctx.json([
{ id: 1, name: "Sector 1" },
{ id: 2, name: "Sector 2" },
])
);
}),
]; /src/mocks/server.js: import { setupServer } from 'msw/node';
import { handlers } from './handlers';
export const server = setupServer(...handlers); And my test is as follows: /tests/Sectors.test.js: import { getSectors } from 'src/api/Sectors';
it("should fetch sectors successfully", async () => {
const token = "fakeToken";
const sectors = await getSectors(token);
expect(sectors).toEqual([
{ id: 1, name: "Sector 1" },
{ id: 2, name: "Sector 2" On the date, furthermore, the console log in handlers never prints. I assume that MSW never intercepts the API calls. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Hi, @wiskani. Does the |
Beta Was this translation helpful? Give feedback.
-
Thank you very much for your help. I tried updating MWS, but it didn't work. I submitted a CodeSandbox, maybe you can see something that I missed. |
Beta Was this translation helpful? Give feedback.
-
I resolved the issue by including the "fetch" function in my test environment using the "whatwg-fetch" library:
|
Beta Was this translation helpful? Give feedback.
I resolved the issue by including the "fetch" function in my test environment using the "whatwg-fetch" library: