Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ compress files at the time and the test was run over VPN. Turning on compression
| Unit (backend) | Vitest | Parsing of X-Wines CSV files, building SQL for search, filtering and sorting, review validation, and passwords, sessions and rate limiting | 147 in 17 files (42 tests, 4 files skipped: they need a test database) |
| End-to-end | Playwright with axe-core, desktop and mobile | Landing, search and detail pages with a stubbed API: one request per page load, debounce, search, filters, sorting, load more, reload, back button, from search to wine and back, the filter dialog on mobile, no hits, wine not found, API errors, keyboard use, reflow at 320 px, the wine lists (also after a reload, with login on the way, and the move of an old list), «Min side» from login to logout with the top 4 chosen with the keyboard, and no axe violations (WCAG 2.1 AA) | 86 (2 skipped) |
| API | Vitest with Apollo Server (`executeOperation`) | Login, reviews, wine lists, the top 4 and the demo user through real GraphQL requests, with a fake browser per user that keeps the cookie. The review tests run against a separate test database (`TEST_DATABASE_URL`), see [API](docs/api.md#database-tests) | Count again |
| End-to-end with the backend | Playwright, desktop | The same pages against the real backend and a PostgreSQL test database (`TEST_DATABASE_URL`), with nothing stubbed: search, filter, sorting and «Last inn flere» on the 100 test wines, a new user with login, logout and a wrong password, a review from search to «Min side», a favourite that follows the user to another browser, and two users who review the same wine. See [API](docs/api.md#end-to-end-tests-with-the-backend) | 11 in 5 files |

Counted on 16 November 2026. The skipped backend tests run the user store against a real database, and need `TEST_DATABASE_URL`.

Expand All @@ -364,6 +365,7 @@ Run `npm run test:coverage` in `frontend/` to see the full report. It prints a l
cd frontend
npm run test:run
npm run e2e # first time: npx playwright install chromium
npm run e2e:api # against the real backend; needs TEST_DATABASE_URL, see docs/api.md

cd ../backend
npm test
Expand Down
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"test": "vitest run",
"db:seed:test": "tsx src/scripts/seed-xwines.ts data/xwines-test/wines.csv data/xwines-test/ratings.csv",
"db:seed:demo": "tsx src/scripts/seed-demo-user.ts",
"db:e2e:clean": "tsx src/scripts/delete-e2e-users.ts",
"db:migrate": "node-pg-migrate up",
"db:migrate:down": "node-pg-migrate down",
"db:migrate:create": "node-pg-migrate create --migration-file-language sql"
Expand Down
18 changes: 18 additions & 0 deletions backend/src/scripts/delete-e2e-users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Removes the users the end-to-end tests made in an earlier run, with their reviews and lists.
// Usage: npm run db:e2e:clean. Playwright runs it against the test database before the tests
// (see frontend/playwright.api.config.ts). It only touches usernames that start with "e2e_".
import { pool } from '../db';

async function main() {
const { rowCount } = await pool.query(
String.raw`DELETE FROM app_user WHERE username LIKE 'e2e\_%'`,
);
console.log(`Deleted ${rowCount ?? 0} users from earlier end-to-end runs.`);
}

main()
.catch((error: unknown) => {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
})
.finally(() => pool.end());
20 changes: 20 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,24 @@ TEST_DATABASE_URL=postgresql://vinmonopolet_app:PASSWORD@localhost:5432/vinmonop

On Windows PowerShell, set the variable first: `$env:TEST_DATABASE_URL="postgresql://..."`, then `npm test`.

## End-to-end tests with the backend

The Playwright tests in `frontend/e2e/` stub the API. The tests in `frontend/e2e-api/` do not: a
click in the browser goes through the frontend, GraphQL and the backend to PostgreSQL and back. They
use the same test database as the database tests above:

```bash
cd frontend
TEST_DATABASE_URL=postgresql://vinmonopolet_app:PASSWORD@localhost:5432/vinmonopolet_test npm run e2e:api
```

Without `TEST_DATABASE_URL` the tests are skipped. `npm run check -- --e2e` from the repository root
runs them too.

Playwright starts its own backend on port 3101 and its own frontend on port 4174
(`frontend/playwright.api.config.ts`), so your dev servers can keep running, and the tests never use
the database in `backend/.env`. Before the tests, the backend runs the migrations, loads the 100
wines in `backend/data/xwines-test`, and deletes the users from earlier runs. Every test makes its
own user with a name that starts with `e2e_`, so the tests can run at the same time.

See [manual testing](manual-testing.md) for how to test users and user data by hand.
2 changes: 1 addition & 1 deletion docs/workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Run from the repository root:
| ------------------------ | ------------------------------------------------------------------------------------------- |
| `npm run check` | Prettier, ESLint, TypeScript, tests with coverage and build, for `frontend/` and `backend/` |
| `npm run check -- --fix` | Runs Prettier and ESLint fixes first |
| `npm run check -- --e2e` | Also runs the Playwright tests |
| `npm run check -- --e2e` | Also runs the Playwright tests, and those against the real backend if `TEST_DATABASE_URL` is set |

- Every step runs, even after a failure. Steps a package lacks show as «not set up».
- The output ends with a summary, the coverage numbers and a link to the report.
Expand Down
62 changes: 62 additions & 0 deletions frontend/e2e-api/auth.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { expect, test, type Page } from '@playwright/test';
import {
expectLoggedIn,
logIn,
logOut,
needsTestDatabase,
PASSWORD,
register,
} from './support/app';

/* Users and sessions against the real backend: the password hash and the session are in PostgreSQL. */
needsTestDatabase();

const menu = (page: Page) => page.getByRole('navigation', { name: 'Hovedmeny' });

test('en ny bruker er fortsatt logget inn etter omlasting, og kan logge ut og inn igjen', async ({
page,
}) => {
const username = await register(page);

// The session cookie is all the browser has. The server finds the user from it.
await page.reload();
await expectLoggedIn(page, username);

await logOut(page);
await page.reload();
await expect(menu(page).getByRole('link', { name: 'Logg inn' })).toBeVisible();

await logIn(page, username);
await expectLoggedIn(page, username);
});

test('feil passord gir en feilmelding og logger ikke inn', async ({ page }) => {
const username = await register(page);
await logOut(page);

await logIn(page, username, `${PASSWORD}-feil`);
await expect(page.getByRole('main').getByRole('alert')).toContainText(
/brukernavn eller passord/i,
);
await expect(menu(page).getByRole('link', { name: 'Logg inn' })).toBeVisible();
});

test('et brukernavn kan bare brukes én gang', async ({ page, browser }) => {
const username = await register(page);

// Someone else, in their own browser, tries the same name.
const other = await browser.newContext();
const otherPage = await other.newPage();
await otherPage.goto('./konto');
await otherPage.getByRole('main').getByRole('button', { name: 'Lag bruker' }).click();
await otherPage.getByRole('textbox', { name: 'Brukernavn' }).fill(username);
await otherPage.getByLabel('Passord').fill(PASSWORD);
await otherPage.getByRole('main').getByRole('button', { name: 'Lag bruker' }).click();

await expect(otherPage.getByRole('textbox', { name: 'Brukernavn' })).toHaveAttribute(
'aria-invalid',
'true',
);
await expect(menu(otherPage).getByRole('link', { name: 'Logg inn' })).toBeVisible();
await other.close();
});
43 changes: 43 additions & 0 deletions frontend/e2e-api/journey.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { expect, test } from '@playwright/test';
import { needsTestDatabase, register } from './support/app';

/*
* The main way through the site, against the real backend and PostgreSQL: search, open a wine,
* save a review and find it on «Min side».
*/
needsTestDatabase();

test('søk, åpne en vin, gi en dom og finn den igjen på «Min side»', async ({ page }) => {
await register(page);

await page.goto('./sok');
await page.getByRole('searchbox', { name: 'Søk etter vin' }).fill('merlot');
await expect(page.getByRole('main').getByRole('status').first()).toContainText('for «merlot»');
await page.getByRole('link', { name: 'Origem Merlot' }).click();
await expect(page.getByRole('heading', { level: 1, name: 'Origem Merlot' })).toBeVisible();

const reviews = page.getByRole('region', { name: 'Vurderinger' });
await expect(reviews.getByText('Ingen dommer ennå')).toBeVisible();
// The radio buttons are hidden behind the stars, so the click goes to the label.
await reviews.getByText('4 av 5 stjerner', { exact: true }).click({ force: true });
await reviews.getByLabel('Kommentar (valgfritt)').fill('Myk og god til pizza.');
await reviews.getByRole('button', { name: 'Lagre dommen' }).click();
await expect(reviews.getByRole('status')).toHaveText('Dommen er lagret.');
await expect(reviews.getByText('4,0 av 5 · 1 dom')).toBeVisible();

await page
.getByRole('navigation', { name: 'Hovedmeny' })
.getByRole('link', { name: 'Min side' })
.click();
const myReviews = page.getByRole('region', { name: 'Mine dommer' });
await expect(myReviews.getByRole('link', { name: 'Origem Merlot' })).toBeVisible();
await expect(myReviews.getByText('Myk og god til pizza.')).toBeVisible();
// A reviewed wine is also put on «Har prøvd».
await expect(
page.getByRole('region', { name: 'Har prøvd' }).getByRole('link', { name: 'Origem Merlot' }),
).toBeVisible();

// Still there after a reload, so it came from the database and not from the page's memory.
await page.reload();
await expect(myReviews.getByText('Myk og god til pizza.')).toBeVisible();
});
55 changes: 55 additions & 0 deletions frontend/e2e-api/lists.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { expect, test } from '@playwright/test';
import { expectLoggedIn, logIn, needsTestDatabase, register } from './support/app';

/* The lists against the real backend: a saved wine is stored on the server, not in the browser. */
needsTestDatabase();

test('en favoritt lagres på serveren og følger brukeren til en annen nettleser', async ({
page,
browser,
}) => {
const username = await register(page);

await page.goto('./vin/102079');
await expect(page.getByRole('heading', { level: 1, name: 'Maré Alta' })).toBeVisible();
const favourite = page.getByRole('button', { name: 'Favoritt', exact: true });
await favourite.click();
await expect(favourite).toHaveAttribute('aria-pressed', 'true');

await page.reload();
await expect(favourite).toHaveAttribute('aria-pressed', 'true');

// A browser that has never seen this user: all it knows comes from the server.
const other = await browser.newContext();
const otherPage = await other.newPage();
await logIn(otherPage, username);
await expectLoggedIn(otherPage, username);
await otherPage.goto('./mine/favoritter');
await expect(otherPage.getByRole('link', { name: 'Maré Alta' })).toBeVisible();
await other.close();

// Taken off again, and gone after a reload.
await favourite.click();
await expect(favourite).toHaveAttribute('aria-pressed', 'false');
await page.goto('./mine/favoritter');
await expect(page.getByText(/Du har ingen favoritter ennå/)).toBeVisible();
});

test('«Har prøvd» tar vinen av «Vil prøve»', async ({ page }) => {
await register(page);
await page.goto('./vin/102902');
const wantToTry = page.getByRole('button', { name: 'Vil prøve', exact: true });
const tried = page.getByRole('button', { name: 'Har prøvd', exact: true });

await wantToTry.click();
await expect(wantToTry).toHaveAttribute('aria-pressed', 'true');
await tried.click();
await expect(tried).toHaveAttribute('aria-pressed', 'true');
await expect(wantToTry).toHaveAttribute('aria-pressed', 'false');

// The server did the move, so the two list pages agree after a fresh load.
await page.goto('./mine/har-provd');
await expect(page.getByRole('link', { name: 'Pátria Alentejo Selection Reserva' })).toBeVisible();
await page.goto('./mine/vil-prove');
await expect(page.getByText(/Du har ikke lagret noen viner ennå/)).toBeVisible();
});
52 changes: 52 additions & 0 deletions frontend/e2e-api/reviews.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { expect, test, type Page } from '@playwright/test';
import { needsTestDatabase, register } from './support/app';

/* Reviews against the real backend: two users, each in their own browser, review the same wine. */
needsTestDatabase();

const WINE = './vin/100191'; // Reserva Chardonnay. No other test reviews it.
const reviews = (page: Page) => page.getByRole('region', { name: 'Vurderinger' });

async function give(page: Page, stars: string, button: 'Lagre dommen' | 'Oppdater dommen') {
// The radio buttons are hidden behind the stars, so the click goes to the label.
await reviews(page).getByText(`${stars} av 5 stjerner`, { exact: true }).click({ force: true });
await reviews(page).getByRole('button', { name: button }).click();
await expect(reviews(page).getByRole('status')).toHaveText('Dommen er lagret.');
}

test('en annen bruker ser dommen, og snittet følger med når dommer endres og slettes', async ({
page,
browser,
}) => {
const kari = await register(page);
await page.goto(WINE);
await reviews(page).getByLabel('Kommentar (valgfritt)').fill('Frisk og fin.');
await give(page, '5', 'Lagre dommen');
await expect(reviews(page).getByText('5,0 av 5 · 1 dom')).toBeVisible();

const other = await browser.newContext();
const otherPage = await other.newPage();
await register(otherPage);
await otherPage.goto(WINE);

// The other user sees the first review, with who wrote it.
const list = otherPage.getByRole('list', { name: 'Studentenes dommer' });
await expect(list.getByRole('listitem')).toHaveCount(1);
await expect(list).toContainText(kari);
await expect(list).toContainText('Frisk og fin.');

await give(otherPage, '3', 'Lagre dommen');
await expect(reviews(otherPage).getByText('4,0 av 5 · 2 dommer')).toBeVisible();

await give(otherPage, '4', 'Oppdater dommen');
await expect(reviews(otherPage).getByText('4,5 av 5 · 2 dommer')).toBeVisible();

await reviews(otherPage).getByRole('button', { name: 'Slett dommen' }).click();
await expect(reviews(otherPage).getByRole('status')).toHaveText('Dommen er slettet.');
await expect(reviews(otherPage).getByText('5,0 av 5 · 1 dom')).toBeVisible();
await other.close();

// The first user sees the same after a reload.
await page.reload();
await expect(reviews(page).getByText('5,0 av 5 · 1 dom')).toBeVisible();
});
63 changes: 63 additions & 0 deletions frontend/e2e-api/search.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { expect, test, type Page } from '@playwright/test';
import { needsTestDatabase } from './support/app';

/*
* Search, filter, sorting and paging against the real backend. The test database has the 100
* wines in backend/data/xwines-test, so the numbers below are known.
*/
needsTestDatabase();

const status = (page: Page) => page.getByRole('main').getByRole('status').first();
const results = (page: Page) => page.getByRole('list', { name: 'Søkeresultater' });

test('finner en vin fra ord i navnet', async ({ page }) => {
await page.goto('./sok');
await page.getByRole('searchbox', { name: 'Søk etter vin' }).fill('Reserva TAWNY');
await expect(status(page)).toHaveText('Viser 1 av 1 vin for «Reserva TAWNY»');
await expect(
results(page).getByRole('link', { name: 'Dona Antonia Porto Reserva Tawny' }),
).toBeVisible();
});

test('sier fra når ingen viner passer', async ({ page }) => {
await page.goto('./sok?q=finnesikke');
await expect(page.getByText('Ingen viner passer til «finnesikke».')).toBeVisible();
});

test('filter og sortering gjelder hele katalogen, ikke bare de vinene som er lastet', async ({
page,
}) => {
await page.goto('./sok');
await expect(status(page)).toHaveText('Viser 24 av 100 viner');

// 53 of the 100 wines are red. Only 24 are on the page, so the count comes from the database.
await page.getByRole('checkbox', { name: /Rødvin/ }).check();
await expect(status(page)).toHaveText('Viser 24 av 53 viner');
await expect(results(page).getByRole('article')).toHaveCount(24);
await expect(results(page).getByText(/^Rødvin · /)).toHaveCount(24);

await page.getByRole('link', { name: 'Fjern filteret Rødvin' }).click();
await expect(status(page)).toHaveText('Viser 24 av 100 viner');
await page.getByRole('combobox', { name: 'Sorter etter' }).selectOption({ label: 'Navn A–Å' });
await expect(results(page).getByRole('article').first()).toContainText(
'Al Poggio Chardonnay di Toscana',
);
});

test('«Last inn flere» henter resten av vinene uten å vise noen to ganger', async ({ page }) => {
await page.goto('./sok');
await expect(status(page)).toHaveText('Viser 24 av 100 viner');

for (const shown of [48, 72, 96, 100]) {
await page.getByRole('button', { name: 'Last inn flere' }).click();
await expect(status(page)).toHaveText(`Viser ${shown} av 100 viner`);
}
await expect(page.getByRole('button', { name: 'Last inn flere' })).toBeHidden();

const links = await results(page)
.getByRole('article')
.getByRole('link')
.evaluateAll((all) => all.map((link) => link.getAttribute('href')));
expect(links).toHaveLength(100);
expect(new Set(links).size).toBe(100);
});
Loading