Skip to content

Commit 51e920e

Browse files
authored
chore: fix flaky tests (#2673)
1 parent cd52873 commit 51e920e

3 files changed

Lines changed: 57 additions & 18 deletions

File tree

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
<iframe id="frame-one" src="./one.html"></iframe>
2-
<iframe id="frame-two" src="./two.html"></iframe>
1+

test/browser/msw-api/setup-worker/scenarios/iframe-isolated-response/iframe-isolated-response.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,33 @@ test('responds with different responses for the same request based on request re
3333
},
3434
)
3535

36-
const frameOne = getFrameById('frame-one', page)!
37-
const frameTwo = getFrameById('frame-two', page)!
36+
// Add iframes dynamically after `window.msw` is set on the parent
37+
// to prevent the iframe scripts from racing with the mocks setup.
38+
await page.evaluate(() => {
39+
for (const [id, src] of [
40+
['frame-one', './one.html'],
41+
['frame-two', './two.html'],
42+
]) {
43+
const iframe = document.createElement('iframe')
44+
iframe.id = id
45+
iframe.name = id
46+
iframe.src = src
47+
document.body.appendChild(iframe)
48+
}
49+
})
50+
51+
// Wait for child frames to be attached and navigated.
52+
await page.waitForFunction(() => {
53+
return document.querySelectorAll('iframe').length === 2
54+
})
55+
const frameOne = getFrameById('frame-one', page)
56+
const frameTwo = getFrameById('frame-two', page)
57+
58+
// Wait for the iframe scripts to load and define `window.request`.
59+
await Promise.all([
60+
frameOne.waitForFunction(() => typeof window.request === 'function'),
61+
frameTwo.waitForFunction(() => typeof window.request === 'function'),
62+
])
3863

3964
await Promise.all([
4065
frameOne.evaluate(() => window.request()),

test/browser/ws-api/ws.clients.browser.test.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,36 @@ test('broadcasts messages across runtimes', async ({
160160
await pageOne.evaluate(() => {
161161
window.ws.send('hi from one')
162162
})
163-
expect(await pageOne.evaluate(() => window.messages)).toEqual(['hi from one'])
164-
expect(await pageTwo.evaluate(() => window.messages)).toEqual(['hi from one'])
163+
164+
{
165+
await pageOne.waitForFunction(() => window.messages.length === 1)
166+
await expect(pageOne.evaluate(() => window.messages)).resolves.toEqual([
167+
'hi from one',
168+
])
169+
170+
await pageTwo.waitForFunction(() => window.messages.length === 1)
171+
await expect(pageTwo.evaluate(() => window.messages)).resolves.toEqual([
172+
'hi from one',
173+
])
174+
}
165175

166176
await pageTwo.evaluate(() => {
167177
window.ws.send('hi from two')
168178
})
169179

170-
expect(await pageTwo.evaluate(() => window.messages)).toEqual([
171-
'hi from one',
172-
'hi from two',
173-
])
174-
expect(await pageOne.evaluate(() => window.messages)).toEqual([
175-
'hi from one',
176-
'hi from two',
177-
])
180+
{
181+
await pageTwo.waitForFunction(() => window.messages.length === 2)
182+
await expect(pageTwo.evaluate(() => window.messages)).resolves.toEqual([
183+
'hi from one',
184+
'hi from two',
185+
])
186+
187+
await pageOne.waitForFunction(() => window.messages.length === 2)
188+
await expect(pageOne.evaluate(() => window.messages)).resolves.toEqual([
189+
'hi from one',
190+
'hi from two',
191+
])
192+
}
178193
})
179194

180195
test('clears the list of clients when the worker is stopped', async ({
@@ -194,7 +209,7 @@ test('clears the list of clients when the worker is stopped', async ({
194209
await worker.start()
195210
})
196211

197-
expect(await page.evaluate(() => window.link.clients.size)).toBe(0)
212+
await expect(page.evaluate(() => window.link.clients.size)).resolves.toBe(0)
198213

199214
await page.evaluate(async () => {
200215
const ws = new WebSocket('wss://example.com')
@@ -213,7 +228,7 @@ test('clears the list of clients when the worker is stopped', async ({
213228
// Must purge the local storage on reload.
214229
// The worker has been started as a part of the test, not runtime,
215230
// so it will start with empty clients.
216-
expect(await page.evaluate(() => window.link.clients.size)).toBe(0)
231+
await expect(page.evaluate(() => window.link.clients.size)).resolves.toBe(0)
217232
})
218233

219234
test('clears the list of clients when the page is reloaded', async ({
@@ -237,7 +252,7 @@ test('clears the list of clients when the page is reloaded', async ({
237252

238253
await enableMocking()
239254

240-
expect(await page.evaluate(() => window.link.clients.size)).toBe(0)
255+
await expect(page.evaluate(() => window.link.clients.size)).resolves.toBe(0)
241256

242257
await page.evaluate(async () => {
243258
const ws = new WebSocket('wss://example.com')
@@ -255,5 +270,5 @@ test('clears the list of clients when the page is reloaded', async ({
255270
// Must purge the local storage on reload.
256271
// The worker has been started as a part of the test, not runtime,
257272
// so it will start with empty clients.
258-
expect(await page.evaluate(() => window.link.clients.size)).toBe(0)
273+
await expect(page.evaluate(() => window.link.clients.size)).resolves.toBe(0)
259274
})

0 commit comments

Comments
 (0)