Skip to content

Commit 68a55f1

Browse files
authored
fix: automatically extend extended child components (#595)
1 parent 14c40e6 commit 68a55f1

File tree

22 files changed

+168
-55
lines changed

22 files changed

+168
-55
lines changed

docs/en/api/config.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,18 @@ VueTestUtils.config.provide['$logger'] = {
7979
}
8080
}
8181
```
82+
83+
### `logModifiedComponents`
84+
85+
- type: `Boolean`
86+
- default: `true`
87+
88+
Logs warning when extended child components are automatically stubbed. Hides warnings when set to `false`. Unlike other config options, this cannot be set on the mounting options.
89+
90+
Example:
91+
92+
```js
93+
import VueTestUtils from '@vue/test-utils'
94+
95+
VueTestUtils.config.logModifiedComponents = false
96+
```

flow/options.flow.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ declare type Options = { // eslint-disable-line no-undef
99
stubs?: Object,
1010
context?: Object,
1111
attrs?: Object,
12-
listeners?: Object
12+
listeners?: Object,
13+
logModifiedComponents?: Boolean
1314
}

packages/create-instance/compile-template.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/create-instance/create-instance.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import addListeners from './add-listeners'
99
import addProvide from './add-provide'
1010
import { addEventLogger } from './log-events'
1111
import { createComponentStubs } from 'shared/stub-components'
12-
import { throwError } from 'shared/util'
13-
import { compileTemplate } from './compile-template'
12+
import { throwError, warn } from 'shared/util'
13+
import { compileTemplate } from 'shared/compile-template'
1414
import deleteoptions from './delete-mounting-options'
1515
import createFunctionalComponent from './create-functional-component'
1616
import { componentNeedsCompiling } from 'shared/validators'
@@ -70,6 +70,16 @@ export default function createInstance (
7070
}
7171
}
7272

73+
Object.keys(component.components || {}).forEach((c) => {
74+
if (component.components[c].extendOptions &&
75+
!instanceOptions.components[c]) {
76+
if (options.logModifiedComponents) {
77+
warn(`an extended child component ${c} has been modified to ensure it has the correct instance properties. This means it is not possible to find the component with a component selector. To find the component, you must stub it manually using the mocks mounting option.`)
78+
}
79+
instanceOptions.components[c] = vue.extend(component.components[c])
80+
}
81+
})
82+
7383
Object.keys(stubComponents).forEach(c => {
7484
vue.component(c, stubComponents[c])
7585
})

packages/server-test-utils/types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ interface VueTestUtilsConfigOptions {
4949
stubs?: Stubs
5050
mocks?: object
5151
methods?: Record<string, Function>
52-
provide?: object
52+
provide?: object,
53+
logModifiedComponents?: Boolean
5354
}
5455

5556
export declare let config: VueTestUtilsConfigOptions

packages/shared/compile-template.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ export function compileTemplate (component: Component) {
1111
}
1212
})
1313
}
14+
1415
if (component.extends) {
1516
compileTemplate(component.extends)
1617
}
18+
19+
if (component.extendOptions && !component.options.render) {
20+
compileTemplate(component.options)
21+
}
22+
1723
if (component.template) {
1824
Object.assign(component, compileToFunctions(component.template))
1925
}

packages/shared/merge-options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export function mergeOptions (
2626
): Options {
2727
return {
2828
...options,
29+
logModifiedComponents: config.logModifiedComponents,
2930
stubs: getOptions('stubs', options.stubs, config),
3031
mocks: getOptions('mocks', options.mocks, config),
3132
methods: getOptions('methods', options.methods, config),

packages/test-utils/src/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export default {
88
},
99
mocks: {},
1010
methods: {},
11-
provide: {}
11+
provide: {},
12+
logModifiedComponents: true
1213
}

packages/test-utils/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import config from './config'
88
import { warn } from 'shared/util'
99

1010
function shallow (component, options) {
11-
warn('shallow has been renamed to shallowMount and will be deprecated in 1.0.0')
11+
warn('shallow has been renamed to shallowMount. shallow will be removed in 1.0.0, use shallowMount instead')
1212
return shallowMount(component, options)
1313
}
1414

packages/test-utils/types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ interface VueTestUtilsConfigOptions {
139139
stubs?: Stubs
140140
mocks?: object
141141
methods?: Record<string, Function>
142-
provide?: object
142+
provide?: object,
143+
logModifiedComponents?: Boolean
143144
}
144145

145146
export declare function createLocalVue (): typeof Vue

0 commit comments

Comments
 (0)