Skip to content

Commit 44f7798

Browse files
committed
Allow mocks to be set in config object
1 parent 27bcee7 commit 44f7798

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

packages/shared/merge-options.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,29 @@ function getStubs (optionStubs, config) {
1616
}
1717
}
1818

19+
function getMocks (optionMocks, config) {
20+
if (optionMocks ||
21+
(config.mocks && Object.keys(config.mocks).length > 0)) {
22+
if (Array.isArray(optionMocks)) {
23+
return [
24+
...optionMocks,
25+
...Object.keys(config.mocks || {})]
26+
} else {
27+
return {
28+
...config.mocks,
29+
...optionMocks
30+
}
31+
}
32+
}
33+
}
34+
1935
export function mergeOptions (
2036
options: Options,
2137
config: Options
2238
): Options {
2339
return {
2440
...options,
25-
stubs: getStubs(options.stubs, config)
41+
stubs: getStubs(options.stubs, config),
42+
mocks: getMocks(options.mocks, config)
2643
}
2744
}

packages/test-utils/src/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export default {
55
stubs: {
66
transition: TransitionStub,
77
'transition-group': TransitionGroupStub
8-
}
8+
},
9+
mocks: {}
910
}

test/specs/config.spec.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import {
22
mount,
33
config,
44
TransitionStub,
5-
TransitionGroupStub
5+
TransitionGroupStub,
6+
createLocalVue
67
} from '~vue/test-utils'
78

89
describe('config', () => {
@@ -33,6 +34,29 @@ describe('config', () => {
3334
expect(wrapper.contains(TransitionGroupStub)).to.equal(true)
3435
})
3536

37+
it('mocks a global variable', () => {
38+
const localVue = createLocalVue()
39+
const t = 'real value'
40+
localVue.prototype.$t = t
41+
42+
const testComponent = {
43+
template: `
44+
<div>{{ $t }}</div>
45+
`
46+
}
47+
48+
config.mocks['$t'] = 'mock value'
49+
50+
const wrapper = mount(testComponent, {
51+
localVue, t
52+
})
53+
54+
expect(wrapper.vm.$t).to.equal('mock value')
55+
expect(wrapper.text()).to.equal('mock value')
56+
57+
localVue.prototype.$t = undefined
58+
})
59+
3660
it('doesn\'t stub transition when config.stubs.transition is set to false', () => {
3761
const testComponent = {
3862
template: `

0 commit comments

Comments
 (0)