{ "id": "guide/testing", "title": "Testing", "contents": "\n\n\n
\n mode_edit\n
\n\n\n
\n \n

Testinglink

\n

Testing your Angular application helps you check that your app is working as you expect.

\n

Prerequisiteslink

\n

Before writing tests for your Angular app, you should have a basic understanding of the following concepts:

\n\n
\n

The testing documentation offers tips and techniques for unit and integration testing Angular applications through a sample application created with the Angular CLI.\nThis sample application is much like the one in the Tour of Heroes tutorial.

\n
\n

For the sample app that the testing guides describe, see the sample app.

\n

For the tests features in the testing guides, see tests.

\n
\n\n

Set up testinglink

\n

The Angular CLI downloads and installs everything you need to test an Angular application with the Jasmine test framework.

\n

The project you create with the CLI is immediately ready to test.\nJust run the ng test CLI command:

\n\n ng test\n\n

The ng test command builds the app in watch mode,\nand launches the Karma test runner.

\n

The console output looks a bit like this:

\n\n10% building modules 1/1 modules 0 active\n...INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/\n...INFO [launcher]: Launching browser Chrome ...\n...INFO [launcher]: Starting browser Chrome\n...INFO [Chrome ...]: Connected on socket ...\nChrome ...: Executed 3 of 3 SUCCESS (0.135 secs / 0.205 secs)\n\n

The last line of the log is the most important.\nIt shows that Karma ran three tests that all passed.

\n

A Chrome browser also opens and displays the test output in the \"Jasmine HTML Reporter\" like this.

\n
\n \"Jasmine\n
\n

Most people find this browser output easier to read than the console log.\nYou can click on a test row to re-run just that test or click on a description to re-run the tests in the selected test group (\"test suite\").

\n

Meanwhile, the ng test command is watching for changes.

\n

To see this in action, make a small change to app.component.ts and save.\nThe tests run again, the browser refreshes, and the new test results appear.

\n

Configurationlink

\n

The CLI takes care of Jasmine and Karma configuration for you.

\n

You can fine-tune many options by editing the karma.conf.js in the root folder of the project and\nthe test.ts files in the src/ folder.

\n

The karma.conf.js file is a partial Karma configuration file.\nThe CLI constructs the full runtime configuration in memory, based on application structure specified in the angular.json file, supplemented by karma.conf.js.

\n

Search the web for more details about Jasmine and Karma configuration.

\n

Other test frameworkslink

\n

You can also unit test an Angular app with other testing libraries and test runners.\nEach library and runner has its own distinctive installation procedures, configuration, and syntax.

\n

Search the web to learn more.

\n

Test file name and locationlink

\n

Look inside the src/app folder.

\n

The CLI generated a test file for the AppComponent named app.component.spec.ts.

\n
\n

The test file extension must be .spec.ts so that tooling can identify it as a file with tests (AKA, a spec file).

\n
\n

The app.component.ts and app.component.spec.ts files are siblings in the same folder.\nThe root file names (app.component) are the same for both files.

\n

Adopt these two conventions in your own projects for every kind of test file.

\n\n

Place your spec file next to the file it testslink

\n

It's a good idea to put unit test spec files in the same folder\nas the application source code files that they test:

\n\n\n

Place your spec files in a test folderlink

\n

Application integration specs can test the interactions of multiple parts\nspread across folders and modules.\nThey don't really belong to any part in particular, so they don't have a\nnatural home next to any one file.

\n

It's often better to create an appropriate folder for them in the tests directory.

\n

Of course specs that test the test helpers belong in the test folder,\nnext to their corresponding helper files.

\n\n

Set up continuous integrationlink

\n

One of the best ways to keep your project bug-free is through a test suite, but it's easy to forget to run tests all the time.\nContinuous integration (CI) servers let you set up your project repository so that your tests run on every commit and pull request.

\n

There are paid CI services like Circle CI and Travis CI, and you can also host your own for free using Jenkins and others.\nAlthough Circle CI and Travis CI are paid services, they are provided free for open source projects.\nYou can create a public project on GitHub and add these services without paying.\nContributions to the Angular repo are automatically run through a whole suite of Circle CI tests.

\n

This article explains how to configure your project to run Circle CI and Travis CI, and also update your test configuration to be able to run tests in the Chrome browser in either environment.

\n

Configure project for Circle CIlink

\n

Step 1: Create a folder called .circleci at the project root.

\n

Step 2: In the new folder, create a file called config.yml with the following content:

\n\nversion: 2\njobs:\n build:\n working_directory: ~/my-project\n docker:\n - image: circleci/node:10-browsers\n steps:\n - checkout\n - restore_cache:\n key: my-project-{{ .Branch }}-{{ checksum \"package-lock.json\" }}\n - run: npm install\n - save_cache:\n key: my-project-{{ .Branch }}-{{ checksum \"package-lock.json\" }}\n paths:\n - \"node_modules\"\n - run: npm run test -- --no-watch --no-progress --browsers=ChromeHeadlessCI\n - run: npm run e2e -- --protractor-config=e2e/protractor-ci.conf.js\n\n

This configuration caches node_modules/ and uses npm run to run CLI commands, because @angular/cli is not installed globally.\nThe double dash (--) is needed to pass arguments into the npm script.

\n

Step 3: Commit your changes and push them to your repository.

\n

Step 4: Sign up for Circle CI and add your project.\nYour project should start building.

\n\n

Configure project for Travis CIlink

\n

Step 1: Create a file called .travis.yml at the project root, with the following content:

\n\nlanguage: node_js\nnode_js:\n - \"10\"\n\naddons:\n chrome: stable\n\ncache:\n directories:\n - ./node_modules\n\ninstall:\n - npm install\n\nscript:\n - npm run test -- --no-watch --no-progress --browsers=ChromeHeadlessCI\n - npm run e2e -- --protractor-config=e2e/protractor-ci.conf.js\n\n

This does the same things as the CircleCI configuration, except that Travis doesn't come with Chrome, so use Chromium instead.

\n

Step 2: Commit your changes and push them to your repository.

\n

Step 3: Sign up for Travis CI and add your project.\nYou'll need to push a new commit to trigger a build.

\n\n

Configure project for GitLab CIlink

\n

Step 1: Create a file called .gitlab-ci.yml at the project root, with the following content:

\n\nimage: node:14.15-stretch\nvariables:\n FF_USE_FASTZIP: \"true\"\n\ncache:\n untracked: true\n policy: push\n key: ${CI_COMMIT_SHORT_SHA}\n paths:\n - node_modules/\n\n.pull_cached_node_modules:\n cache:\n untracked: true\n key: ${CI_COMMIT_SHORT_SHA}\n policy: pull\n\nstages:\n - setup\n - test\n\ninstall:\n stage: setup\n script:\n - npm ci\n\ntest:\n stage: test\n extends: .pull_cached_node_modules\n before_script:\n - apt-get update\n - wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb\n - apt install -y ./google-chrome*.deb;\n - export CHROME_BIN=/usr/bin/google-chrome\n script:\n - npm run test -- --no-watch --no-progress --browsers=ChromeHeadlessCI\n - npm run e2e -- --protractor-config=e2e/protractor-ci.conf.js\n\n

This configuration caches node_modules/ in the install job and re-uses the cached node_modules/ in the test job.

\n

Step 2: Sign up for GitLab CI and add your project.\nYou'll need to push a new commit to trigger a build.

\n

Step 3: Commit your changes and push them to your repository.

\n\n

Configure CLI for CI testing in Chromelink

\n

When the CLI commands ng test and ng e2e are generally running the CI tests in your environment, you might still need to adjust your configuration to run the Chrome browser tests.

\n

There are configuration files for both the Karma JavaScript test runner\nand Protractor end-to-end testing tool,\nwhich you must adjust to start Chrome without sandboxing.

\n

We'll be using Headless Chrome in these examples.

\n\n\nbrowsers: ['ChromeHeadlessCI'],\ncustomLaunchers: {\n ChromeHeadlessCI: {\n base: 'ChromeHeadless',\n flags: ['--no-sandbox']\n }\n},\n\n\n\nconst config = require('./protractor.conf').config;\n\nconfig.capabilities = {\n browserName: 'chrome',\n chromeOptions: {\n args: ['--headless', '--no-sandbox']\n }\n};\n\nexports.config = config;\n\n

Now you can run the following commands to use the --no-sandbox flag:

\n\n ng test --no-watch --no-progress --browsers=ChromeHeadlessCI\n ng e2e --protractor-config=e2e/protractor-ci.conf.js\n\n
\n

Note: Right now, you'll also want to include the --disable-gpu flag if you're running on Windows. See crbug.com/737678.

\n
\n

More info on testinglink

\n

After you've set up your app for testing, you may find the following testing guides useful.

\n\n\n \n
\n\n\n" }