Skip to content

Commit b606e89

Browse files
yuwu9145Yuchao9145
authored andcommitted
fix(runtime-core): Avoid mutating original options obj in createApp
1 parent 7bb9dd0 commit b606e89

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

packages/runtime-core/src/apiCreateApp.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ export function createAppAPI<HostElement>(
179179
hydrate?: RootHydrateFunction
180180
): CreateAppFunction<HostElement> {
181181
return function createApp(rootComponent, rootProps = null) {
182+
183+
rootComponent = { ...rootComponent }
184+
182185
if (rootProps != null && !isObject(rootProps)) {
183186
__DEV__ && warn(`root props passed to app.mount() must be an object.`)
184187
rootProps = null

packages/runtime-dom/__tests__/createApp.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,27 @@ describe('createApp for dom', () => {
1212
expect(root.children.length).toBe(1)
1313
expect(root.children[0] instanceof SVGElement).toBe(true)
1414
})
15+
16+
// #4398
17+
test('should not mutate original Root options object', () => {
18+
19+
const originalObj = {
20+
data() {
21+
return {
22+
counter: 0
23+
}
24+
}
25+
}
26+
27+
const handler = jest.fn((msg, instance, trace) => {
28+
expect(msg).toMatch(`Component is missing template or render function`)
29+
})
30+
31+
const Root = { ...originalObj}
32+
33+
const app = createApp(Root)
34+
app.config.warnHandler = handler
35+
app.mount(document.createElement('div'))
36+
expect(originalObj).toMatchObject(Root) // ensure no additional properties are added to Root
37+
})
1538
})

0 commit comments

Comments
 (0)