Testing components using Jest

The page provides instructions for testing components using Jest.

Running Jest tests

CT5 uses Jest to run unit tests. Each CT5 component directory includes a tests directory for unit tests.

To run unit tests for all components in your application, execute the command below from the root directory of your application:

$ yarn test

Debugging Jest tests

You can debug Jest tests using Google Chrome, IntelliJ IDEA, or Visual Studio Code.

Debug using Google Chrome Developer Tools

To debug application tests using Google Chrome Developer Tools, follow the steps below:

  1. From the root directory of your application, run the command below:

    $ yarn run test:debug

    The command outputs a chrome-devtools:// URL

  2. Open Google Chrome. Paste the chrome-devtools:// URL into the address bar, or navigate to chrome://inspect and select your app.

The test:debug script uses the --inspect-brk option to start the Node.js Inspector. If the test:debug script fails for you, then edit your application’s packages.json file and use the alternative definition for the test:debug script below:
"scripts": {
    
    "test:debug": "node --inspect --debug-brk ./node_modules/jest-cli/bin/jest.js -i --env jest-environment-node-debug"
    
}

Debug using IntelliJ IDEA

To debug application tests using IntelliJ IDEA, follow the steps below:

  1. Install the Node.js plugin from the JetBrains plugin repository

  2. Click Run > Debug > Edit Configurations

  3. Click + > Jest

  4. Configure the following options:

    Option Value

    Jest package

    the path to the node_modules/jest-cli directory in your CT5 application directory

    Working directory

    the path to your CT5 application directory

    Jest options

    --runInBand

Debug using Visual Studio Code

To debug application tests using Microsoft Visual Studio Code, follow the steps below:

  1. Click File > Open Folder and open the directory of your CT5 application

  2. Click Debug > Run Configurations

  3. If prompted to select an environment, select Node.js

  4. From the list of possible run configurations, select Launch Program

  5. Edit the Launch Program configuration:

    {
        "type": "node",
        "request": "launch",
        "name": "Debug Jest tests",
        "program": "${workspaceRoot}/node_modules/jest-cli/bin/jest.js",
        "args": "--runInBand"
    }
  6. Click Debug > Start Debugging