Setting Up Jasmine and Karma / Jest
Angular applications include a testing environment by default.
Two popular setups:
| Tool | Purpose |
|---|---|
| Jasmine | Testing framework |
| Karma | Test runner |
| Jest | Faster alternative to Karma |
Angular CLI usually installs Jasmine + Karma automatically.
Running Tests
Section titled “Running Tests”13.1 Setting Up Jasmine and Karma / Jest
Section titled “13.1 Setting Up Jasmine and Karma / Jest”Angular applications include a testing environment by default.
Two popular setups:
| Tool | Purpose |
|---|---|
| Jasmine | Testing framework |
| Karma | Test runner |
| Jest | Faster alternative to Karma |
Angular CLI usually installs Jasmine + Karma automatically.
Running Tests
Section titled “Running Tests”ng testThis command:
- builds the test environment
- launches Karma
- runs all
*.spec.tsfiles
Installing Jest (Optional)
Section titled “Installing Jest (Optional)”npm install jest jest-preset-angular @types/jestExample Test File
Section titled “Example Test File”describe("Simple Test", () => { it("should add numbers correctly", () => { const result = 2 + 3;
expect(result).toBe(5); });});Explanation
Section titled “Explanation”Key concepts:
describe()defines a test suiteit()defines a test caseexpect()verifies results
ng test This command:
- builds the test environment
- launches Karma
- runs all
*.spec.tsfiles
Installing Jest (Optional)
Section titled “Installing Jest (Optional)”npm install jest jest-preset-angular @types/jestExample Test File
Section titled “Example Test File”describe("Simple Test", () => { it("should add numbers correctly", () => { const result = 2 + 3;
expect(result).toBe(5); });});Explanation
Section titled “Explanation”Key concepts:
describe()defines a test suiteit()defines a test caseexpect()verifies results