Skip to content

Commit a74f8d7

Browse files
committed
test: use polling for more stable markdown e2e tests
close vuejs#1908
1 parent d4cc7b2 commit a74f8d7

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

packages/vue/__tests__/e2eUtils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ const puppeteerOptions = process.env.CI
66
? { args: ['--no-sandbox', '--disable-setuid-sandbox'] }
77
: {}
88

9+
const maxTries = 20
10+
export const timeout = (n: number) => new Promise(r => setTimeout(r, n))
11+
12+
export async function expectByPolling(
13+
poll: () => Promise<any>,
14+
expected: string
15+
) {
16+
for (let tries = 0; tries < maxTries; tries++) {
17+
const actual = (await poll()) || ''
18+
if (actual.indexOf(expected) > -1 || tries === maxTries - 1) {
19+
expect(actual).toMatch(expected)
20+
break
21+
} else {
22+
await timeout(50)
23+
}
24+
}
25+
}
26+
927
export function setupPuppeteer() {
1028
let browser: puppeteer.Browser
1129
let page: puppeteer.Page

packages/vue/examples/__tests__/markdown.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import path from 'path'
2-
import { setupPuppeteer, E2E_TIMEOUT } from '../../__tests__/e2eUtils'
2+
import {
3+
setupPuppeteer,
4+
expectByPolling,
5+
E2E_TIMEOUT
6+
} from '../../__tests__/e2eUtils'
37

48
describe('e2e: markdown', () => {
59
const { page, isVisible, value, html } = setupPuppeteer()
@@ -18,8 +22,8 @@ describe('e2e: markdown', () => {
1822
await page().type('textarea', '\n## foo\n\n- bar\n- baz')
1923
// assert the output is not updated yet because of debounce
2024
expect(await html('#editor div')).toBe('<h1 id="hello">hello</h1>\n')
21-
await page().waitFor(200)
22-
expect(await html('#editor div')).toBe(
25+
await expectByPolling(
26+
() => html('#editor div'),
2327
'<h1 id="hello">hello</h1>\n' +
2428
'<h2 id="foo">foo</h2>\n' +
2529
'<ul>\n<li>bar</li>\n<li>baz</li>\n</ul>\n'

0 commit comments

Comments
 (0)