This tutorial is adapted from the Web Age course Angular Unit Testing with Jasmine.
1.1 Software Testing
1.2 Types of tests
TestType |
Description |
---|---|
UX | User Experience |
UAT | User Acceptance |
Regression | New/Changed Features |
Stress | Extreme Throughput/Capacity |
Performance | Measure Under Varying Load |
Functional | Functional Requirements |
Integration | Multiple Components |
Unit | Individual Unit |
1.3 Test Pyramid
1.4 Unit Tests
1.5 Jasmine, Karma and Angular
1.6 Jasmine
1.7 Karma
- Test runner works with Jasmine (and other test tools)
- Runs test in real or simulated devices/browsers
- Controlled by command line or IDE
- Automatically runs tests on changes to source files
- Works with CI/CD tools
1.8 Basic Terminology
1.9 Test Suite
app.salestax.service.ts app.salestax.service.spec.ts
The function takes two parameters:
describe( 'a test suite', () => { // insert tests/specs here })
Note
Test suites may be nested
1.10 Spec
it( 'should add 2+2=4', () => {
expect(2+2).toBe(4)
})
Note
If the arrow function evaluates to true the spec succeeds.
1.11 Assertion
expect(service.isComplete()).toBeTrue()
expect(app.title).toEqual('Jasmine Test')
1.12 Matchers
-
toBe toBeUndefined toBeCloseTo toContain toBeDefined toEqual toBeFalsey toHaveBeenCalled toBeGreaterThan toHaveBeenCalledWith toBeLessThan toMatch toBeNaN toThrow toBeNull toThrowError toBeTruthy
Note
In addition to these pre-defined matchers, you can define your own matcher function.
1.13 Setup and Teardown
The arrow function performs the action
Functions
1.14 A Test Suite
describe('a test suite', () => { let order = { 'widgets':2 } beforeAll( () => { order = 4 }) beforeEach( () => { order = 6 }) it( 'should have widgets', () => { expect(order).toBeTruthy() // succeeds }) it( 'should have widgets', () => { expect(order).toBe(4) // fails }) })