Pragmatic Testing in AdonisJS with Japa

Throughout this series, we'll introduce the Japa testing framework, which comes prepackaged with most AdonisJS starter kits. We'll understand how it integrates with AdonisJS, the CLI options it provides, it's assertion library, plugins, and more.

Difficulty Beginner
What's Inside 30 Lessons
Viewing Time 4h 6m
Sign up to save progress
1

Module 1
The Foundations

1.0

Why Bother Testing?

In this lesson, we'll discuss what the benefits of testing are, what we should focus on when testing, and give a brief introduction to this series.

2m 10s
1.1

The AdonisJS Testing Stack

In this lesson, we'll pull down the starter project for this series and use it to discuss how Japa is integrated within AdonisJS projects.

6m 45s
1.2

Our First Test

In this lesson, we'll write our first test with Japa and learn about spec files, as well as the three key components of crafting a good test: arrange, act, and assert.

5m 19s
1.3

Running Tests with the Test Runner

In this lesson, we'll learn about the various ways we can use the Ace CLI and Japa's test runner to execute all, some, or individual tests.

6m 14s
2

Module 2
Japa Fundamentals

2.0

Making Assertions

In this lesson, we'll get comfy with making assertions against actual and expected values. We'll step through and demo several highly used assertion methods to see exactly how they work.

9m 43s
2.1

Naming Tests

In this lesson, we'll discuss how to quickly write a good test name in a single, brief sentence.

2m 52s
2.2

Group & Test Lifecycle Hooks

In this lesson, we'll walk through a number of lifecycle hooks we can use on a per-group and per-test basis. We'll also discuss how we can run a hook for each test and how setup hooks allow a convenient cleanup method.

5m 37s

Testing Async Code & Exceptions

In this lesson, we'll cover testing asynchronous code in AdonisJS using Japa. Learn to assert successful promises with both async/await and promise chaining. We'll then discuss testing rejected promises and what to watch for to prevent false positives.

13m 32s

Data-Driven Tests with Datasets

In this lesson, learn to write DRY tests using Japa's datasets! Define an array or callback function of data (primitives or objects) to run a single test multiple times, enabling TypeScript inference, and using interpolation in the test name for context.

10m 47s

Filtering & Controlling Test Runs

In this lesson, learn how to control Japa's test execution. Ignore tests using skip, only run specific tests using pinning, and also how assigning tags can powerfully allow us to easily filter tests across specs.

14m 8s
3

Module 3
Unit Testing Services

Testing a Simple Service

In this lesson, we'll focus on Unit Testing in AdonisJS using Japa. Learn how to write isolated tests for a service's business logic, including asserting correct results and handling expected exceptions.

7m 18s

Introduction to Mocks & Dependency Swapping

In this lesson, we'll cover Unit Testing services with dependencies. Learn to create a mock for a dependency and use the AdonisJS IoC Container to replace the real dependency with your mock, ensuring isolated and side-effect-free testing.

12m 15s

Introduction to Fakes

In this lesson, we'll use AdonisJS Fakes in Japa for isolated testing of service dependencies (like email). Learn to put the mail service in fake mode, call the service method, use built-in assertions, and ensure proper cleanup.

5m 55s

Unit Testing Event Listeners

In this lesson, learn how to unit test AdonisJS Event Listeners with Japa. Use the Mail Fake to isolate the test, instantiate the listener, manually call the listener and pass data, and use assertions to confirm the expected email was queued.

3m 32s

Testing Ace Console Commands

In this lesson, we'll learn how to test AdonisJS Ace Commands with Japa. We'll cover how to instantiate and execute the command, while passing arguments and capturing prompts. We'll also discuss asserting logged statements sent from the command.

9m 7s
4

Module 4
Functional & API Testing

Functional vs Unit Testing

In this lesson, we'll distinguish between Unit and Functional testing. We'll discuss how Unit tests are for isolated code and are lightweight, while Functional tests boot the HTTP server to test the entire application stack from request to response.

1m 5s

Meet the API Client

