forked from MarcusFelling/demo.playwright
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress-test.ts
54 lines (46 loc) · 1.5 KB
/
express-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* In this test we define our worker fixtures.
* @see https://playwright.dev/docs/test-fixtures#worker-fixtures
*/
import {test as base} from '@playwright/test';
import express from 'express';
import type {Express} from 'express';
// Declare worker fixtures.
type ExpressWorkerFixtures = {
port: number;
express: Express;
};
// Note that we did not provide any test-scoped fixtures, so we pass {}.
const test = base.extend<{}, ExpressWorkerFixtures>({
// We pass a tuple to specify fixtures options.
// In this case, we mark this fixture as worker-scoped.
port: [async ({}, use, workerInfo) => {
// "port" fixture uses a unique value of the worker process index.
await use(3000 + workerInfo.workerIndex);
}, {scope: 'worker'}],
// "express" fixture starts automatically for every worker - we pass "auto" for that.
express: [async ({port}, use) => {
// Setup express app.
const app = express();
app.get('/1', (req, res) => {
res.send('Hello World 1!');
});
app.get('/2', (req, res) => {
res.send('Hello World 2!');
});
// Start the server.
let server;
console.log('Starting server...');
await new Promise((f) => {
server = app.listen(port);
});
console.log('Server ready');
// Use the server in the tests.
await use(server);
// Cleanup.
console.log('Stopping server...');
await new Promise((f) => server.close(f));
console.log('Server stopped');
}, {scope: 'worker', auto: true}],
});
export default test;