Skip to content

Commit 2622f8d

Browse files
committed
test: use page.waitForFunction for HMR testing
1 parent 0ca37b7 commit 2622f8d

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

packages/@vue/cli-plugin-typescript/__tests__/tsPlugin.helper.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@ const create = require('@vue/cli-test-utils/createTestProject')
55
const serve = require('@vue/cli-test-utils/serveWithPuppeteer')
66
const launchPuppeteer = require('@vue/cli-test-utils/launchPuppeteer')
77

8-
const sleep = n => new Promise(resolve => setTimeout(resolve, n))
9-
108
exports.assertServe = async (name, options) => {
119
test('serve', async () => {
1210
const project = await create(name, options)
1311

1412
await serve(
1513
() => project.run('vue-cli-service serve'),
16-
async ({ nextUpdate, helpers }) => {
14+
async ({ page, nextUpdate, helpers }) => {
1715
const msg = `Welcome to Your Vue.js + TypeScript App`
1816
expect(await helpers.getText('h1')).toMatch(msg)
1917

2018
// test hot reload
2119
const file = await project.read(`src/App.vue`)
2220
project.write(`src/App.vue`, file.replace(msg, `Updated`))
2321
await nextUpdate() // wait for child stdout update signal
24-
await sleep(5000) // give the client time to update
25-
expect(await helpers.getText('h1')).toMatch(`Updated`)
22+
await page.waitForFunction(selector => {
23+
return document.querySelector(selector).textContent.includes('Updated')
24+
}, {}, 'h1')
2625
}
2726
)
2827
})

packages/@vue/cli-service-global/__tests__/globalService.spec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const launchPuppeteer = require('@vue/cli-test-utils/launchPuppeteer')
1010

1111
const cwd = path.resolve(__dirname, 'temp')
1212
const binPath = require.resolve('@vue/cli/bin/vue')
13-
const sleep = n => new Promise(resolve => setTimeout(resolve, n))
1413
const write = (file, content) => fs.writeFile(path.join(cwd, file), content)
1514

1615
const entryVue = fs.readFileSync(path.resolve(__dirname, 'entry.vue'), 'utf-8')
@@ -32,12 +31,14 @@ beforeEach(async () => {
3231
test('global serve', async () => {
3332
await serve(
3433
() => execa(binPath, ['serve'], { cwd }),
35-
async ({ nextUpdate, helpers }) => {
34+
async ({ page, nextUpdate, helpers }) => {
3635
expect(await helpers.getText('h1')).toMatch('hi')
3736
write('App.vue', entryVue.replace(`{{ msg }}`, 'Updated'))
3837
await nextUpdate() // wait for child stdout update signal
39-
await sleep(1000) // give the client time to update
40-
expect(await helpers.getText('h1')).toMatch(`Updated`)
38+
await page.waitForFunction(selector => {
39+
const el = document.querySelector(selector)
40+
return el && el.textContent.includes('Updated')
41+
}, {}, 'h1')
4142
}
4243
)
4344
})

packages/@vue/cli-service/__tests__/serve.spec.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ const { defaultPreset } = require('@vue/cli/lib/options')
66
const create = require('@vue/cli-test-utils/createTestProject')
77
const serve = require('@vue/cli-test-utils/serveWithPuppeteer')
88

9-
const sleep = n => new Promise(resolve => setTimeout(resolve, n))
10-
119
test('serve', async () => {
1210
const project = await create('e2e-serve', defaultPreset)
1311

1412
await serve(
1513
() => project.run('vue-cli-service serve'),
16-
async ({ nextUpdate, helpers }) => {
14+
async ({ page, nextUpdate, helpers }) => {
1715
const msg = `Welcome to Your Vue.js App`
1816
expect(await helpers.getText('h1')).toMatch(msg)
1917

2018
// test hot reload
2119
const file = await project.read(`src/App.vue`)
2220
project.write(`src/App.vue`, file.replace(msg, `Updated`))
2321
await nextUpdate() // wait for child stdout update signal
24-
await sleep(5000) // give the client time to update
25-
expect(await helpers.getText('h1')).toMatch(`Updated`)
22+
await page.waitForFunction(selector => {
23+
const el = document.querySelector(selector)
24+
return el && el.textContent.includes('Updated')
25+
}, {}, 'h1')
2626
}
2727
)
2828
})
@@ -97,16 +97,18 @@ test('serve with inline entry', async () => {
9797

9898
await serve(
9999
() => project.run('vue-cli-service serve src/index.js'),
100-
async ({ nextUpdate, helpers }) => {
100+
async ({ page, nextUpdate, helpers }) => {
101101
const msg = `Welcome to Your Vue.js App`
102102
expect(await helpers.getText('h1')).toMatch(msg)
103103

104104
// test hot reload
105105
const file = await project.read(`src/App.vue`)
106106
project.write(`src/App.vue`, file.replace(msg, `Updated`))
107107
await nextUpdate() // wait for child stdout update signal
108-
await sleep(1000) // give the client time to update
109-
expect(await helpers.getText('h1')).toMatch(`Updated`)
108+
await page.waitForFunction(selector => {
109+
const el = document.querySelector(selector)
110+
return el && el.textContent.includes('Updated')
111+
}, {}, 'h1')
110112
}
111113
)
112114
})
@@ -118,16 +120,18 @@ test('serve with no public dir', async () => {
118120

119121
await serve(
120122
() => project.run('vue-cli-service serve'),
121-
async ({ nextUpdate, helpers }) => {
123+
async ({ page, nextUpdate, helpers }) => {
122124
const msg = `Welcome to Your Vue.js App`
123125
expect(await helpers.getText('h1')).toMatch(msg)
124126

125127
// test hot reload
126128
const file = await project.read(`src/App.vue`)
127129
project.write(`src/App.vue`, file.replace(msg, `Updated`))
128130
await nextUpdate() // wait for child stdout update signal
129-
await sleep(1000) // give the client time to update
130-
expect(await helpers.getText('h1')).toMatch(`Updated`)
131+
await page.waitForFunction(selector => {
132+
const el = document.querySelector(selector)
133+
return el && el.textContent.includes('Updated')
134+
}, {}, 'h1')
131135
}
132136
)
133137
})

0 commit comments

Comments
 (0)