-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SCF-66] mock server init #67
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export async function register() { | ||
if (process.env.NEXT_RUNTIME === "nodejs") { | ||
console.log("server instrucmenttation"); | ||
const { server } = await import("./mocks/server"); | ||
server.listen(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { setupWorker } from "msw/browser"; | ||
import { handlers } from "./handlers"; | ||
|
||
export const worker = setupWorker(...handlers); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import sign from "./sign"; | ||
|
||
export const handlers = [...sign]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { http, HttpResponse } from "msw"; | ||
|
||
const signIn = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. signIn -> sign으로 바꿔야할 것 같아욥 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분은 로그인 관련 테스트 코드 작업하면서 수정해놓겠습니다. 현재 PR의 중점은 MSW 기본 세팅위주로 잡은지라 수정될 여지가 많아서요. |
||
http.post(`${process.env.NEXT_PUBLIC_API_KEY}/sign-in`, () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 첫번째 매개변수 통해 넘겨온 api를 mock이 탈취해서 HttpResponse.json 형태로 돌려주는 꼴 맞나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 맞습니다. 리턴 방식은 다양하게 바꿔줄 수 있습니다만, 임시로 공식문서에서 제안한 �리턴값을 넣어둔 상태입니다. |
||
return HttpResponse.json({ | ||
accessToken: "123", | ||
}); | ||
}), | ||
http.post(`${process.env.NEXT_PUBLIC_API_KEY}/sign-up`, () => { | ||
return HttpResponse.json(); | ||
}), | ||
]; | ||
|
||
export default signIn; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mock handler들을 여기서 다 갖고와서 목업 서버를 셋팅하는거구요 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { setupServer } from "msw/node"; | ||
import { handlers } from "./handlers"; | ||
|
||
export const server = setupServer(...handlers); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"use client"; | ||
import { useEffect } from "react"; | ||
|
||
/** | ||
* 참조 링크 | ||
* https://github.com/mswjs/msw/issues/1644 | ||
* draft pr : https://github.com/mswjs/examples/pull/101 | ||
*/ | ||
|
||
export function MockProvider({ | ||
children, | ||
}: Readonly<{ | ||
children: React.ReactNode; | ||
}>) { | ||
useEffect(() => { | ||
(async () => { | ||
if (typeof window !== "undefined" && process.env.NEXT_MOCK_SERVER) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 useEffect를 사용해서 mockServer를 클라이언트에서 마운트되고 시작하게 만든거군욥 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 검색해서 사람들이 사용한 방식 채용한겁니다. env 값으로 msw를 쓸지말지 정하는 방식은 좋아보이더라고요. |
||
const { worker } = await import("../mocks/browser"); | ||
await worker.start(); | ||
} | ||
})(); | ||
}, []); | ||
|
||
return <>{children}</>; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기서 모든 mock handler들이 다 모이는거구요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
네네 맞습니다. node와 브라우저간 핸들러 다루는 함수(setupWorker)가 달라 이 둘은 나눠집니다.