Skip to content

Commit e97ab7e

Browse files
authored
test: unflake some web socket tests (#4673)
Tests were waiting for `framesent` event after awaiting `page.evaluate`. Sometimes, `page.evaluate` took long enough and finished after the `framesent`. Drive-by: small fixes for mode=service test fixture.
1 parent 12dc04a commit e97ab7e

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

test/fixtures.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ fixtures.playwright.override(async ({ browserName, testWorkerIndex, platform, mo
131131
const spawnedProcess = childProcess.fork(path.join(__dirname, '..', 'lib', 'service.js'), [String(port)], {
132132
stdio: 'pipe'
133133
});
134+
spawnedProcess.stderr.pipe(process.stderr);
134135
await new Promise(f => {
135136
spawnedProcess.stdout.on('data', data => {
136137
if (data.toString().includes('Listening on'))
@@ -144,9 +145,11 @@ fixtures.playwright.override(async ({ browserName, testWorkerIndex, platform, mo
144145
spawnedProcess.on('exit', onExit);
145146
const client = await PlaywrightClient.connect(`ws://localhost:${port}/ws`);
146147
await run(client.playwright());
147-
spawnedProcess.removeListener('exit', onExit);
148148
await client.close();
149+
spawnedProcess.removeListener('exit', onExit);
150+
const processExited = new Promise(f => spawnedProcess.on('exit', f));
149151
spawnedProcess.kill();
152+
await processExited;
150153
await teardownCoverage();
151154
} else {
152155
const playwright = require('../index');

test/web-socket.spec.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,41 +108,47 @@ it('should emit error', async ({page, server, isFirefox}) => {
108108
expect(message).toContain(': 400');
109109
});
110110

111-
it('should not have stray error events', async ({page, server, isFirefox}) => {
112-
const [ws] = await Promise.all([
113-
page.waitForEvent('websocket'),
111+
it('should not have stray error events', async ({page, server}) => {
112+
let error;
113+
page.on('websocket', ws => ws.on('socketerror', e => error = e));
114+
await Promise.all([
115+
page.waitForEvent('websocket').then(async ws => {
116+
await ws.waitForEvent('framereceived');
117+
return ws;
118+
}),
114119
page.evaluate(port => {
115120
(window as any).ws = new WebSocket('ws://localhost:' + port + '/ws');
116121
}, server.PORT)
117122
]);
118-
let error;
119-
ws.on('socketerror', e => error = e);
120-
await ws.waitForEvent('framereceived');
121123
await page.evaluate('window.ws.close()');
122124
expect(error).toBeFalsy();
123125
});
124126

125-
it('should reject waitForEvent on socket close', async ({page, server, isFirefox}) => {
127+
it('should reject waitForEvent on socket close', async ({page, server}) => {
126128
const [ws] = await Promise.all([
127-
page.waitForEvent('websocket'),
129+
page.waitForEvent('websocket').then(async ws => {
130+
await ws.waitForEvent('framereceived');
131+
return ws;
132+
}),
128133
page.evaluate(port => {
129134
(window as any).ws = new WebSocket('ws://localhost:' + port + '/ws');
130135
}, server.PORT)
131136
]);
132-
await ws.waitForEvent('framereceived');
133137
const error = ws.waitForEvent('framesent').catch(e => e);
134138
await page.evaluate('window.ws.close()');
135139
expect((await error).message).toContain('Socket closed');
136140
});
137141

138-
it('should reject waitForEvent on page close', async ({page, server, isFirefox}) => {
142+
it('should reject waitForEvent on page close', async ({page, server}) => {
139143
const [ws] = await Promise.all([
140-
page.waitForEvent('websocket'),
144+
page.waitForEvent('websocket').then(async ws => {
145+
await ws.waitForEvent('framereceived');
146+
return ws;
147+
}),
141148
page.evaluate(port => {
142149
(window as any).ws = new WebSocket('ws://localhost:' + port + '/ws');
143150
}, server.PORT)
144151
]);
145-
await ws.waitForEvent('framereceived');
146152
const error = ws.waitForEvent('framesent').catch(e => e);
147153
await page.close();
148154
expect((await error).message).toContain('Page closed');

0 commit comments

Comments
 (0)