Skip to content

Commit

Permalink
fix(runtime-core): Avoid mutating original options obj in createApp
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwu9145 authored and Yuchao9145 committed Oct 23, 2021
1 parent 7bb9dd0 commit b606e89
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/runtime-core/src/apiCreateApp.ts
Expand Up @@ -179,6 +179,9 @@ export function createAppAPI<HostElement>(
hydrate?: RootHydrateFunction
): CreateAppFunction<HostElement> {
return function createApp(rootComponent, rootProps = null) {

rootComponent = { ...rootComponent }

if (rootProps != null && !isObject(rootProps)) {
__DEV__ && warn(`root props passed to app.mount() must be an object.`)
rootProps = null
Expand Down
23 changes: 23 additions & 0 deletions packages/runtime-dom/__tests__/createApp.spec.ts
Expand Up @@ -12,4 +12,27 @@ describe('createApp for dom', () => {
expect(root.children.length).toBe(1)
expect(root.children[0] instanceof SVGElement).toBe(true)
})

// #4398
test('should not mutate original Root options object', () => {

const originalObj = {
data() {
return {
counter: 0
}
}
}

const handler = jest.fn((msg, instance, trace) => {
expect(msg).toMatch(`Component is missing template or render function`)
})

const Root = { ...originalObj}

const app = createApp(Root)
app.config.warnHandler = handler
app.mount(document.createElement('div'))
expect(originalObj).toMatchObject(Root) // ensure no additional properties are added to Root
})
})

0 comments on commit b606e89

Please sign in to comment.