Skip to content

Commit 3dd9a07

Browse files
authored
test: add test case for vuejs#13210
The tests need to be implemented as E2E because the leak is in the detached DOM nodes, which were not possible to reproduce in Vitest regardless of the presence of JSDOM or not to mock the DOM (in runtime/core tests we test VNodes, not real DOM elements)
1 parent 3f27c58 commit 3dd9a07

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

packages/vue/__tests__/e2e/gc.spec.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import path from 'node:path'
2+
import { setupPuppeteer } from './e2eUtils'
3+
4+
const { page } = setupPuppeteer()
5+
6+
beforeEach(async () => {
7+
await page().addScriptTag({
8+
path: path.resolve(__dirname, '../../dist/vue.global.js'),
9+
})
10+
await page().setContent(`<div id="app"></div>`)
11+
})
12+
13+
describe('e2e: gc', () => {
14+
test('should stop tracking deps in setRef during unmount', async () => {
15+
const client = await page().createCDPSession()
16+
await page().evaluate(async () => {
17+
const {
18+
createApp,
19+
shallowRef,
20+
computed,
21+
nextTick,
22+
getCurrentInstance,
23+
onMounted,
24+
} = (window as any).Vue
25+
const show = shallowRef(true)
26+
27+
createApp({
28+
components: {
29+
Comp: {
30+
setup: () => {
31+
const foo = computed(() => {})
32+
onMounted(() => {
33+
// @ts-expect-error - Custom property and same lib as runtime is used
34+
window.__REF__ = new WeakRef(getCurrentInstance().subTree.el)
35+
})
36+
37+
return { foo }
38+
},
39+
template: `
40+
<h1 :ref="() => { foo }">Component</h1>
41+
`,
42+
},
43+
},
44+
template: `
45+
<Comp v-if="show"></Comp>
46+
`,
47+
setup() {
48+
return { show }
49+
},
50+
}).mount('#app')
51+
52+
await nextTick()
53+
show.value = false
54+
await nextTick()
55+
})
56+
57+
const isCollected = async () =>
58+
// @ts-expect-error - Custom property
59+
await page().evaluate(() => window.__REF__.deref() === undefined)
60+
61+
while ((await isCollected()) === false) {
62+
await client.send('HeapProfiler.collectGarbage')
63+
}
64+
65+
expect(await isCollected()).toBe(true)
66+
})
67+
})

0 commit comments

Comments
 (0)