βMSW μ¬μ©νμ§ μκ³ axiosλ₯Ό λ°λ‘ mockingνλ©΄?
- λμμ λ κ°μ§λ₯Ό μμ²νλ ν μ»΄ν¬λνΈμμ axiosλ₯Ό mockingνλ©΄, λ°νκ°μΌλ‘λ μ΄λ₯Ό κ΄λ¦¬νκΈ° μ΄λ ΅κ³ λΆμμ
- νμ΄μ§λ₯Ό λ€μ μ λ ¬νλ©΄ λͺ¨μ νλ©΄μμ λ°νκ°μ μμκ° μλͺ»λμ΄ ν μ€νΈ μ€ν¨ν μ μμ
- MSW νΈλ€λ¬ ν¨μλ‘ μμ²μ νλΌλ―Έν° λ±μ λ°λΌ λ€λ₯Έ κ° λ°ν κ°λ₯
- μΏ ν€, μ΄μ§ μλ΅(μ: μ΄λ―Έμ§) λ±μ μμ² μ²λ¦¬ κ°λ₯
μμ½: κ°λ¨ν μ±μμλ λͺ¨νΉμ΄ κ°λ₯νμ§λ§ λ 볡μ‘ν μ±μμλ MSWλ‘ λ€μν λꡬλ₯Ό μ΄μ©νλ κ²μ΄ μ’μ
yarn add msw
// src.mocks.handler.ts μμ±
import { http, HttpResponse } from "msw";
export const handlers = [
http.get("http://localhost:3030/scoops", async () => {
return HttpResponse.json([
{ name: "Chocolate", imagePath: "/images/chocolate.png" },
{ name: "Vanilla", imagePath: "/images/vanilla.png" },
]);
})
]
// src/mocks/server.ts μμ±
import { setupServer } from 'msw/node'
import { handlers } from './handlers'
export const server = setupServer(...handlers)
// vitest.setup.ts μΆκ°
import { beforeAll, afterEach, afterAll } from 'vitest'
import { server } from './src/mocks/node'
beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())
await
, findBy
νμ μ¬μ©(getBy μ¬μ© X)const images = await screen.findAllByRole("img", { name: /test$/i });
expect(images).toHaveLength(3);