Getting Started (Beginner Guide)

A beginner-friendly guide to testing Aurelia applications and components

Testing ensures your Aurelia application works correctly and continues to work as you make changes. This guide will help you get started with testing, from setting up your environment to writing your first tests.

Why Test?

Testing provides several benefits:

  • Confidence: Know your code works as expected

  • Refactoring Safety: Make changes without fear of breaking things

  • Documentation: Tests show how your code should be used

  • Bug Prevention: Catch issues before users do

  • Faster Development: Find and fix bugs early

Quick Start

1. Install Testing Dependencies

Most Aurelia projects come with testing dependencies already installed. If not, install them:

npm install --save-dev @aurelia/testing jest @types/jest

2. Configure Your Test Environment

Create a test setup file (e.g., test-setup.ts) to initialize Aurelia's testing platform:

3. Configure Jest

Update your jest.config.js:

Your First Test

Let's test a simple greeting component.

The Component

Create greeting.ts:

Create greeting.html:

The Test

Create greeting.spec.ts:

Run Your Tests

You should see all three tests pass!

Understanding the Test Structure

Let's break down what's happening:

1. createFixture

createFixture creates a test environment for your component:

2. Assertions

The fixture provides assertion helpers:

3. Cleanup

Always clean up after tests:

This prevents memory leaks and ensures test isolation.

Testing Common Scenarios

Testing User Input

Test a todo input component:

Testing Forms

Test a login form:

Testing Lists and Loops

Test a component with a repeater:

Testing Conditional Rendering

Test components with if/else:

Testing with Dependencies

Many components depend on services. Here's how to test them:

Component with Service

Testing with Mocks

Testing Best Practices

1. Write Descriptive Test Names

2. Test Behavior, Not Implementation

3. Keep Tests Independent

Each test should work on its own:

4. Use AAA Pattern

Organize tests with Arrange, Act, Assert:

5. Test Edge Cases

Common Testing Patterns

Setup and Teardown

Use beforeEach and afterEach for shared setup:

Testing Async Operations

Snapshot Testing

Test that component output hasn't changed unexpectedly:

Troubleshooting

"Platform not set" Error

Solution: Call bootstrapTestEnvironment() before creating fixtures:

Component Not Updating

Solution: Wait for the task queue to flush:

Element Not Found

Solution: Check that the element exists and use the correct selector:

Next Steps

Now that you understand the basics of testing:

Summary

Testing Aurelia applications is straightforward:

  1. Setup: Configure Jest and initialize the test platform

  2. Create Fixtures: Use createFixture to instantiate components

  3. Assert: Use assertion helpers to verify behavior

  4. Cleanup: Always call stop(true) to prevent memory leaks

Testing gives you confidence that your application works correctly and will continue to work as you make changes. Start testing today and build better, more reliable applications!

Last updated

Was this helpful?