Skip to content

Commit d6e493d

Browse files
authored
fix(unit-jest, unit-mocha): generate passing tests when bare option is used with router enabled (#5591)
Fixes #3544
1 parent f42888d commit d6e493d

File tree

8 files changed

+202
-2
lines changed

8 files changed

+202
-2
lines changed

Diff for: packages/@vue/cli-plugin-unit-jest/__tests__/jestGenerator.spec.js

+53
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,56 @@ test('TS + bare', async () => {
8888
expect(spec).toMatch(`const wrapper = shallowMount(App)`)
8989
expect(spec).toMatch(`expect(wrapper.text()).toMatch(\`Welcome to Your Vue.js + TypeScript App\`)`)
9090
})
91+
92+
test('bare + router', async () => {
93+
const { files } = await generateWithPlugin([
94+
{
95+
id: 'unit-jest',
96+
apply: require('../generator'),
97+
options: {}
98+
},
99+
{
100+
id: '@vue/cli-service',
101+
apply: () => {},
102+
options: { bare: true }
103+
},
104+
{
105+
id: 'router',
106+
apply: () => {},
107+
options: {}
108+
}
109+
])
110+
111+
const spec = files['tests/unit/example.spec.js']
112+
expect(spec).toMatch(`const wrapper = mount(App,`)
113+
expect(spec).toMatch(`expect(wrapper.text()).toMatch(\`Welcome to Your Vue.js App\`)`)
114+
})
115+
116+
test('TS + bare + router', async () => {
117+
const { files } = await generateWithPlugin([
118+
{
119+
id: 'unit-jest',
120+
apply: require('../generator'),
121+
options: {}
122+
},
123+
{
124+
id: 'typescript',
125+
apply: () => {},
126+
options: {}
127+
},
128+
{
129+
id: '@vue/cli-service',
130+
apply: () => {},
131+
options: { bare: true }
132+
},
133+
{
134+
id: 'router',
135+
apply: () => {},
136+
options: {}
137+
}
138+
])
139+
140+
const spec = files['tests/unit/example.spec.ts']
141+
expect(spec).toMatch(`const wrapper = mount(App,`)
142+
expect(spec).toMatch(`expect(wrapper.text()).toMatch(\`Welcome to Your Vue.js App\`)`)
143+
})

Diff for: packages/@vue/cli-plugin-unit-jest/generator/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module.exports = (api, options, rootOptions, invoking) => {
33

44
api.render('./template', {
55
isVue3,
6-
hasTS: api.hasPlugin('typescript')
6+
hasTS: api.hasPlugin('typescript'),
7+
hasRouter: api.hasPlugin('router')
78
})
89

910
api.extendPackage({

Diff for: packages/@vue/cli-plugin-unit-jest/generator/template/tests/unit/example.spec.js

+22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<%_ if (!hasTS) { _%>
2+
<%_ if (!rootOptions.bare || !hasRouter) { _%>
23
import { shallowMount } from '@vue/test-utils'
4+
<%_ } else { _%>
5+
import { mount, createLocalVue } from '@vue/test-utils'
6+
<%_ } _%>
37
<%_ if (!rootOptions.bare) { _%>
48
import HelloWorld from '@/components/HelloWorld.vue'
59

@@ -18,10 +22,28 @@ describe('HelloWorld.vue', () => {
1822
})
1923
<%_ } else { _%>
2024
import App from '@/App.vue'
25+
<%_ if (!hasRouter) { _%>
2126

2227
test('App should work', () => {
2328
const wrapper = shallowMount(App)
2429
expect(wrapper.text()).toMatch(`Welcome to Your Vue.js App`)
2530
})
31+
32+
<%_ } else {_%>
33+
import VueRouter from 'vue-router'
34+
import router from '@/router'
35+
36+
const localVue = createLocalVue()
37+
localVue.use(VueRouter)
38+
39+
test('App should render default route', () => {
40+
const wrapper = mount(App, {
41+
localVue,
42+
router
43+
})
44+
expect(wrapper.text()).toMatch(`Welcome to Your Vue.js App`)
45+
})
46+
47+
<%_ } _%>
2648
<%_ } _%>
2749
<%_ } _%>

Diff for: packages/@vue/cli-plugin-unit-jest/generator/template/tests/unit/example.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<%_ if (hasTS) { _%>
2+
<%_ if (!rootOptions.bare || !hasRouter) { _%>
23
import { shallowMount } from '@vue/test-utils'
4+
<%_ } else { _%>
5+
import { mount, createLocalVue } from '@vue/test-utils'
6+
<%_ } _%>
37
<%_ if (!rootOptions.bare) { _%>
48
import HelloWorld from '@/components/HelloWorld.vue'
59

@@ -18,10 +22,28 @@ describe('HelloWorld.vue', () => {
1822
})
1923
<%_ } else { _%>
2024
import App from '@/App.vue'
25+
<%_ if (!hasRouter) { _%>
2126

2227
test('App should work', () => {
2328
const wrapper = shallowMount(App)
2429
expect(wrapper.text()).toMatch(`Welcome to Your Vue.js + TypeScript App`)
2530
})
31+
32+
<%_ } else {_%>
33+
import VueRouter from 'vue-router'
34+
import router from '@/router'
35+
36+
const localVue = createLocalVue()
37+
localVue.use(VueRouter)
38+
39+
test('App should render default route', () => {
40+
const wrapper = mount(App, {
41+
localVue,
42+
router
43+
})
44+
expect(wrapper.text()).toMatch(`Welcome to Your Vue.js App`)
45+
})
46+
47+
<%_ } _%>
2648
<%_ } _%>
2749
<%_ } _%>

Diff for: packages/@vue/cli-plugin-unit-mocha/__tests__/mochaGenerator.spec.js

+53
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,56 @@ test('TS + bare', async () => {
8585
expect(spec).toMatch(`const wrapper = shallowMount(App)`)
8686
expect(spec).toMatch(`expect(wrapper.text()).to.include(\`Welcome to Your Vue.js + TypeScript App\`)`)
8787
})
88+
89+
test('bare + router', async () => {
90+
const { files } = await generateWithPlugin([
91+
{
92+
id: 'unit-mocha',
93+
apply: require('../generator'),
94+
options: {}
95+
},
96+
{
97+
id: '@vue/cli-service',
98+
apply: () => {},
99+
options: { bare: true }
100+
},
101+
{
102+
id: 'router',
103+
apply: () => {},
104+
options: {}
105+
}
106+
])
107+
108+
const spec = files['tests/unit/example.spec.js']
109+
expect(spec).toMatch(`const wrapper = mount(App,`)
110+
expect(spec).toMatch(`expect(wrapper.text()).to.include(\`Welcome to Your Vue.js App\`)`)
111+
})
112+
113+
test('TS + bare + router', async () => {
114+
const { files } = await generateWithPlugin([
115+
{
116+
id: 'unit-mocha',
117+
apply: require('../generator'),
118+
options: {}
119+
},
120+
{
121+
id: 'typescript',
122+
apply: () => {},
123+
options: {}
124+
},
125+
{
126+
id: '@vue/cli-service',
127+
apply: () => {},
128+
options: { bare: true }
129+
},
130+
{
131+
id: 'router',
132+
apply: () => {},
133+
options: {}
134+
}
135+
])
136+
137+
const spec = files['tests/unit/example.spec.ts']
138+
expect(spec).toMatch(`const wrapper = mount(App,`)
139+
expect(spec).toMatch(`expect(wrapper.text()).to.include(\`Welcome to Your Vue.js App\`)`)
140+
})

Diff for: packages/@vue/cli-plugin-unit-mocha/generator/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module.exports = (api, options, rootOptions, invoking) => {
33

44
api.render('./template', {
55
isVue3,
6-
hasTS: api.hasPlugin('typescript')
6+
hasTS: api.hasPlugin('typescript'),
7+
hasRouter: api.hasPlugin('router')
78
})
89

910
api.extendPackage({

Diff for: packages/@vue/cli-plugin-unit-mocha/generator/template/tests/unit/example.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<%_ if (!hasTS) { _%>
22
import { expect } from 'chai'
3+
<%_ if (!rootOptions.bare || !hasRouter) { _%>
34
import { shallowMount } from '@vue/test-utils'
5+
<%_ } else { _%>
6+
import { mount, createLocalVue } from '@vue/test-utils'
7+
<%_ } _%>
48
<%_ if (!rootOptions.bare) { _%>
59
import HelloWorld from '@/components/HelloWorld.vue'
610

@@ -19,12 +23,32 @@ describe('HelloWorld.vue', () => {
1923
})
2024
<%_ } else { _%>
2125
import App from '@/App.vue'
26+
<%_ if (!hasRouter) { _%>
2227

2328
describe('App', () => {
2429
it('should work', () => {
2530
const wrapper = shallowMount(App)
2631
expect(wrapper.text()).to.include(`Welcome to Your Vue.js App`)
2732
})
2833
})
34+
35+
<%_ } else {_%>
36+
import VueRouter from 'vue-router'
37+
import router from '@/router'
38+
39+
const localVue = createLocalVue()
40+
localVue.use(VueRouter)
41+
42+
describe('App', () => {
43+
it('should render default route', () => {
44+
const wrapper = mount(App, {
45+
localVue,
46+
router
47+
})
48+
expect(wrapper.text()).to.include(`Welcome to Your Vue.js App`)
49+
})
50+
})
51+
52+
<%_ } _%>
2953
<%_ } _%>
3054
<%_ } _%>

Diff for: packages/@vue/cli-plugin-unit-mocha/generator/template/tests/unit/example.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<%_ if (hasTS) { _%>
22
import { expect } from 'chai'
3+
<%_ if (!rootOptions.bare || !hasRouter) { _%>
34
import { shallowMount } from '@vue/test-utils'
5+
<%_ } else { _%>
6+
import { mount, createLocalVue } from '@vue/test-utils'
7+
<%_ } _%>
48
<%_ if (!rootOptions.bare) { _%>
59
import HelloWorld from '@/components/HelloWorld.vue'
610

@@ -19,12 +23,32 @@ describe('HelloWorld.vue', () => {
1923
})
2024
<%_ } else { _%>
2125
import App from '@/App.vue'
26+
<%_ if (!hasRouter) { _%>
2227

2328
describe('App', () => {
2429
it('should work', () => {
2530
const wrapper = shallowMount(App)
2631
expect(wrapper.text()).to.include(`Welcome to Your Vue.js + TypeScript App`)
2732
})
2833
})
34+
35+
<%_ } else {_%>
36+
import VueRouter from 'vue-router'
37+
import router from '@/router'
38+
39+
const localVue = createLocalVue()
40+
localVue.use(VueRouter)
41+
42+
describe('App', () => {
43+
it('should render default route', () => {
44+
const wrapper = mount(App, {
45+
localVue,
46+
router
47+
})
48+
expect(wrapper.text()).to.include(`Welcome to Your Vue.js App`)
49+
})
50+
})
51+
52+
<%_ } _%>
2953
<%_ } _%>
3054
<%_ } _%>

0 commit comments

Comments
 (0)