# Jest

# Introduction

This guide will take you through the process of installing Jest, a popular JavaScript testing framework, on an existing Ts.ED project. Jest provides a comprehensive solution for testing JavaScript and TypeScript code, and it integrates seamlessly with Ts.ED. Follow the steps below to incorporate Jest into your project.

# Installation

Open your project's terminal where you usually run npm commands. Run the following npm command to install Jest and its required dependencies:

    This command installs Jest, the TypeScript Jest transformer (ts-jest), and the Jest TypeScript types.

    # Create Jest Configuration

    Create a Jest configuration file named jest.config.ts at the root of your project. Copy and paste the following content into the file:

    export default {
      // Automatically clear mock calls and instances between every test
      clearMocks: true,
    
      // Indicates whether the coverage information should be collected while executing the test
      collectCoverage: true,
    
      // An array of glob patterns indicating a set of files for which coverage information should be collected
      // collectCoverageFrom: undefined,
    
      // The directory where Jest should output its coverage files
      coverageDirectory: "coverage",
    
      // An array of regexp pattern strings used to skip coverage collection
      coveragePathIgnorePatterns: ["index.ts", "/node_modules/"],
    
      // An array of file extensions your modules use
      moduleFileExtensions: ["js", "json", "jsx", "ts", "tsx", "node"],
    
      // The test environment that will be used for testing
      testEnvironment: "node",
    
      // The glob patterns Jest uses to detect test files
      testMatch: ["**/src/**/__tests__/**/*.[jt]s?(x)", "**/src/**/?(*.)+(spec|test).[tj]s?(x)"],
      // A map from regular expressions to paths to transformers
      transform: {
        "\\.(ts)$": "ts-jest"
      }
    };
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29

    This configuration file specifies Jest settings for TypeScript, including the test file patterns, transformation rules, and other essential configurations.

    # Update package.json with Test Scripts

    Add the following example of test scripts to your package.json file:

    {
      "scripts": {
        "test": "jest",
        "test:watch": "jest --watch",
        "test:cov": "jest --coverage",
        "test:debug": "node --inspect-brk -r ts-node/register node_modules/.bin/jest --runInBand",
        "test:e2e": "jest --config ./jest.config.e2e.js"
      }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    These scripts allow you to run various Jest commands, such as running tests, watching for changes, generating coverage reports, debugging tests, and running end-to-end tests with a custom configuration.

    Congratulations! You have successfully integrated Jest into your existing Ts.ED project, and you're ready to leverage the provided scripts for efficient testing.

    Last Updated: 10/24/2024, 6:33:40 AM

    Other topics