Skip to content
LogoLogo

End-to-end tests

An app's end-to-end tests sign in with no dialog, and send transactions on Tempo testnet.

Overview

A test in a browser cannot sign in through Tempo ID's dialog. The dialog plugin of accounts/test replaces the dialog plugin in a test build. Its sign-in shows no dialog, and sends no request to Tempo ID.

A local Tempo Account signs the key authorization of a new access key, as Tempo ID's cloud wallet does after a sign-in. The session then holds the access key, and the app sends transactions on the chain as it does in production.

The demo signs in with the plugin, funds the new Tempo Account from the testnet faucet, and sends 1 OUSD.

Sign in with no dialogDemo

Walkthrough

These steps test a React app built with Vite, with Playwright.

Install the packages

@playwright/test runs the tests in a browser.

Swap the dialog plugin

A build with VITE_E2E uses the dialog plugin of accounts/test. It has the ID dialog, so accounts.dialog.signIn signs in with it, and the app's code stays the same. Vite replaces import.meta.env.VITE_E2E at build time, so a production bundle holds none of accounts/test.

tempo.config.ts
import { Accounts } from 'accounts'
import { dialog, viem } from 'accounts/plugins'
import { react } from 'accounts/react'
import * as test from 'accounts/test'
 
export const accounts = Accounts.create({
  // Each sign-in requests an access key that sends up to 100 OUSD for a day.
  accessKey: () => ({
    expiry: '1d',
    limits: [{ limit: '100', token: 'ousd' }],
  }),
  chain: 'testnet',
  plugins: [
    react(),
    import.meta.env.VITE_E2E ? test.dialog() : dialog(), 
    viem(),
  ],
})

Sign in from a click

accounts.dialog.signIn creates the access key, sets the session, and runs the plugins' afterSignIn hooks, as a sign-in in the dialog does. Each sign-in of the same instance signs in the same Tempo Account.

import { accounts } from './tempo.config.js'
 
export function App() {
  const { session } = accounts.useSession()
 
  if (!session)
    return (
      <button onClick={() => accounts.dialog.signIn()} type="button">
        Sign in
      </button>
    )
 
  return <p>Signed in as {session.user.email}</p>
}

Serve the test build

Playwright's webServer builds the app with VITE_E2E, and serves the build before the tests run.

playwright.config.ts
import { defineConfig } from '@playwright/test'
 
export default defineConfig({
  testDir: 'e2e',
  use: { baseURL: 'http://localhost:4173' },
  webServer: {
    command: 'VITE_E2E=true vite build && vite preview --port 4173',
    url: 'http://localhost:4173',
  },
})

Write the test

The test clicks the app's own button, and reads the session that the page shows.

e2e/sign-in.test.ts
import { expect, test } from '@playwright/test'
 
test('signs in', async ({ page }) => {
  await page.goto('/')
 
  await page.getByRole('button', { name: 'Sign in' }).click()
 
  await expect(page.getByText('Signed in as test@example.com')).toBeVisible()
})

Use cases

Each use case sets an option of the dialog plugin of accounts/test, or of its sign-in, except the last, which signs in through Tempo ID's dialog.

Use a funded account

privateKey names the Tempo Account's private key, such as one that a global setup funded. Without it, each instance signs in a new account, and each page load creates a new instance.

tempo.config.ts
import { Accounts } from 'accounts'
import { dialog, viem } from 'accounts/plugins'
import * as test from 'accounts/test'
 
export const accounts = Accounts.create({
  chain: 'testnet',
  plugins: [
    import.meta.env.VITE_E2E
      ? test.dialog({ privateKey: import.meta.env.VITE_E2E_PRIVATE_KEY }) 
      : dialog(),
    viem(),
  ],
})

Name the user

user sets the signed-in user. Without it, the email is test@example.com, and the ID is test: followed by the account's address.

tempo.config.ts
import { Accounts } from 'accounts'
import { dialog } from 'accounts/plugins'
import * as test from 'accounts/test'
 
export const accounts = Accounts.create({
  chain: 'testnet',
  plugins: [
    import.meta.env.VITE_E2E
      ? test.dialog({ user: { email: 'ada@example.com', id: 'user_ada' } }) 
      : dialog(),
  ],
})

Request an access key without bounds

accessKey of a sign-in names the access key's permissions, in place of the instance's. An access key with scopes: 'any' and no limits makes any call and spends any amount, so a test needs no new access key for each contract. Tempo ID refuses such an access key on a page, so only a test build requests it.

tempo.config.ts
import { Accounts } from 'accounts'
import { dialog, viem } from 'accounts/plugins'
import * as test from 'accounts/test'
 
export const accounts = Accounts.create({
  accessKey: import.meta.env.VITE_E2E
    ? { expiry: '1h', scopes: 'any' }
    : { expiry: '1d', limits: [{ limit: '100', token: 'ousd' }] },
  chain: 'testnet',
  plugins: [import.meta.env.VITE_E2E ? test.dialog() : dialog(), viem()],
})

Turn off the retry of transactions

The viem plugin's retry signs in again for a refused transaction, such as a transfer above the limit. That sign-in opens Tempo ID's dialog, which a test cannot complete. A test build sets retry: false, so the transaction rejects with the node's error.

tempo.config.ts
import { Accounts } from 'accounts'
import { dialog, viem } from 'accounts/plugins'
import * as test from 'accounts/test'
 
export const accounts = Accounts.create({
  chain: 'testnet',
  plugins: [
    import.meta.env.VITE_E2E ? test.dialog() : dialog(),
    viem({ retry: !import.meta.env.VITE_E2E }), 
  ],
})

Sign in without an access key

accessKey: false signs in with no access key. The session still holds the Tempo Account, which signs nothing.

SignIn.tsx
import { accounts } from './tempo.config.js'
 
export function SignIn() {
  return (
    <button
      onClick={() => accounts.dialog.signIn({ accessKey: false })}
      type="button"
    >
      Sign in
    </button>
  )
}

Connect wagmi

The tempoId connector takes the plugin among its plugins. Its connect signs in with the plugin's dialog.signIn.

config.ts
import { dialog } from 'accounts/plugins'
import * as test from 'accounts/test'
import { tempoId } from 'accounts/wagmi'
import { tempoModerato } from 'viem/tempo/chains'
import { createConfig, http } from 'wagmi'
 
export const config = createConfig({
  chains: [tempoModerato],
  connectors: [
    tempoId({
      plugins: [import.meta.env.VITE_E2E ? test.dialog() : dialog()], 
    }),
  ],
  transports: { [tempoModerato.id]: http() },
})

Sign in at a local Tempo ID

A test that covers Tempo ID's dialog itself signs in at an id-dev instance of Tempo ID, whose inbox returns each sign-in code. accounts/test/playwright holds the selectors of the dialog. dialog returns the dialog's frame once it holds still, signIn completes the sign-in form, and code reads the inbox.

e2e/dialog.test.ts
import { expect, test } from '@playwright/test'
import { code, dialog, signIn } from 'accounts/test/playwright'
 
test('signs in through the dialog', async ({ page }) => {
  const email = `test+${crypto.randomUUID()}@example.org`
  await page.goto('/')
  await page.getByRole('button', { name: 'Sign in' }).click()
 
  await signIn(await dialog(page), {
    code: (email) => code({ email, url: 'http://localhost:8787' }),
    email,
  })
 
  await expect(page.getByText(`Signed in as ${email}`)).toBeVisible()
})

Next steps