Skip to content

Commit b62c6ba

Browse files
committed
fix: ensure unit test examples work in projects created with --bare
close vuejs#2262
1 parent a024513 commit b62c6ba

File tree

7 files changed

+168
-3
lines changed

7 files changed

+168
-3
lines changed

packages/@vue/cli-plugin-typescript/generator/template/src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
extend: '@vue/cli-service/generator/template/src/App.vue'
33
replace:
4-
- !!js/regexp /Welcome to Your Vue\.js App/
4+
- !!js/regexp /Welcome to Your Vue\.js App/g
55
- !!js/regexp /<script>[^]*?<\/script>/
66
---
77

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ test('base', async () => {
3030
// eslint
3131
expect(files['tests/unit/.eslintrc.js']).toMatch('jest: true')
3232

33-
const spec = files['tests/unit/HelloWorld.spec.js']
33+
const spec = files['tests/unit/example.spec.js']
3434
expect(spec).toMatch(`expect(wrapper.text()).toMatch(msg)`)
3535
})
3636

@@ -46,3 +46,65 @@ test('without babel/eslint', async () => {
4646
expect(pkg.devDependencies).not.toHaveProperty('babel-jest')
4747
expect(files['tests/unit/.eslintrc.js']).toBeUndefined()
4848
})
49+
50+
test('with TS', async () => {
51+
const { files } = await generateWithPlugin([
52+
{
53+
id: 'unit-jest',
54+
apply: require('../generator'),
55+
options: {}
56+
},
57+
// mock presence of the ts plugin
58+
{
59+
id: 'typescript',
60+
apply: () => {},
61+
options: {}
62+
}
63+
])
64+
65+
const spec = files['tests/unit/example.spec.ts']
66+
expect(spec).toMatch(`expect(wrapper.text()).toMatch(msg)`)
67+
})
68+
69+
test('bare', async () => {
70+
const { files } = await generateWithPlugin([
71+
{
72+
id: 'unit-jest',
73+
apply: require('../generator'),
74+
options: {}
75+
},
76+
{
77+
id: '@vue/cli-service',
78+
apply: () => {},
79+
options: { bare: true }
80+
}
81+
])
82+
83+
const spec = files['tests/unit/example.spec.js']
84+
expect(spec).toMatch(`const wrapper = shallowMount(App)`)
85+
expect(spec).toMatch(`expect(wrapper.text()).toMatch(\`Welcome to Your Vue.js App\`)`)
86+
})
87+
88+
test('TS + bare', async () => {
89+
const { files } = await generateWithPlugin([
90+
{
91+
id: 'unit-jest',
92+
apply: require('../generator'),
93+
options: {}
94+
},
95+
{
96+
id: 'typescript',
97+
apply: () => {},
98+
options: {}
99+
},
100+
{
101+
id: '@vue/cli-service',
102+
apply: () => {},
103+
options: { bare: true }
104+
}
105+
])
106+
107+
const spec = files['tests/unit/example.spec.ts']
108+
expect(spec).toMatch(`const wrapper = shallowMount(App)`)
109+
expect(spec).toMatch(`expect(wrapper.text()).toMatch(\`Welcome to Your Vue.js + TypeScript App\`)`)
110+
})

packages/@vue/cli-plugin-unit-jest/generator/template/tests/unit/HelloWorld.spec.js renamed to packages/@vue/cli-plugin-unit-jest/generator/template/tests/unit/example.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<%_ if (!hasTS) { _%>
22
import { shallowMount } from '@vue/test-utils'
3+
<%_ if (!rootOptions.bare) { _%>
34
import HelloWorld from '@/components/HelloWorld.vue'
45

56
describe('HelloWorld.vue', () => {
@@ -11,4 +12,12 @@ describe('HelloWorld.vue', () => {
1112
expect(wrapper.text()).toMatch(msg)
1213
})
1314
})
15+
<%_ } else { _%>
16+
import App from '@/App.vue'
17+
18+
test('App should work', () => {
19+
const wrapper = shallowMount(App)
20+
expect(wrapper.text()).toMatch(`Welcome to Your Vue.js App`)
21+
})
22+
<%_ } _%>
1423
<%_ } _%>

