Pragmatic Testing in AdonisJS with Japa #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.

Created by
@tomgobich
Published

Notes Used to Craft This Lesson

I'll preface by saying Japa is not restricted to AdonisJS and is a great testing option for any NodeJS application using ESM and TypeScript.

It comes with everything you'll need for testing backend applications, including assertions, file system helpers, snapshot testing, API testing, and even Playwright for browser testing.

As I mentioned, Japa will come preconfigured in most AdonisJS starter kits. To ensure we're focusing on testing, I have a repository prepared for this series with code prewritten, which we'll be able to add tests for.

Nothing has been altered when it comes to Japa or its integration within the project. So, let's start by pulling the repository down.

npm init adonisjs@latest -- -K="adocasts/testing-with-japa-starter"
Copied!

Once cloned down, take a second to copy the .env.example into a new .env file. If you'd like to actually run and inspect the application, you'll also want to take a second to set up SMTP. I like to use Mailtrap to set up a quick and easy sandbox inbox for development.

Within this project, Japa resides in the tests directory. The bootstrap file we have in here already is where Japa's plugins, setup, and teardown instructions reside.

In addition to the bootstrap file, we'll also have a folder for each of our test suites, which provides a way to group tests by their type. The most common test suites are functional and unit. Our functional tests will go within the functional suite, and unit tests will go into the unit suite.

Unit tests are for testing single functions in isolation, and our AdonisJS server will not be started for these. These are great for testing complex business logic.

Functional tests are for testing from the outside in, like performing entire HTTP requests and verifying we get an expected response. For these, our AdonisJS server will be started, and we can make actual requests to defined URLs within our application.

We can configure this for other suites as well, if needed, directly within the configureSuite function in the bootstrap file. By default, the AdonisJS server will run for browser, functional, and end-to-end (e2e) suites.

Join the Discussion 0 comments

Create a free account to join in on the discussion
robot comment bubble

Be the first to comment!