Ready to get started?

Join Adocasts Plus for $8/mo, or sign into an existing Adocasts Plus account, to get access to all of our lessons.

robot mascot smiling

Pragmatic Testing in AdonisJS with Japa #7.2

Authentication in Browser Tests

In This Lesson

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.

Created by
@tomgobich
Published

Finally, let's quickly cover authentication within browser tests. Similar to the API Client, the AdonisJS Auth and Session packages also provide plugins for the Browser Client. So, since we're using session authentication,n we'll want to add both the session and auth plugins.

// tests/bootstrap.ts
import { sessionBrowserClient } from "@adonisjs/session/plugins/browser_client";
import { authBrowserClient } from "@adonisjs/auth/plugins/browser_client";

export const plugins: Config["plugins"] = [
  assert(),
  openapi({
    schemas: [new URL("../docs/openapi.json", import.meta.url)],
  }),
  apiClient(),
  browserClient({
    runInSuites: ["browser"],
  }),
  pluginAdonisJS(app),
  sessionApiClient(app),
  authApiClient(app),
++  sessionBrowserClient(app),
++  authBrowserClient(app),
  shieldApiClient(),
  disallowPinnedTests({
    disallow: !!process.env.CI,
  }),
];

Perfect! These both add their methods to the browserContext. This provides a way to operate multiple pages with the same context so that our session or auth persists if an action taken on one page opens another page.

Join the Discussion 0 comments

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

Be the first to comment!