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

테스트 환경에서 msw 모킹을 위한 세팅 #278

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions frontend/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const config: StorybookConfig = {
'@_types': path.resolve(__dirname, '../src/types'),
'@_utils': path.resolve(__dirname, '../src/utils'),
'@_routes': path.resolve(__dirname, 'src/routes'),
'@_mocks': path.resolve(__dirname, 'src/mocks'),
};
}

Expand Down
25 changes: 25 additions & 0 deletions frontend/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"preset": "ts-jest",
"testEnvironment": "jest-environment-jsdom",



"moduleNameMapper": {
"^@_apis/(.*)$": "<rootDir>/src/apis/$1",
"^@_constants/(.*)$": "<rootDir>/src/constants/$1",
"^@_common/(.*)$": "<rootDir>/src/common/$1",
"^@_components/(.*)$": "<rootDir>/src/components/$1",
"^@_hooks/(.*)$": "<rootDir>/src/hooks/$1",
"^@_layouts/(.*)$": "<rootDir>/src/layouts/$1",
"^@_pages/(.*)$": "<rootDir>/src/pages/$1",
"^@_types/(.*)$": "<rootDir>/src/types/$1",
"^@_utils/(.*)$": "<rootDir>/src/utils/$1",
"^@_routes/(.*)$": "<rootDir>/src/routes/$1",
"^@_mocks/(.*)$": "<rootDir>/src/mocks/$1"
},
"testEnvironmentOptions": {
"customExportConditions": [""]
},
"setupFiles": ["dotenv/config", "./jest.polyfills.js"],
"setupFilesAfterEnv": ["<rootDir>/jest.setup.ts"]
}
29 changes: 0 additions & 29 deletions frontend/jest.config.ts

This file was deleted.

32 changes: 32 additions & 0 deletions frontend/jest.polyfills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @note The block below contains polyfills for Node.js globals
* required for Jest to function when running JSDOM tests.
* These HAVE to be require's and HAVE to be in this exact
* order, since "undici" depends on the "TextEncoder" global API.
*
* Consider migrating to a more modern test runner if
* you don't want to deal with this.
*/

const { TextDecoder, TextEncoder } = require('node:util');
const { ReadableStream, TransformStream } = require('node:stream/web');

Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
ReadableStream: { value: ReadableStream },
TransformStream: { value: TransformStream },
});

const { Blob, File } = require('node:buffer');
const { fetch, Headers, FormData, Request, Response } = require('undici');

Object.defineProperties(globalThis, {
fetch: { value: fetch, writable: true },
Blob: { value: Blob },
File: { value: File },
Headers: { value: Headers },
FormData: { value: FormData },
Request: { value: Request },
Response: { value: Response },
});
21 changes: 21 additions & 0 deletions frontend/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { server } from './src/mocks/server';
import '@testing-library/jest-dom';
import dotenv from 'dotenv';
dotenv.config({ path: './.env' });

beforeAll(() => {
console.log('befor all server listen');
server.listen({
onUnhandledRequest: 'error', // 이 옵션을 통해 핸들되지 않은 요청을 경고로 표시
});
});

afterEach(() => {
console.log('after each server reset handlers ');
server.resetHandlers();
});
// Clean up after the tests are finished.
afterAll(() => {
console.log('after all server closed');
server.close();
});
49 changes: 10 additions & 39 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"lint:eslint": "eslint 'src/**/*.{ts,tsx}'",
"lint:style": "stylelint **/*.style.ts --fix",
"lint": "npm run lint:eslint && npm run lint:style"

},
"repository": {
"type": "git",
Expand Down Expand Up @@ -75,6 +74,7 @@
"ts-node": "^10.9.2",
"type-fest": "^4.21.0",
"typescript": "^5.5.3",
"undici": "^6.19.5",
"webpack": "^5.92.1",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/hooks/queries/useMoims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default function useMoims() {
queryKey: [QUERY_KEYS.moims],
queryFn: getMoims,
});

// TODO:서버에서 검증하면 없애야 함
const filteredMoims = useMemo(
() =>
Expand All @@ -19,6 +18,5 @@ export default function useMoims() {
}),
[moims],
);

