What is Jest?

박태건·2023년 11월 6일

Javascript

목록 보기
1/1

What is Jest?

❓ **“What is Jest?”**

Jest is a JavaScript testing framework that is developed and maintained by Facebook.
It is designed to be fast, flexible, and easy to use, and it can be used to test the functionality of JavaScript code in a variety of contexts, including web applications, Node.js servers, and more.
Jest is built on top of Jasmine, a popular testing framework for JavaScript, and it includes a number of features that make it easy to write and run tests, including a command-line interface, automatic test discovery, and support for snapshot testing.
Jest is widely used in the JavaScript development community and is well-documented, with extensive documentation available online.


Jest는 페이스북에 의해 개발되어 관리되는 자바스크립트 테스팅 프레임워크입니다.
빠르고 유연하며 사용하기 쉽도록 설계되었으며, 웹 응용 프로그램, Node.js 서버 등 다양한 컨텍스트에서 JavaScript 코드의 기능을 테스트하는 데 사용될 수 있습니다.
Jest는 JavaScript의 인기있는 테스트 프레임워크인 Jasmine 위에 구축되었으며 커맨드라인 인터페이스, 자동 테스트 검색 및 스냅 샷 테스트 지원을 포함하여 테스트를 쉽게 작성하고 실행할 수 있는 여러 기능이 포함되어 있습니다.
Jest는 자바스크립트 개발 커뮤니티에서 널리 사용되며 문서화가 잘 되어 있고 온라인에서 광범위한 문서를 이용할 수 있습니다.

