Angular CLI − ng test Command



This chapter will discuss the Angular CLI ng test command, including its syntax, arguments, and options. It will also provide an example that demonstrates how to use this command in your project.

The "ng test" Command

The Angular CLI (Command Line Interface) provides a command called ng test, which is specific to Angular applications. It is used for running unit tests in an Angular application. When you run ng test, it compiles the application and launches a test runner (Karma), which executes the tests and provides feedback on the results.

Syntax

Following is the syntax of the Angular CLI "ng test" command −

ng test ng[project] [options]

Here,

  • project: The name of the project you want to run the tests for. If you have multiple Angular projects in a workspace, you can specify which project tests to run.
  • options: Various flags or configurations you can use to modify the behavior of the command.
Note! You can also use its shorthand form "ng t", where "t" stands for "test". This will work the same way as the full ng test command does.

Arguments

Sr.No. Argument & Description
1 <project>

The name of the project to test.

Options

Below is a list of a few commonly used options −

Sr.No. Option & Description
1 --browsers=browsers

Override which browsers tests are run against.

2 --codeCoverage=true|false

Output a code coverage report.

Default: false

3 --codeCoverageExclude

Globs to exclude from code coverage.

4 --configuration=configuration

A named build target, as specified in the "configurations" section of angular.json. Each named target is accompanied by a configuration of option defaults for that target. Setting this explicitly overrides the "--prod" flag

Aliases: -c

5 --help=true|false|json|JSON

Shows a help message for this command in the console.

Default: false

6 --include

Globs of files to include, relative to workspace or project root. There are 2 special cases −

  • when a path to directory is provided, all spec files ending ".spec.@(ts|tsx)" will be included.

  • when a path to a file is provided, and a matching spec file exists it will be included instead.

7 --karmaConfig=karmaConfig

The name of the Karma configuration file.

8 --main=main

The name of the main entry-point file.

9 --poll

Enable and define the file watching poll time period in milliseconds.

10 --polyfills=polyfills

The name of the polyfills file.

11 --preserveSymlinks=true|false

Do not use the real path when resolving modules.

Default: false

12 --prod=true|false

Shorthand for "--configuration=production". When true, sets the build configuration to the production target. By default, the production target is set up in the workspace configuration such that all builds make use of bundling, limited tree-shaking, and also limited dead code elimination.

13 --progress=true|false

Log progress to the console while building.

13 --progress=true|false

Log progress to the console while building.

14 --reporters

Karma reporters to use. Directly passed to the karma runner.

15 --sourceMap=true|false

Output sourcemaps.

Default: true

16 --tsConfig=tsConfig

The name of the TypeScript configuration file.

17 --watch=true|false

Run build when files change.

18 --webWorkerTsConfig=webWorkerTsConfig

TypeScript configuration for Web Worker modules.

Example

An example for ng test command is given below −

\>Node\>TutorialsPoint> ng test
...
WARN: ''app-goals' is not a known element:
1. If 'app-goals' is an Angular component, then verify that it is part of this module.
2. If 'app-goals' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'
Chrome 83.0.4103 (Windows 7.0.0): Executed 0 of 4 SUCCESS (0 secs / 0 secs)
...
AppComponent should render title FAILED
   TypeError: Cannot read property 'textContent' of null
      at <Jasmine>
      at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/app.component.spec.ts:33:51)
            ...
Chrome 83.0.4103 (Windows 7.0.0): Executed 1 of 4 (1 FAILED) (0 secs / 0.203 secs)
...
Chrome 83.0.4103 (Windows 7.0.0): Executed 2 of 4 (1 FAILED) (0 secs / 0.221 secs)
...
Chrome 83.0.4103 (Windows 7.0.0): Executed 4 of 4 (1 FAILED) (0 secs / 0.244 sec
Chrome 83.0.4103 (Windows 7.0.0): Executed 4 of 4 (1 FAILED) (0.282 secs / 0.244
 secs)
TOTAL: 1 FAILED, 3 SUCCESS

Now to fix failures update the app.component.spec.ts

app.component.spec.ts

import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
   beforeEach(async(() => {
      TestBed.configureTestingModule({
         imports: [
            RouterTestingModule
         ],
         declarations: [
            AppComponent
         ],
      }).compileComponents();
   }));

   it('should create the app', () => {
      const fixture = TestBed.createComponent(AppComponent);
      const app = fixture.componentInstance;
      expect(app).toBeTruthy();
   });
});

Now run the test command.

Example

An example is given below −

\>Node\>TutorialsPoint> ng test
...
WARN: ''app-goals' is not a known element:
1. If 'app-goals' is an Angular component, then verify that it is part of this m
odule.
2. If 'app-goals' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@
NgModule.schemas' of this component to suppress this message.'
Chrome 83.0.4103 (Windows 7.0.0): Executed 1 of 2 SUCCESS (0 secs / 0.053 secs)
...
Chrome 83.0.4103 (Windows 7.0.0): Executed 2 of 2 SUCCESS (0.097 secs / 0.073 se
cs)
TOTAL: 2 SUCCESS

ng test also opens the browser and displays the test status.

Unit Test
Advertisements