return { moims: filteredMoims, isLoading };
}
12 changes: 8 additions & 4 deletions frontend/src/mocks/browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { setupWorker } from 'msw/browser';
import { moimHandler } from './handlers/moimHandler';
import { interestHandler } from './handlers/interestHandler';
import { pleaseHandler } from './handlers/pleaseHandler';
import { moimHandler } from './handler/moimHandler';
import { interestHandler } from './handler/interestHandler';
import { pleaseHandler } from './handler/pleaseHandler';

export const worker = setupWorker(...moimHandler, ...interestHandler, ...pleaseHandler);
export const worker = setupWorker(
...moimHandler,
...interestHandler,
...pleaseHandler,
);
3 changes: 3 additions & 0 deletions frontend/src/mocks/handler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { moimHandler } from './moimHandler';

export const handlers = [...moimHandler];
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { http, HttpResponse } from 'msw';
import ENDPOINTS from '@_apis/endPoints';

export const moimHandler = [
http.get(`${ENDPOINTS.moims}`, () => {
console.log('msw중');
return HttpResponse.json({
data: {
moims: [
Expand All @@ -16,6 +16,10 @@ export const moimHandler = [
maxPeople: 2,
authorNickname: '2',
description: '',
status: 'MOIMING',
participants: [],
comments: [],
isZzimed: false,
},
{
moimId: 2,
Expand All @@ -27,6 +31,10 @@ export const moimHandler = [
maxPeople: 2,
authorNickname: '2',
description: '',
status: 'MOIMING',
participants: [],
comments: [],
isZzimed: false,
},
{
moimId: 3,
Expand All @@ -38,6 +46,10 @@ export const moimHandler = [
maxPeople: 2,
authorNickname: '2',
description: '',
status: 'MOIMING',
participants: [],
comments: [],
isZzimed: false,
},
{
moimId: 4,
Expand All @@ -49,6 +61,10 @@ export const moimHandler = [
maxPeople: 2,
authorNickname: '2',
description: '',
status: 'MOIMING',
participants: [],
comments: [],
isZzimed: false,
},
{
moimId: 5,
Expand All @@ -60,6 +76,10 @@ export const moimHandler = [
maxPeople: 2,
authorNickname: '2',
description: '',
status: 'MOIMING',
participants: [],
comments: [],
isZzimed: false,
},
{
moimId: 6,
Expand All @@ -71,6 +91,10 @@ export const moimHandler = [
maxPeople: 2,
authorNickname: '2',
description: '',
status: 'MOIMING',
participants: [],
comments: [],
isZzimed: false,
},
{
moimId: 7,
Expand All @@ -82,6 +106,10 @@ export const moimHandler = [
maxPeople: 2,
authorNickname: '2',
description: '',
status: 'MOIMING',
participants: [],
comments: [],
isZzimed: false,
},
{
moimId: 8,
Expand All @@ -93,6 +121,10 @@ export const moimHandler = [
maxPeople: 2,
authorNickname: '2',
description: '',
status: 'MOIMING',
participants: [],
comments: [],
isZzimed: false,
},
{
moimId: 9,
Expand All @@ -104,6 +136,10 @@ export const moimHandler = [
maxPeople: 2,
authorNickname: '2',
description: '',
status: 'MOIMING',
participants: [],
comments: [],
isZzimed: false,
},
],
},
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/mocks/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { setupServer } from 'msw/node';
import { handlers } from './handler/index';

export const server = setupServer(...handlers);
19 changes: 19 additions & 0 deletions frontend/src/mocks/wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { PropsWithChildren } from 'react';

const queryClient = new QueryClient({
defaultOptions: {
queries: {
networkMode: 'always',
retry: false,
},
},
});

const wrapper = ({ children }: PropsWithChildren) => {
return (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
};

export default wrapper;
3 changes: 2 additions & 1 deletion frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"@_pages/*": ["src/pages/*"],
"@_types/*": ["src/types/*"],
"@_utils/*": ["src/utils/*"],
"@_routes/*": ["src/routes/*"]
"@_routes/*": ["src/routes/*"],
"@_mocks/*": ["src/mocks/*"]
}
},
"ts-node": {
Expand Down
1 change: 1 addition & 0 deletions frontend/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = {
'@_types': path.resolve(__dirname, 'src/types'),
'@_utils': path.resolve(__dirname, 'src/utils'),
'@_routes': path.resolve(__dirname, 'src/routes'),
'@_mocks': path.resolve(__dirname, 'src/mocks'),
},
},
module: {
Expand Down
Loading