Skip to content

Commit 21a9012

Browse files
committed
Refactor tests to run Playwright's server in Docker
1 parent e3f660b commit 21a9012

9 files changed

Lines changed: 50 additions & 77 deletions

docker-compose.yml

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,11 @@
11
services:
2-
serve-webpack-example:
3-
build:
4-
context: .
5-
dockerfile: ./example/webpack/Dockerfile
6-
stdin_open: true
7-
tty: true
8-
serve-express-example:
9-
build:
10-
context: .
11-
dockerfile: ./example/express-server/Dockerfile
12-
stdin_open: true
13-
tty: true
14-
test:
15-
depends_on:
16-
- serve-webpack-example
17-
- serve-express-example
18-
build:
19-
context: .
20-
dockerfile: ./tests/Dockerfile
21-
volumes:
22-
- ./demo-dist:/app/demo-dist:ro
23-
- ./playwright.config.ts:/app/playwright.config.ts:ro
24-
- ./tests:/app/tests
25-
- ./test-results:/app/test-results
26-
- ./playwright-report:/app/playwright-report
27-
command: npx playwright test
28-
update-snapshots:
29-
depends_on:
30-
- serve-webpack-example
31-
- serve-express-example
2+
playwright-server:
323
build:
334
context: .
345
dockerfile: ./tests/Dockerfile
35-
volumes:
36-
- ./demo-dist:/app/demo-dist:ro
37-
- ./playwright.config.ts:/app/playwright.config.ts:ro
38-
- ./tests:/app/tests
39-
- ./test-results:/app/test-results
40-
- ./playwright-report:/app/playwright-report
41-
command: npx playwright test --update-snapshots
6+
args:
7+
PLAYWRIGHT_VERSION: ${PLAYWRIGHT_VERSION}
8+
ipc: host
9+
command: playwright run-server --host=0.0.0.0 --port=3000
10+
ports:
11+
- '3000:3000'

package-lock.json

Lines changed: 1 addition & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"homepage": "https://apis.guru/graphql-voyager",
88
"repository": {
99
"type": "git",
10-
"url": "https://github.com/APIs-guru/graphql-voyager.git"
10+
"url": "git+https://github.com/APIs-guru/graphql-voyager.git"
1111
},
1212
"funding": "https://github.com/APIs-guru/graphql-voyager?sponsor=1",
1313
"bugs": {
@@ -49,8 +49,8 @@
4949
"prettier": "prettier --write --list-different . **/*.svg",
5050
"prettier:check": "prettier --check . **/*.svg",
5151
"check:spell": "cspell --cache --no-progress '**/*'",
52-
"testonly": "npm run build:demo && npm pack && docker compose up --abort-on-container-exit --build test",
53-
"update-snapshots": "npm run build:demo && npm pack && docker compose up --build update-snapshots",
52+
"testonly": "npm run build:demo && npm pack && playwright test",
53+
"update-snapshots": "npm run build:demo && npm pack && playwright test --update-snapshots",
5454
"deploy": "wrangler deploy"
5555
},
5656
"peerDependencies": {
@@ -68,7 +68,6 @@
6868
"svg-pan-zoom": "3.6.2"
6969
},
7070
"devDependencies": {
71-
"@playwright/test": "1.53.0",
7271
"@svgr/webpack": "8.1.0",
7372
"@types/commonmark": "0.27.9",
7473
"@types/node": "24.0.1",
@@ -86,6 +85,7 @@
8685
"eslint-plugin-simple-import-sort": "12.1.1",
8786
"graphql": "16.11.0",
8887
"mini-css-extract-plugin": "2.9.2",
88+
"playwright": "1.53.0",
8989
"postcss-cssnext": "3.1.1",
9090
"postcss-import": "16.1.0",
9191
"postcss-loader": "8.1.1",

playwright.config.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import type { PlaywrightTestConfig } from '@playwright/test';
2-
import { devices } from '@playwright/test';
1+
import playwrightPackage from 'playwright/package.json' with { type: 'json' };
2+
import { devices, type PlaywrightTestConfig } from 'playwright/test';
33

44
const isCI = !!process.env['CI'];
55
/**
66
* See https://playwright.dev/docs/test-configuration.
77
*/
88
const config: PlaywrightTestConfig = {
99
testDir: './tests',
10+
// FIXME: remove '-linux'
11+
snapshotPathTemplate:
12+
'{snapshotDir}/{testFileDir}/{testFileName}-snapshots/{arg}{-projectName}-linux{ext}',
1013
/* Maximum time one test can run for. */
1114
timeout: 20 * 1000,
1215
expect: {
@@ -38,12 +41,17 @@ const config: PlaywrightTestConfig = {
3841

3942
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
4043
trace: 'retain-on-failure',
44+
45+
connectOptions: {
46+
wsEndpoint: 'ws://127.0.0.1:3000/',
47+
exposeNetwork: '<loopback>',
48+
},
4149
},
4250
projects: [
4351
{
4452
name: 'Demo',
4553
testMatch: 'Demo.spec.ts',
46-
use: { baseURL: 'http://localhost:9090' },
54+
use: { baseURL: 'http://127.0.0.1:9090' },
4755
},
4856
{
4957
name: 'WebpackExample',
@@ -57,10 +65,26 @@ const config: PlaywrightTestConfig = {
5765
},
5866
],
5967
outputDir: 'test-results/',
60-
webServer: {
61-
command: 'npm run serve',
62-
url: 'http://localhost:9090/',
63-
},
68+
webServer: [
69+
{
70+
name: 'playwright-server',
71+
env: { PLAYWRIGHT_VERSION: playwrightPackage.version },
72+
command:
73+
'docker compose up --abort-on-container-exit --build playwright-server',
74+
url: 'http://127.0.0.1:3000/',
75+
stdout: 'pipe',
76+
timeout: 120 * 1000,
77+
gracefulShutdown: {
78+
signal: 'SIGINT',
79+
timeout: 120 * 1000,
80+
},
81+
},
82+
{
83+
name: 'npm run serve',
84+
command: 'npm run serve',
85+
url: 'http://localhost:9090/',
86+
},
87+
],
6488
};
6589

6690
export default config;

tests/Dockerfile

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
# syntax=docker/dockerfile:1
2-
32
FROM node:24
43

5-
WORKDIR /app
6-
7-
COPY ["./package.json", "./package-lock.json", "./"]
8-
RUN npm ci && npx playwright install --with-deps chromium
9-
10-
COPY ["./tsconfig.json", "./"]
11-
COPY ["./scripts", "./scripts"]
4+
ARG PLAYWRIGHT_VERSION="missing PLAYWRIGHT_VERSION arg"
5+
RUN npm -g install "playwright@$PLAYWRIGHT_VERSION"
6+
RUN playwright install --with-deps chromium

tests/PageObjectModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Locator, type Page } from '@playwright/test';
1+
import { type Locator, type Page } from 'playwright/test';
22
import { format } from 'prettier';
33

44
interface VoyagerURLParams {

tests/demo.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { expect, test } from '@playwright/test';
21
import { buildSchema, graphqlSync } from 'graphql';
2+
import { expect, test } from 'playwright/test';
33

44
import { gotoVoyagerPage, SchemaPresets } from './PageObjectModel.ts';
55

tests/express.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from '@playwright/test';
1+
import { expect, test } from 'playwright/test';
22

33
import { gotoVoyagerPage } from './PageObjectModel.ts';
44

tests/webpack.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from '@playwright/test';
1+
import { expect, test } from 'playwright/test';
22

33
import { gotoVoyagerPage } from './PageObjectModel.ts';
44

0 commit comments

Comments
 (0)