Skip to content
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

[#13] Init: msw 설정 #14

Merged
merged 5 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export async function register() {
);

if (process.env.NEXT_RUNTIME === 'nodejs') {
console.log('MOCKING ENABLED FOR:', process.pid);

const { server } = await import('@/src/mocks/node');
server.listen();
server.listen({
onUnhandledRequest: 'bypass',
});
}
}
3 changes: 1 addition & 2 deletions src/mocks/MSWComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ export const MSWComponent = ({ children }: { children: React.ReactNode }) => {

useEffect(() => {
if (!isInit) {
initMsw();
isSetInit(true);
initMsw().then(() => isSetInit(true));
}
}, [isInit]);

Expand Down
2 changes: 1 addition & 1 deletion src/mocks/handlers/post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpResponse, http } from 'msw';

const getPostList = http.get(`${process.env.NEXT_DOMAIN}/posts`, () => {
const getPostList = http.get(`${process.env.NEXT_PUBLIC_URL}/posts`, () => {
return HttpResponse.json({
content: [
{
Expand Down
23 changes: 6 additions & 17 deletions src/mocks/initMsw.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
export const initMsw = () => {
if (process.env.NODE_ENV === 'development') {
if (process.env.NEXT_RUNTIME === 'nodejs') {
(async () => {
const { server } = await import('./node');
server.listen({
onUnhandledRequest: 'bypass',
});
})();
} else {
(async () => {
const { worker } = await import('./browser');
worker.start({
onUnhandledRequest: 'bypass',
});
})();
}
export const initMsw = async () => {
if (process.env.NEXT_RUNTIME !== 'nodejs') {
const { worker } = await import('./browser');
await worker.start({
onUnhandledRequest: 'bypass',
});
}
ienrum marked this conversation as resolved.
Show resolved Hide resolved
};
Loading