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

Bug : jest, esbuild-jest 의 사용으로 인한 버그 #197

Closed
unchaptered opened this issue Sep 23, 2022 · 2 comments
Closed

Bug : jest, esbuild-jest 의 사용으로 인한 버그 #197

unchaptered opened this issue Sep 23, 2022 · 2 comments
Assignees
Labels
bug 버그 발생 test 테스트 코드

Comments

@unchaptered
Copy link
Member

unchaptered commented Sep 23, 2022

1. jest, esbuild-jest 의 미사용 변수가 에러를 발생

1.1. 한 일

@axisotherwise

jest 의 성능 이슈 로 인해서 esbuild-jest 를 사용하였습니다.

  • ts-jest : 최초 실행 120초, 반복 실행 55초
  • esbuild-jest : 최초 실행 1030초, 반복 실행 38초

그리고 다음과 같은 테스트용 유닛 테스트 를 작성했습니다.

import AuthController from "../../../../src/routes/controllers/auth.controller";

jest.mock("../../../../src/routes/services/auth.service");
jest.mock("../../../../src/modules/validators/joi.validator");

describe('Auth Controller Test', () => {

    let authController: AuthController;                     // 사용하지 않은 변수가 에러를 발생

    beforeAll(() => {
        authController = new AuthController();
    });

    it("Authontroller be defined", () => expect(AuthController).toBeDefined());

});

1.2. 에러 내용

~\test\unit\routes\controllers\auth.controller.test.ts: Cannot transform the imported binding "AuthController" since it's also used in a type annotation.
Please strip type annotations using @babel/preset-typescript or @babel/preset-flow

     9 | describe('Auth Controller Test', () => {
    10 |
  > 11 |     let authController: AuthController;
       |                         ^^^^^^^^^^^^^^
    12 |     let mockRequest: Request, mockResponse: Response, mockNextFunc: NextFunction;
    13 |
    14 |     beforeAll(() => {

1.3. 조치 사항 A : 실패

선언 후 사용하지 않은 변수가 에러를 일으키고 있습니다. (오기)

import 구문을 사용하고 있을 때, type annotation 을 명시해버리면 에러가 나고 있습니다.

따라서, 파일의 11번 라인을 다음과 같이 변경하면 에러가 사라집니다.

let authController;

해당 방식을 사용하면 authController 의 타입이 any 로 변경되어서, AuthController.prototype 이 가지고 있는 프로퍼티에 접근할 수 없습니다.

1.4. 조치 사항 B : 성공 - @babel/preset-typescript 설치

종속성 라이브러리인 @babel/preset-typescript 을 설치하고 프로젝트 루트 경로에 babel.config.json 을 생성했습니다.

{
    "presets": [
        "@babel/preset-typescript"
    ]
}

환경 설정

OS: Windows 10
Node:  v16.15.1 (@unchaptered), v16.13.1(@axisotherwise)
npm : v8.11.0 (@unchaptered), v8.2.2 (@axisotherwise)
@unchaptered unchaptered added the bug 버그 발생 label Sep 23, 2022
@unchaptered unchaptered changed the title Bug : jest, esbuild-jest 의 미사용 변수가 에러를 발생 Bug : jest, esbuild-jest 의 사용으로 인한 버그 Sep 23, 2022
@unchaptered
Copy link
Member Author

unchaptered commented Sep 23, 2022

2. 파일 파싱 실패

2.1. 한 일

다음의 테스트 코드를 실행시켰습니다.

import AuthController from "../../../../src/routes/controllers/auth.controller";

import { Request, Response, NextFunction } from "express";
import * as mockHttp from 'node-mocks-http';

jest.mock("../../../../src/routes/services/auth.service");
jest.mock("../../../../src/modules/validators/joi.validator");

describe('Auth Controller Test', () => {

    let authController: AuthController;
    let mockRequest: Request, mockResponse: Response, mockNextFunc: NextFunction;
    
    beforeAll(() => {
        authController = new AuthController();
    });
    beforeEach(() => {
        mockRequest = mockHttp.createRequest();
        mockResponse = mockHttp.createResponse();
        mockNextFunc = jest.fn();
    })

    it("Authontroller be defined", () => expect(AuthController).toBeDefined());

    it('authContro', () => {

        authController.logout = jest.fn();
        authController.logout(mockRequest, mockResponse, mockNextFunc);

    });

});

2.2. 에러 내용

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.

2.3. 조치 사항

본 이슈의 1.4. 조치 사항 B : 성공 - @babel/preset-typescript 설치 로 해결했습니다.

@unchaptered unchaptered changed the title Bug : jest, esbuild-jest 의 사용으로 인한 버그 Bug : jest, esbuild-jest 의 사용으로 인한 버그 Sep 23, 2022
@unchaptered
Copy link
Member Author

해당 이슈는 해결되었습니다.

unchaptered added a commit that referenced this issue Sep 23, 2022
unchaptered added a commit that referenced this issue Sep 23, 2022
@unchaptered unchaptered added this to the 테스트 코드 milestone Sep 26, 2022
@unchaptered unchaptered added the test 테스트 코드 label Sep 26, 2022
@unchaptered unchaptered self-assigned this Sep 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 버그 발생 test 테스트 코드
Projects
None yet
Development

No branches or pull requests

1 participant