End-to-End Tests With Playwright: Frontend, Backend and Database · Part 8 of 14 in the series
Part 8: End to End, Through All Layers
Build the frontend, let every worker's own server hand it out, and write five tests that go through browser, API and database at once. After this, no test touches your development setup any more.
End-to-End Tests With Playwright: Frontend, Backend and DatabasePart 8 of 14
On this page
Quest objectives
- Know what is left for end-to-end tests when every layer has tests of its own
- Serve the built frontend from each worker's own server, so a test gets page and API from one address
- Check in one test what the page shows and what the database holds
- Wait for the database with expect.poll instead of sleeping
- Run the whole suite without touching your development servers or your development database
Three sections are filled. The database tests check the rules of the data, the backend tests ask the real API, and the frontend tests show the page any answer they like. Each of them looks at one layer and takes the others on trust. The frontend tests in particular believe their mocks.
What none of them can tell you is whether the layers fit together. That is the job of the fourth section, and the only job left for it. So e2e stays small: a few tests, each one a whole flow through a real browser, the real API and a real database.
There is an old debt to settle on the way. The two e2e tests from part 2 still open the Vite dev server, which passes their requests on to your development API and so into your development database. They are the last tests that do. After this part, none does.
One address for page and API
Since part 6 every worker starts its own copy of the app on its own database. For the backend tests that was enough, because they only speak HTTP. A browser needs a page to open as well. The app can already hand one out: the last line of createApp from part 1 serves whatever lies in web/dist, and that folder is what npm run build writes. So the plan is short: build the frontend once before the tests, and every worker's server offers page and API under one address.
The frontend section should test the same built files. Its tests start no server, so something else has to serve web/dist to them, and Vite brings that along. Add a script to package.json, below build:
"build": "vite build",
"preview": "vite preview --port 4173 --strictPort",vite preview serves the built files. The port is fixed, and --strictPort makes Vite give up if that port is taken, instead of quietly moving to the next one, where no test would look.
Now the config. This is the whole file:
import { existsSync } from 'node:fs';
import { defineConfig, devices } from '@playwright/test';
// The tests read the same settings as the app. Where there is no .env, such as in a
// pipeline, the variables are set from outside.
if (existsSync('.env')) process.loadEnvFile();
// The built frontend, served by `vite preview`.
const preview = 'http://localhost:4173';
export default defineConfig({
fullyParallel: true,
reporter: [['list'], ['html']],
use: {
trace: 'on-first-retry',
},
projects: [
{
name: 'setup',
testDir: './tests/setup',
testMatch: /.*\.setup\.ts/,
},
{
name: 'database',
testDir: './tests/database',
dependencies: ['setup'],
},
{
name: 'backend',
testDir: './tests/backend',
dependencies: ['setup'],
},
{
name: 'frontend',
testDir: './tests/frontend',
use: { ...devices['Desktop Chrome'], baseURL: preview },
},
{
name: 'e2e',
testDir: './tests/e2e',
dependencies: ['setup'],
use: { ...devices['Desktop Chrome'] },
},
],
// Before the tests, Playwright builds the frontend and serves the result. The frontend
// tests open it there; the servers of the workers hand out the same files from web/dist.
webServer: {
command: 'npm run build && npm run preview',
url: preview,
reuseExistingServer: false,
},
});Four things have changed:
webServeris a single command now. It builds the frontend and then serves it. Playwright waits until the address on port 4173 answers before it starts the first test, and it only answers once the build is through. Soweb/distis complete when the workers start their servers.reuseExistingServer: falsemeans Playwright never takes a preview that is still running from earlier. Every run builds the frontend afresh, so no test sees the page of yesterday.frontendopens its pages at the preview address. Those are the same files the e2e tests get.e2ehas nobaseURLany more. The fixture from part 6 supplies it: the address of the worker's own server. That server works on a database copied from the template, so the project now depends onsetup.
Look at what is gone, too: the two constants for ports 3000 and 5173, and the two dev servers. No address in the config points at your development servers any more. One thing stays as it was: the preview server passes /api on to port 3000 as the dev server does, so the catch-all route of the last part keeps its job.
The page object comes along
The e2e tests use the fixtures of tests/fixtures.ts: database, server, address. One thing is missing there, the page object from the last part. Here is the whole file again, with three new places: the import, the line in TestFixtures and the fixture at the end.
import { once } from 'node:events';
import type { AddressInfo } from 'node:net';
import { serve } from '@hono/node-server';
import { test as base } from '@playwright/test';
import { sql } from 'drizzle-orm';
import { createApp } from '../server/app.ts';
import { connect, type Db } from '../server/db/client.ts';
import { QuestBoard } from './support/board.ts';
import { TEMPLATE, databaseUrl, onServer } from './support/databases.ts';
type TestFixtures = {
/** This worker's database, with empty tables. */
db: Db;
board: QuestBoard;
};
type WorkerFixtures = {
workerDb: Db;
/** The address of this worker's own API. */
server: string;
};
export const test = base.extend<TestFixtures, WorkerFixtures>({
// Once per worker: a database of its own, copied from the template.
workerDb: [
async ({}, use, workerInfo) => {
const name = `questboard_test_${workerInfo.parallelIndex}`;
await onServer(
`drop database if exists ${name} with (force)`,
`create database ${name} template ${TEMPLATE}`,
);
const { db, pool } = connect(databaseUrl(name));
await use(db);
await pool.end();
await onServer(`drop database ${name}`);
},
{ scope: 'worker' },
],
// Once per worker: the app on a free port, working on this worker's database.
server: [
async ({ workerDb }, use) => {
const server = serve({ fetch: createApp(workerDb).fetch, port: 0 });
await once(server, 'listening');
const { port } = server.address() as AddressInfo;
await use(`http://localhost:${port}`);
server.close();
},
{ scope: 'worker' },
],
// Requests and pages of a test go to its worker's server.
baseURL: async ({ server }, use) => {
await use(server);
},
// Before every test: empty the tables, so no test sees what another one left behind.
// `auto` runs this for tests that never mention `db` as well.
db: [
async ({ workerDb }, use) => {
await workerDb.execute(sql`truncate quests restart identity`);
await use(workerDb);
},
{ auto: true },
],
board: async ({ page }, use) => {
await use(new QuestBoard(page));
},
});
export { expect } from '@playwright/test';It is the same QuestBoard class the frontend tests use. The difference is the page it gets. Here baseURL is the worker's server, so board.open() loads the built frontend from that server, and every request of the page goes to the API at the same address, which works on the worker's own database.
Five flows
Replace the two tests in tests/e2e/quest-board.spec.ts with these five:
import { quests } from '../../server/db/schema.ts';
import { expect, test } from '../fixtures.ts';
import { seedQuests } from '../support/seed.ts';
test('a new quest travels from the form into the database', async ({ board, db }) => {
await board.open();
await board.add('Find the lost sword', 50);
await expect(board.quests).toHaveText([/Find the lost sword.*50 gold/]);
expect(await db.select().from(quests)).toMatchObject([
{ title: 'Find the lost sword', reward: 50, done: false },
]);
});
test('the board shows what is in the database', async ({ board, db, page }) => {
await seedQuests(
db,
{ title: 'Feed the dragon', done: true },
{ title: 'Find the lost sword', reward: 50 },
{ title: 'Map the cave' },
);
await board.open();
await expect(board.quests).toHaveText([/Find the lost sword/, /Map the cave/, /Feed the dragon/]);
await expect(page.getByText('2 of 3 quests open')).toBeVisible();
});
test('a finished quest is still finished after a reload', async ({ board, db, page }) => {
await seedQuests(db, { title: 'Find the lost sword' });
await board.open();
await board.checkbox('Find the lost sword').check();
// The click is done before the request is: ask the database until it agrees.
await expect.poll(async () => (await db.select().from(quests))[0].done).toBe(true);
await page.reload();
await expect(board.checkbox('Find the lost sword')).toBeChecked();
});
test('a deleted quest is gone for good', async ({ board, db, page }) => {
await seedQuests(db, { title: 'Find the lost sword' }, { title: 'Map the cave' });
await board.open();
await board.deleteButton('Map the cave').click();
await expect(board.quests).toHaveText([/Find the lost sword/]);
await page.reload();
await expect(board.quests).toHaveText([/Find the lost sword/]);
expect(await db.select({ title: quests.title }).from(quests)).toEqual([{ title: 'Find the lost sword' }]);
});
test('a title that is taken is turned down, all the way from the database', async ({ board, db }) => {
await seedQuests(db, { title: 'Find the lost sword' });
await board.open();
await board.add('Find the lost sword');
await expect(board.problem).toHaveText('A quest with this title already exists.');
expect(await db.select().from(quests)).toHaveLength(1);
});Every test asks for board and db: the browser at one end, a direct look into the database at the other. Between the two lies everything the app is made of.
From the form into the database. The first test types a quest into the form and expects it in the list. The page adds the row only once the API has answered, so when toHaveText is satisfied, the quest is stored, and the next line can read it from the database without waiting. toMatchObject compares the fields the test names and ignores the others, such as id and createdAt.
From the database into the page. The second test goes the other way: seedQuests from part 5 puts three rows into the database, then the page is opened. The dragon was stored first but is done, so it has to stand last. The ordering rule of the query has arrived in the browser.
Ticking a quest. Here the test has a question of timing. check() is finished as soon as the checkbox is ticked in the browser, but the request that stores the change is still on its way. Reading the database in the next line would be a race. expect.poll settles it: it calls the function you give it again and again, until the result is what you expect or the time is up. That is waiting for a fact, not for a number of milliseconds. Then the page is reloaded. A reloaded page has forgotten everything and asks the API again, so a checkbox that is still ticked can only come from the database.
Deleting. The fourth test deletes one of two quests and looks three times: in the page, in the page after a reload, and in the database, where exactly the other quest has to be left.
A title that is taken. The last test needs all three layers to do their part. The database turns the second row down because of its unique constraint, the API makes a 409 with a sentence out of that, and the page shows the sentence. Each step has a test in its own section, and this one proves that they are connected. It is also the scene from the end of part 2, this time the right way round. There the message appeared by accident and the test passed for the wrong reason. Here the message is what the test expects, on a database that holds exactly what the test put into it.
Notice what is not here: no invalid rewards, no loading text, no 404. Those details are tested where they are cheap. An end-to-end test needs a browser, a server and a database, and when it fails, it cannot say on its own which of the three is to blame. So it is kept for what only it can show.
Run it
First the new section:
$ npm run test:e2e> questboard@1.0.0 test:e2e> playwright test --project=e2eRunning 6 tests using 5 workers ✓ 1 [setup] › tests/setup/database.setup.ts:6:1 › build the template database (110ms) ✓ 6 [e2e] › tests/e2e/quest-board.spec.ts:16:1 › the board shows what is in the database (158ms) ✓ 2 [e2e] › tests/e2e/quest-board.spec.ts:5:1 › a new quest travels from the form into the database (229ms) ✓ 3 [e2e] › tests/e2e/quest-board.spec.ts:30:1 › a finished quest is still finished after a reload (261ms) ✓ 4 [e2e] › tests/e2e/quest-board.spec.ts:43:1 › a deleted quest is gone for good (227ms) ✓ 5 [e2e] › tests/e2e/quest-board.spec.ts:55:1 › a title that is taken is turned down, all the way from the database (227ms) 6 passed (3.2s)
Six tests: the setup test that builds the template, then the five flows, spread over the workers, each worker with its own database and its own server. Before the first line appears, Playwright has built the frontend.
And now everything at once. With more than fifty tests the list gets long, so this run uses the dot reporter, which prints one dot per test:
$ npx playwright test --reporter=dotRunning 52 tests using 8 workers····························[✓] Pulling schema from database...························ 52 passed (6.7s)
52 tests in under seven seconds: the four sections and the setup test. The line in the middle is Drizzle Kit at work in the migration test of part 5, not an error.
What you have now
All four sections are filled: with the setup test, 52 tests that check the database, the API, the page and the way through all three, in parallel. The browser tests run against the built frontend, and no test touches your development servers or your development database any more.
A green suite is the pleasant case. In the next part you break the app on purpose and learn to read what Playwright tells you when a test is red: the error, the report and the trace.
Comments
The comments are GitHub Discussions: sign in with GitHub to write one.
The comments come from GitHub, through giscus. Loading them sends your IP address to giscus.app and GitHub.