packages/@vue/cli-plugin-unit-jest/generator/template/tests/unit/HelloWorld.spec.ts renamed to packages/@vue/cli-plugin-unit-jest/generator/template/tests/unit/example.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<%_ if (hasTS) { _%>
22
import { shallowMount } from '@vue/test-utils'
3+
<%_ if (!rootOptions.bare) { _%>
34
import HelloWorld from '@/components/HelloWorld.vue'
45

56
describe('HelloWorld.vue', () => {
@@ -11,4 +12,12 @@ describe('HelloWorld.vue', () => {
1112
expect(wrapper.text()).toMatch(msg)
1213
})
1314
})
15+
<%_ } else { _%>
16+
import App from '@/App.vue'
17+
18+
test('App should work', () => {
19+
const wrapper = shallowMount(App)
20+
expect(wrapper.text()).toMatch(`Welcome to Your Vue.js + TypeScript App`)
21+
})
22+
<%_ } _%>
1423
<%_ } _%>

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,70 @@ test('base', async () => {
1919
expect(pkg.devDependencies).toHaveProperty('@vue/test-utils')
2020
expect(files['tests/unit/.eslintrc.js']).toMatch('mocha: true')
2121

22-
const spec = files['tests/unit/HelloWorld.spec.js']
22+
const spec = files['tests/unit/example.spec.js']
2323
expect(spec).toMatch(`import { expect } from 'chai'`)
2424
expect(spec).toMatch(`expect(wrapper.text()).to.include(msg)`)
2525
})
26+
27+
test('with TS', async () => {
28+
const { files } = await generateWithPlugin([
29+
{
30+
id: 'unit-mocha',
31+
apply: require('../generator'),
32+
options: {}
33+
},
34+
// mock presence of the ts plugin
35+
{
36+
id: 'typescript',
37+
apply: () => {},
38+
options: {}
39+
}
40+
])
41+
42+
const spec = files['tests/unit/example.spec.ts']
43+
expect(spec).toMatch(`import { expect } from 'chai'`)
44+
expect(spec).toMatch(`expect(wrapper.text()).to.include(msg)`)
45+
})
46+
47+
test('bare', async () => {
48+
const { files } = await generateWithPlugin([
49+
{
50+
id: 'unit-mocha',
51+
apply: require('../generator'),
52+
options: {}
53+
},
54+
{
55+
id: '@vue/cli-service',
56+
apply: () => {},
57+
options: { bare: true }
58+
}
59+
])
60+
61+
const spec = files['tests/unit/example.spec.js']
62+
expect(spec).toMatch(`const wrapper = shallowMount(App)`)
63+
expect(spec).toMatch(`expect(wrapper.text()).to.include(\`Welcome to Your Vue.js App\`)`)
64+
})
65+
66+
test('TS + bare', async () => {
67+
const { files } = await generateWithPlugin([
68+
{
69+
id: 'unit-mocha',
70+
apply: require('../generator'),
71+
options: {}
72+
},
73+
{
74+
id: 'typescript',
75+
apply: () => {},
76+
options: {}
77+
},
78+
{
79+
id: '@vue/cli-service',
80+
apply: () => {},
81+
options: { bare: true }
82+
}
83+
])
84+
85+
const spec = files['tests/unit/example.spec.ts']
86+
expect(spec).toMatch(`const wrapper = shallowMount(App)`)
87+
expect(spec).toMatch(`expect(wrapper.text()).to.include(\`Welcome to Your Vue.js + TypeScript App\`)`)
88+
})

packages/@vue/cli-plugin-unit-mocha/generator/template/tests/unit/HelloWorld.spec.js renamed to packages/@vue/cli-plugin-unit-mocha/generator/template/tests/unit/example.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<%_ if (!hasTS) { _%>
22
import { expect } from 'chai'
33
import { shallowMount } from '@vue/test-utils'
4+
<%_ if (!rootOptions.bare) { _%>
45
import HelloWorld from '@/components/HelloWorld.vue'
56

67
describe('HelloWorld.vue', () => {
@@ -12,4 +13,14 @@ describe('HelloWorld.vue', () => {
1213
expect(wrapper.text()).to.include(msg)
1314
})
1415
})
16+
<%_ } else { _%>
17+
import App from '@/App.vue'
18+
19+
describe('App', () => {
20+
it('should work', () => {
21+
const wrapper = shallowMount(App)
22+
expect(wrapper.text()).to.include(`Welcome to Your Vue.js App`)
23+
})
24+
})
25+
<%_ } _%>
1526
<%_ } _%>

packages/@vue/cli-plugin-unit-mocha/generator/template/tests/unit/HelloWorld.spec.ts renamed to packages/@vue/cli-plugin-unit-mocha/generator/template/tests/unit/example.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<%_ if (hasTS) { _%>
22
import { expect } from 'chai'
33
import { shallowMount } from '@vue/test-utils'
4+
<%_ if (!rootOptions.bare) { _%>
45
import HelloWorld from '@/components/HelloWorld.vue'
56

67
describe('HelloWorld.vue', () => {
@@ -12,4 +13,14 @@ describe('HelloWorld.vue', () => {
1213
expect(wrapper.text()).to.include(msg)
1314
})
1415
})
16+
<%_ } else { _%>
17+
import App from '@/App.vue'
18+
19+
describe('App', () => {
20+
it('should work', () => {
21+
const wrapper = shallowMount(App)
22+
expect(wrapper.text()).to.include(`Welcome to Your Vue.js + TypeScript App`)
23+
})
24+
})
25+
<%_ } _%>
1526
<%_ } _%>

0 commit comments

Comments
 (0)