Testing, Building and Deploying
Writing the app is only half the job: you also need confidence that it works (tests), small fast files for users (production build), the right settings for each environment, and a place to host it. Angular's CLI handles testing and building; the result is static files you can host almost anywhere, cheaply.
A first unit test
Angular projects come ready for unit tests. Run them with ng test. A test creates the component, changes something, and checks the screen.
Example
import { TestBed } from '@angular/core/testing';
import { Counter } from './counter';
describe('Counter', () => {
it('adds one when the button is clicked', async () => {
const fixture = TestBed.createComponent(Counter);
await fixture.whenStable();
const el: HTMLElement = fixture.nativeElement;
el.querySelector('button')!.click();
await fixture.whenStable();
expect(el.textContent).toContain('Count: 1');
});
});Building for production
Example
ng buildThis makes small, fast files in dist/<your-app>/browser: minified JavaScript, CSS and your index.html. Those files are all you upload.
Environment settings
Keep different API addresses for development and production, and choose with a build configuration.
Example
// src/environments/environment.ts
export const environment = { apiUrl: 'http://localhost:8080' };
// src/environments/environment.prod.ts
export const environment = { apiUrl: 'https://api.myshop.example' };Deploying
- Static hosting (Firebase Hosting, Netlify, Vercel, Cloudflare Pages, AWS S3 + CloudFront): upload the
browserfolder. - Set the host to send
index.htmlfor every unknown address, so deep links like/products/42work after a refresh. - Server-side rendering (
ng add @angular/ssr): pages are built on the server first, which helps SEO and the first load. - Automate it: a GitHub Actions workflow can run
ng testandng build, then upload, every time you push.
Never put secret keys in Angular code or environment files. Everything in the browser can be read by users. Secrets belong on your server.
Key points
- ng test runs unit tests; ng build makes production files.
- Use environment configurations for different API addresses.
- Host the browser folder on any static host; never put secrets in front-end code.
📝 Test yourself
1. Which command makes the production files?
2. Why must the host send index.html for unknown paths?
3. Fill in the command that runs unit tests:
ng
