Ora

How to Debug Angular Test Cases in IntelliJ?

Published in Angular Testing Debugging 5 mins read

Debugging Angular test cases in IntelliJ is a streamlined process that leverages the IDE's powerful debugger integration with Node.js, npm, and browser environments. By setting up the correct run/debug configuration, you can easily step through your test code, inspect variables, and identify issues.

Prerequisites

Before you begin, ensure you have the following installed and configured:

  • IntelliJ IDEA Ultimate Edition: Required for full JavaScript/TypeScript and Node.js debugging capabilities.
  • Node.js and npm: Essential for running Angular CLI commands and managing project dependencies.
  • Angular CLI: Used to generate and run Angular projects and tests.

Core Debugging Steps for Angular Tests

Follow these steps to set up and debug your Angular test cases in IntelliJ:

1. Set Breakpoints

The first step in any debugging session is to identify where you want the execution to pause.

  • Locate your test file: Open the .spec.ts file corresponding to the component or service you wish to test.
  • Place breakpoints: Click in the left gutter next to the line number where you want the debugger to stop. A red circle will appear, indicating an active breakpoint. You can set breakpoints within your test methods (it(), beforeEach(), etc.) or even within the actual component/service code that your test exercises.

2. Create an npm Run/Debug Configuration

IntelliJ uses run/debug configurations to define how to execute or debug various tasks. For Angular tests, an npm configuration is typically used.

  1. Go to Run > Edit Configurations....

  2. Click the + icon in the top-left corner and select npm.

  3. Configuration Name: Give it a descriptive name, e.g., "Debug Angular Tests."

  4. Package Manager: Select npm.

  5. Commands: Enter test. This corresponds to the test script defined in your package.json file, which usually calls ng test.

  6. Script: Leave this blank if Commands is test. If your package.json has a custom script like "my-test": "ng test --no-watch", you would enter my-test here and leave Commands blank.

  7. package.json location: In the Configuration tab, specify the absolute path to your project's package.json file. IntelliJ usually pre-fills this correctly based on your project structure.

  8. Arguments (Optional but Recommended): For Angular tests, it's highly recommended to add specific arguments to ng test for a better debugging experience. Add these to the Arguments field:

    • --no-watch: Prevents Karma from continuously watching for file changes, which can interfere with debugging.
    • --browsers=ChromeDebugging: This instructs Karma to launch a special Chrome instance that waits for an external debugger connection. This is crucial for IntelliJ to attach to the browser's JavaScript engine.

    Your complete arguments might look like: --no-watch --browsers=ChromeDebugging

  9. Working directory: Ensure this points to your project's root directory.

3. Start the Debugging Session

Once your npm run/debug configuration is set up:

  1. Select your newly created "Debug Angular Tests" configuration from the dropdown in the top-right corner of IntelliJ.
  2. Click the Debug (bug icon) button next to the dropdown.

IntelliJ will launch the npm test command. Karma will start, open a Chrome instance (waiting for the debugger), and IntelliJ will automatically attach its debugger to this browser process. When the test execution reaches one of your breakpoints, it will pause, and IntelliJ will switch to the Debug tool window.

Utilizing the Debugger Tools

While paused at a breakpoint, IntelliJ's Debug tool window offers several powerful features:

  • Frames: View the call stack to understand how you reached the current point.
  • Variables: Inspect the values of all local and global variables in the current scope.
  • Watches: Add specific expressions to watch their values as you step through the code.
  • Console: Interact with the browser's JavaScript environment, execute code, and log output.
  • Step Controls:
    • F8 (Step Over): Execute the current line and move to the next.
    • F7 (Step Into): If the current line is a function call, step into that function's code.
    • Shift+F8 (Step Out): Exit the current function and return to the calling function.
    • F9 (Resume Program): Continue execution until the next breakpoint or the end of the program.

Here's a quick reference for common debugger actions:

Action Keyboard Shortcut (macOS) Keyboard Shortcut (Windows/Linux) Description
Toggle Breakpoint Cmd+F8 Ctrl+F8 Add or remove a breakpoint on the current line.
Step Over F8 F8 Execute the current line, skipping function calls.
Step Into F7 F7 Enter a function call on the current line.
Step Out Shift+F8 Shift+F8 Complete the current function and return to caller.
Resume Program F9 F9 Continue execution until the next breakpoint.
Stop Debugging Cmd+F2 Ctrl+F2 Terminate the debugging session.

Tips for Effective Debugging

  • Focus on Specific Tests: To debug a single test or a group of tests, use fdescribe or fit in your .spec.ts files to temporarily isolate them. Remember to remove these before committing your code.
  • Inspect Console Output: The IntelliJ Run/Debug console will show output from Karma and any console.log statements in your tests or application code.
  • Source Maps: Ensure source maps are enabled in your Angular project (they are by default). This allows the debugger to map compiled JavaScript back to your original TypeScript code.

By following these steps, you can efficiently debug your Angular test cases directly within IntelliJ, making it easier to pinpoint and resolve issues in your application logic and tests.