Skip to content

Commit c293cf1

Browse files
committed
feat: Adds provide to the global config.
1 parent db5e07e commit c293cf1

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

docs/en/api/config.md

+19
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,22 @@ VueTestUtils.config.methods['errors'] = () => {
6060
any: () => false
6161
}
6262
```
63+
64+
### `provide`
65+
66+
- type: `Object`
67+
- default: `{}`
68+
69+
Like `stubs` or `mocks`, the values passed to `config.provide` are used by default. Any values passed to the mounting options `provide` object will take priority over the ones declared in `config.provide`. **Please take note that it is not supported to pass a function as `config.provide`.**
70+
71+
Example:
72+
73+
```js
74+
import VueTestUtils from '@vue/test-utils'
75+
76+
VueTestUtils.config.provide['$logger'] = {
77+
log: (...args) => {
78+
console.log(...args)
79+
}
80+
}
81+
```

packages/shared/merge-options.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
function getOptions (key, options, config) {
44
if (options ||
55
(config[key] && Object.keys(config[key]).length > 0)) {
6-
if (Array.isArray(options)) {
6+
if (options instanceof Function) {
7+
return options
8+
} else if (Array.isArray(options)) {
79
return [
810
...options,
911
...Object.keys(config[key] || {})]
10-
} else {
12+
} else if (!(config[key] instanceof Function)) {
1113
return {
1214
...config[key],
1315
...options
1416
}
17+
} else {
18+
throw new Error(`Config can't be a Function.`)
1519
}
1620
}
1721
}
@@ -24,7 +28,8 @@ export function mergeOptions (
2428
...options,
2529
stubs: getOptions('stubs', options.stubs, config),
2630
mocks: getOptions('mocks', options.mocks, config),
27-
methods: getOptions('methods', options.methods, config)
31+
methods: getOptions('methods', options.methods, config),
32+
provide: getOptions('provide', options.provide, config)
2833
}
2934
}
3035

packages/test-utils/src/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ export default {
77
'transition-group': TransitionGroupStub
88
},
99
mocks: {},
10-
methods: {}
10+
methods: {},
11+
provide: {}
1112
}

test/specs/mounting-options/provide.spec.js

+46
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { config } from '~vue/test-utils'
12
import ComponentWithInject from '~resources/components/component-with-inject.vue'
23
import { injectSupported } from '~resources/utils'
34
import {
@@ -6,6 +7,17 @@ import {
67
} from '~resources/utils'
78

89
describeWithMountingMethods('options.provide', (mountingMethod) => {
10+
let configProvideSave
11+
12+
beforeEach(() => {
13+
configProvideSave = config.provide
14+
config.provide = {}
15+
})
16+
17+
afterEach(() => {
18+
config.provide = configProvideSave
19+
})
20+
921
itDoNotRunIf(!injectSupported(),
1022
'provides objects which is injected by mounted component', () => {
1123
if (!injectSupported()) return
@@ -44,4 +56,38 @@ describeWithMountingMethods('options.provide', (mountingMethod) => {
4456

4557
expect(wrapper.vm.setInBeforeCreate).to.equal('created')
4658
})
59+
60+
it('injects the provide from the config', () => {
61+
config.provide['fromMount'] = 'globalConfig'
62+
63+
const wrapper = mountingMethod(ComponentWithInject)
64+
const HTML = mountingMethod.name === 'renderToString'
65+
? wrapper
66+
: wrapper.html()
67+
68+
expect(HTML).to.contain('globalConfig')
69+
})
70+
71+
it('prioritize mounting options over config', () => {
72+
config.provide['fromMount'] = 'globalConfig'
73+
74+
const wrapper = mountingMethod(ComponentWithInject, {
75+
provide: { fromMount: '_' }
76+
})
77+
const HTML = mountingMethod.name === 'renderToString'
78+
? wrapper
79+
: wrapper.html()
80+
81+
expect(HTML).to.contain('_')
82+
})
83+
84+
it('config with function throws', () => {
85+
config.provide = () => {}
86+
87+
expect(() => {
88+
mountingMethod(ComponentWithInject, {
89+
provide: { fromMount: '_' }
90+
})
91+
}).to.throw()
92+
})
4793
})

0 commit comments

Comments
 (0)