In this lesson, learn how to use Japa's API Client for Functional Testing of AdonisJS routes. We'll install and register the plugin, and use the client in tests to make requests and assert against their responses.

6m 37s

Testing GET Endpoints

In this lesson, we'll write Functional Tests to test an AdonisJS route using the Japa API Client. Learn to test happy paths by asserting successful responses and sad paths by asserting purposefully failed responses.

7m 38s

testing VineJS Validations & Working with CSRF

In this lesson, learn Functional Testing for POST requests. We'll cover sending JSON and form payloads and how to assert validation errors. We'll also discuss how to set up and use CSRF protection with the Session and Shield plugins.

14m 29s

Asserting JSON Structures

In this lesson, we'll learn how to test fetching a list of items functionally. Assert a successful response, confirm the body is as we expect, check object structures, and loosely validate response data.

3m 4s

Asserting OpenAPI Specifications

In this lesson, we'll learn how to validate AdonisJS API responses against an OpenAPI Specification, like those generated by Swagger APIs, using Japa's OpenAPI Assertions plugin.

7m 6s

Testing File Uploads

In this lesson, we'll learn how to test AdonisJS file uploads using Drive Fakes and the Japa API Client. We'll generate fake files for testing, use a multipart request, assert successes, test validation failures, and impress the importance of cleanup.

13m 2s
5

Module 5
Testing with the Database & Lucid

Database Test Runner Hooks

In this lesson, we'll learn how to set up a dedicated test database to ensure clean, isolated testing. We'll configure a separate SQLite database using a test environment variable and set up Japa runner hooks to automatically migrate and seed our database

5m 54s

Testing Database-Driven Endpoints

In this lesson, we'll learn how to functionally test a registration endpoint using a test database. We'll learn about global transactions and how we can use them to easily reset our database between tests and confirm database state with Lucid.

9m 49s

Testing with Lucid Model Factories

In this lesson, we'll learn about AdonisJS Model Factories and how we can use them to create realistic, yet fake, test data. We'll create a few factories we'll use in this series and learn how we can stub or create data with them in tests.

10m 1s

Testing Model Logic

In this lesson, we'll learn how to Unit Test AdonisJS Model logic using Japa. We'll test a password reset token's validity to show instance-level tests. Then, we'll test its generate and verify methods to show static methods.

10m 28s
6

Module 6
Testing Authentication & Authorization

The Auth Plugin

In this lesson, we'll learn how to test authenticated routes in AdonisJS/Japa. First, we'll install and register the Auth API Client plugin. Then, we'll learn how to use our User Factory to create a user and login as them for our test's request.

5m 15s

Testing Auth Protected Routes

In this lesson, we'll learn how to test auth-protected routes via the auth middleware and non-auth-protected routes via the guest middleware. We'll ensure our user is redirected appropriately and shown a flash message where applicable

11m 27s

Testing Authorization with Bouncer

In this lesson, we'll learn to test AdonisJS Authentication with Bouncer for actions like deleting a post. We'll cover happy paths where authorization is granted and sad paths where authorization is denied and the action is forbidden.

8m 47s

Piecing It All Together

In this lesson, we'll piece everything we've learned thus far together and create functional tests for a route allowing the authenticated user to change their email. This encompasses authentication, email sending, database records, and more

16m 27s
7

Module 7
A Quick Look at Browser Testing

The Browser Client

In this lesson, we'll softly introduce Browser Testing, which allows powerful DOM assertions, in AdonisJS using Japa's Browser Client and Playwright. We'll get everything installed and configured and write our first simple test.

Coming soon

Testing Browser Form Submissions & Validation Feedback

In this lesson, we'll learn how to test form submissions and validation using Japa's Browser Client and Playwright. We'll programmatically fill and submit our form and assert the visibility and contents of our validation errors.

Coming soon

Authentication in Browser Tests

In this final lesson, we'll learn how to use AdonisJS Authentication with the Browser Client. We'll install and configure the Session and Auth browser plugins and visit an auth-protected page to show how it all works.

Coming soon