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

[⚙️ jest config] 테스트 도입 위한 jest 설정 #43

Closed
wants to merge 11 commits into from

Conversation

Pyotato
Copy link
Collaborator

@Pyotato Pyotato commented Jul 31, 2024

PULL REQUEST TEMPLATE

PR 설명

@appear

⚙️ 테스트 도입을 위한 설정 - jest & rtl

image

@Pyotato Pyotato linked an issue Jul 31, 2024 that may be closed by this pull request
Comment on lines 1 to 9
import { describe, expect, test } from "@jest/globals";

const sum = (num1: number, num2: number) => num1 + num2;

describe("sum module", () => {
test("adds 1 + 2 to equal 3", () => {
expect(sum(1, 2)).toBe(3);
});
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

web 폴더에 설정 잘 됐는 지 위한 임시 테스트 파일입니다.

@@ -0,0 +1,10 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 18 to 25
overrides: [
{
files: ["src/__tests__/**/*"],
env: {
jest: true,
},
},
],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 경로의 파일의 jest 테스트 파일들 eslint 설정

https://jestjs.io/docs/getting-started#using-eslint

Copy link

Copy link

Copy link

Comment on lines 1 to 9
{
"preset": "ts-jest",
"testEnvironment": "jsdom",
"transform": {
"^.+\\.ts?$": "ts-jest",
"^.+\\.tsx?$": "ts-jest",
"^.+\\.scss$": "jest-scss-transform"
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jest.config 는 최상단에 하나만 있으면되지않을까요 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇 그럼 혹시 dev dependency 설정같은 것도 최상단에 해줘야 할까요?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 그러면 좋을 것 같아요 :) 어디서든 쓸 수 있도록

import { cleanup, render } from "@testing-library/react";
import Text from "./_text";

afterEach(cleanup);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

모든 테스트에 afterEach(cleanup); 가 들어있는데 당장은 필요없지 않아여 ?

Comment on lines 4 to 8
"transform": {
"^.+\\.ts?$": "ts-jest",
"^.+\\.tsx?$": "ts-jest",
"^.+\\.scss$": "jest-scss-transform"
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const {pathsToModuleNameMapper} = require('ts-jest')
const {compilerOptions} = require('./tsconfig')

module.exports = {
    rootDir: '.',
    testRegex: '(/__(tests|specs)__/.*|(\\.|/)(test|spec))\\.([tj]sx?)$',
    transform: {
        '^.+\\.jsx?$': 'babel-jest',
        '^.+\\.tsx?$': [
            'ts-jest',
            {
                diagnostics: false,
                tsconfig: 'tsconfig.jest.json',
                babelConfig: true,
                isolatedModules: true,
            },
        ],
    },
    moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
    moduleDirectories: ['src/', 'node_modules'],
    modulePaths: ['src/'],
    moduleNameMapper: {
        '^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
        ...pathsToModuleNameMapper(compilerOptions.paths),
    },
    testPathIgnorePatterns: ['/.next/', '/node_modules/', '/config/'],
    testEnvironment: 'jsdom',
    setupFiles: [],
    setupFilesAfterEnv: ['jest-canvas-mock', './jest.setup.js', 'jest-extended/all'],
    collectCoverageFrom: ['**/*.{ts,tsx}', '!index.{ts,tsx}'],
    resolver: require.resolve('@paymoney/jest/resolver'),
    modulePathIgnorePatterns: ['/dist/'],
}

저는 요렇게 설정해놓고 쓰고있어용 참고차 전달드려요

Copy link

github-actions bot commented Aug 2, 2024

Comment on lines +1 to +12
module.exports = new Proxy(
{},
{
get: function getter() {
return () => ({
className: "className",
variable: "variable",
style: { fontFamily: "fontFamily" },
});
},
}
);
Copy link
Collaborator Author

@Pyotato Pyotato Aug 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@appear

mocks 는 design-system 과 web 각각에 따로 위치해야 할까요..? 아니면 최상위에 하나를 둘까요..?

Comment on lines +2 to +11
{},
{
get: function getter() {
return () => ({
className: "className",
variable: "variable",
style: { fontFamily: "fontFamily" },
});
},
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

web은 경로 설정이 좀 이상한 지ㅜㅜ 최상단의 경로를 읽지 못해서 어떻게 설정해야할 지 고민입니다 🥲

Comment on lines +6 to +7
// return <button onClick={() => setMode(null)}>os control</button>;
return <MyButton onClick={() => setMode(null)}>os control</MyButton>; //!! 🥲 <body><div /> </body>인 이유..?
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

web 에서 테스트를 만드면 다음과 같이 렌더링 되는 거 같아서

<body>
  <div/>
</body>  

렌더를 못하고 있는 거 같아서 설정을 어떻게 고쳐줘야 할 지 고민입니다.

Comment on lines +23 to +27
test('renders button with text "os control"', async () => {
render(<ThemeButton />);
screen.debug();
//!! 🥲 <body><div /> </body>인 이유..? 렌더가 제대로 안된 거 같은...
expect(screen.getByRole("button")).toBeInTheDocument();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

다음과 같이 렌더되어서 못찾는 중 ㅜㅜ

<body>
  <div/>
 </body> 

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

일단 .. 너무 먼길을 잘못가고계신 것 같은데 ..
useAppTheme 만 모킹을 해줘야하니 아래처럼 해줘야 될 것 같아요

jest.mock('@tripie/design-system', () => {
  const actual = jest.requireActual('@tripie/design-system');

  return {
    ...actual,
    useAppTheme: jest.fn(() => ({ mode: null, setMode: jest.fn(), toggle: jest.fn() })),
  };
});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 pull 을 받아서 테스트를 해봤는데 .. 처음 설정부터 다 손봐야 할 것 같아요 tsconfig 들도

제가 예제를 하나 만들어서 공유드리는게 좋을 것 같은데 어떠세요 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵넵 ㅜㅜ 부탁드립니다ㅠㅜ

@appear appear closed this Aug 15, 2024
@Pyotato Pyotato deleted the 42-jest-react-testing-library-setup branch August 30, 2024 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

jest 설정
2 participants