❓ **“Let you introudce me guidelines for the Jest tutorial?”**
  1. Install Jest: Before you can use Jest, you need to install it. You can do this by running npm install --save-dev jest in your terminal. This will install Jest and add it to the devDependencies section of your package.json file.

    npm install --save-dev jest
  2. Write test cases: To use Jest, you need to write test cases for your code. A test case is a function that tests a specific aspect of your code and verifies that it is working correctly. Test cases are usually written using the test function provided by Jest, which takes a string description of the test case as its first argument and a function containing the code to be tested as its second argument.

    // math.test.js
    
    function add(a, b) {
      return a + b;
    }
    
    test('adds 1 + 2 to equal 3', () => {
      expect(add(1, 2)).toBe(3);
    });
  3. Run test cases: To run your test cases with Jest, you can use the jest command in your terminal. This will execute all of the test cases in your project and display the results.

  4. Debug test cases: If a test case fails, you can use Jest's built-in debugging tools to figure out why. Jest provides a number of functions and features that you can use to debug your test cases, including the ability to run test cases in isolation, the ability to inspect the values of variables at different points in the test case, and the ability to pause the execution of the test case and interact with it using the debugger.

  5. Learn more about Jest: There are many more features and capabilities of Jest that you can learn about as you continue to use it. You can find more information about Jest in its documentation and in the many tutorials and resources available online(https://jestjs.io/).


  • Install Jest: Jest를 사용하기 전 먼저 Jest를 설치 해야 합니다. npm install --save-dev jest 를 Terminal에 입력하여 실행시킬 수 있습니다. 이 명령줄은 Jest를 설치하고 pacakage.json 파일 안 구역에 devDependencies 를 추가합니다.
  • Write test cases: 코드를 위해서 테스트 케이스를 작성해야만 합니다. 테스트케이스는 코드의 특정 부분을 테스트하고 코드가 정확히 작동하는지 검증하는 함수입니다. 테스트 케이스는 일반적으로 Jest가 제공하는 test 함수를 사용하여 작성되는데, 이 함수는 테스트 케이스의 문자열 설명을 첫 번째 인수로 사용하고 테스트할 코드를 포함하는 함수를 두 번째 인자로 사용합니다.
  • Run test cases: 제스트로 테스트 케이스를 실행시키기 위해서 jest 명령어를 터미널에서 사영할 수 있습니다. jest 명령어는 프로젝트의 모든 테스트 케이스를 실행하고 그에 대한 결과를 표시합니다.
  • Debug test cases: 만약 테스트 케이스가 실패한다면 Jest의 내장 디버깅 도구를 사용하여 그 이유를 파악할 수 있습니다. Jest 는 테스트 케이스를 분리하여 실행하는 기능, 테스트 케이스의 다른 지점에서 변수 값을 검사하는 기능, 테스트 케이스의 실행을 일시 중지하고 디버거를 사용하여 테스트 케이스와 상호작용 하는 기능 등 테스트 케이스를 디버깅하는 데 사용할 수 있는 여러 기능과 함수를 제공합니다.
  • Learn more about jest: Jest 에는 Jest를 계속 사용하면서 배울 수 있는 여러가지 기능이 더욱 많이 있습니다. Jest에 대한 자세한 내용은 설명서와 온라인으로 제공되는 자습서 및 리소스(https://jestjs.io/)에서 확인 할 수 있습니다.
❓ **“Let you recommend to me the best practice Jest Using TypeScript?”**
  • Use the @types/jest package: This package provides type definitions for Jest, which will help you avoid type errors when writing your tests. You can install it by running the following command:
npm install --save-dev @types/jest
  • Configure Jest to use TypeScript: To use TypeScript with Jest, you will need to configure Jest to use the ts-jest preprocessor. You can do this by adding the following section to your package.json file:
"jest": {
  "transform": {
    "^.+\\.tsx?$": "ts-jest"
  }
}
  • Write your tests in TypeScript: Once you have configured Jest to use TypeScript, you can write your tests in TypeScript just like you would write any other TypeScript code. You can use the .ts or .tsx file extension for your test files, and Jest will automatically compile them using ts-jest.
  • Use type-safe assertions: Jest includes a number of built-in assertions that you can use to test the behavior of your code. When using Jest with TypeScript, it's a good idea to use the expect function from Jest's @jest/types package, which provides type-safe assertions that can help you avoid type errors.
  • Use the -noEmit flag: When running Jest with TypeScript, it's a good idea to use the -noEmit flag to prevent Jest from generating any JavaScript files. This will ensure that Jest only runs the TypeScript compiler and does not generate any output files.

  • Use the @types/jest package: types/jest 패키지는 Jest에 대한 유형 정의를 제공하므로 테스트를 작성할 때 타입 유형 오류를 방지할 수 있습니다.
  • Configure jest to use TypeScript: Jest와 함께 TypeScript를 사용하기 위해선 ts-jest 전처리기를 Jest가 사용하도록 구성해야 합니다.
  • Write your tests in TypeScript: TypeScript를 사용하기 위한 Jest를 구성했으므로 다른 TypeScript 코드를 작성하는 것처럼 TypeScript로 테스트를 작성할 수 있습니다. 테스트 파일을 위해 확장자 .ts.tsx 를 사용할 수 있고, Jest는 ts-jest 를 사용하여 자동으로 컴파일합니다.
  • Use type-safe assertions: Jest는 코드의 동작을 테스트하기 위한 다양한 내장 단언(assertions)를 포함하고 있습니다. TypeScript와 함께 Jest를 사용할 때 @jest/types package의 type-safe한 assertion를 제공하여 타입 에러를 회피하도록 도와주는 expect 함수를 사용하는것이 좋습니다.
  • Use the --noEmit flag: TypeScript와 함께 Jest를 실행시킬 때 Jest가 다른 자바스크립트 파일을 생성하지 못하도록 --noEmit 플래그를 사용하는 것이 좋습니다. 이 플래그는 Jest가 오직 TypeScript 컴파일러만 실행하고 다른 출력 파일들을 생성하지 않는 것을 보장합니다.
❓ **“Let you recommend to me the best practice Jest documentation Using TypeScript well-documented?”**
  • Jest documentation: The Jest documentation is the main source of information about Jest and is available at https://jestjs.io/. The documentation includes detailed guides on how to use Jest, as well as information about the various features and APIs that are available.
  • Jest with TypeScript guide: The Jest documentation includes a specific guide on how to use Jest with TypeScript, which can be found at https://jestjs.io/docs/en/getting-started#using-typescript. This guide covers topics such as setting up Jest to work with TypeScript, writing tests in TypeScript, and using type-safe assertions with Jest.
  • ts-jest documentation: Jest uses the ts-jest preprocessor to compile TypeScript code when running tests. The ts-jest documentation, which is available at https://kulshekhar.github.io/ts-jest/, includes detailed information about how to configure and use ts-jest, as well as information about the various options that are available.

  • Jest documentation: Jest 문서는 Jest에 대한 주요 정보 소스이며 https://jestjs.io/ 에서 이용 가능합니다. Jest 문서는 Jest 사용 방법에 대한 자세한 아내와 함께 사용 가능한 다양한 기능과 API에 대한 정보를 제공합니다.
  • Jest with TypeScript guide: Jest 문서는 TypeScript와 함께 사용하는 Jest를 어떻게 사용하는지에 대한 구체적인 가이드를 포함하고 있으며 가이드는 https://jestjs.io/docs/en/getting-started#using-typescript에서 찾을 수 있습니다. 가이드는 Jest가 TypeScript가 함께 동작 하도록 Jest를 세팅하는 법, TypeScript로 테스트 작성하는 것과 type-safe한 assertions을 사용하는 것과 같은 항목을 다룹니다.
  • ts-jest documentation: Jest는 TypeScript 코드를 실행시킬 때 ts-jest 전처리기를 사용합니다. https://kulshekhar.github.io/ts-jest/ 에서 제공되는 ts-jest 문서에는 ts-jest 를 구성하고 사용하는 방법에 대한 자세한 정보와 함께 사용 가능한 다양한 옵션에 대한 정보가 포함되어 있습니다.
profile
노드 리액트 스프링 자바 등 웹개발에 관심이 많은 초보 개발자 입니다

0개의 댓글