Skip to content

Commit 252dd3d

Browse files
committed
fix: improve jest/mocha add compat with typescript
1 parent cf6290f commit 252dd3d

File tree

6 files changed

+81
-10
lines changed

6 files changed

+81
-10
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
module.exports = api => {
2-
api.render('./template')
1+
module.exports = (api, _, __, invoking) => {
2+
api.render('./template', {
3+
hasTS: api.hasPlugin('typescript')
4+
})
5+
36
api.extendPackage({
47
scripts: {
58
'test:unit': 'vue-cli-service test:unit'
@@ -60,16 +63,15 @@ module.exports = api => {
6063
})
6164
}
6265
} else {
63-
applyTS(api)
66+
applyTS(api, invoking)
6467
}
6568

6669
if (api.hasPlugin('eslint')) {
6770
applyESLint(api)
6871
}
6972
}
7073

71-
const applyTS = module.exports.applyTS = api => {
72-
// TODO inject type into tsconfig.json
74+
const applyTS = module.exports.applyTS = (api, invoking) => {
7375
api.extendPackage({
7476
jest: {
7577
moduleFileExtensions: ['ts', 'tsx'],
@@ -90,6 +92,22 @@ const applyTS = module.exports.applyTS = api => {
9092
}
9193
})
9294
}
95+
// inject jest type to tsconfig.json
96+
if (invoking) {
97+
api.render(files => {
98+
const tsconfig = files['tsconfig.json']
99+
if (tsconfig) {
100+
const parsed = JSON.parse(tsconfig)
101+
if (
102+
parsed.compilerOptions.types &&
103+
!parsed.compilerOptions.types.includes('jest')
104+
) {
105+
parsed.compilerOptions.types.push('jest')
106+
}
107+
files['tsconfig.json'] = JSON.stringify(parsed, null, 2)
108+
}
109+
})
110+
}
93111
}
94112

95113
const applyESLint = module.exports.applyESLint = api => {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<%_ if (!hasTS) { _%>
12
import { shallowMount } from '@vue/test-utils'
23
import HelloWorld from '@/components/HelloWorld.vue'
34

@@ -10,3 +11,4 @@ describe('HelloWorld.vue', () => {
1011
expect(wrapper.text()).toMatch(msg)
1112
})
1213
})
14+
<%_ } _%>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<%_ if (hasTS) { _%>
2+
import { shallowMount } from '@vue/test-utils'
3+
import HelloWorld from '@/components/HelloWorld.vue'
4+
5+
describe('HelloWorld.vue', () => {
6+
it('renders props.msg when passed', () => {
7+
const msg = 'new message'
8+
const wrapper = shallowMount(HelloWorld, {
9+
propsData: { msg }
10+
})
11+
expect(wrapper.text()).toMatch(msg)
12+
})
13+
})
14+
<%_ } _%>

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
module.exports = api => {
2-
api.render('./template')
1+
module.exports = (api, _, __, invoking) => {
2+
api.render('./template', {
3+
hasTS: api.hasPlugin('typescript')
4+
})
35

46
api.extendPackage({
57
devDependencies: {
@@ -16,7 +18,7 @@ module.exports = api => {
1618
}
1719

1820
if (api.hasPlugin('typescript')) {
19-
applyTS(api)
21+
applyTS(api, invoking)
2022
}
2123
}
2224

@@ -31,12 +33,30 @@ const applyESLint = module.exports.applyESLint = api => {
3133
})
3234
}
3335

34-
const applyTS = module.exports.applyTS = api => {
35-
// TODO inject type into tsconfig.json
36+
const applyTS = module.exports.applyTS = (api, invoking) => {
3637
api.extendPackage({
3738
devDependencies: {
3839
'@types/mocha': '^5.2.4',
3940
'@types/chai': '^4.1.0'
4041
}
4142
})
43+
// inject mocha/chai types to tsconfig.json
44+
if (invoking) {
45+
api.render(files => {
46+
const tsconfig = files['tsconfig.json']
47+
if (tsconfig) {
48+
const parsed = JSON.parse(tsconfig)
49+
const types = parsed.compilerOptions.types
50+
if (types) {
51+
if (!types.includes('mocha')) {
52+
types.push('mocha')
53+
}
54+
if (!types.includes('chai')) {
55+
types.push('chai')
56+
}
57+
}
58+
files['tsconfig.json'] = JSON.stringify(parsed, null, 2)
59+
}
60+
})
61+
}
4262
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<%_ if (!hasTS) { _%>
12
import { expect } from 'chai'
23
import { shallowMount } from '@vue/test-utils'
34
import HelloWorld from '@/components/HelloWorld.vue'
@@ -11,3 +12,4 @@ describe('HelloWorld.vue', () => {
1112
expect(wrapper.text()).to.include(msg)
1213
})
1314
})
15+
<%_ } _%>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<%_ if (hasTS) { _%>
2+
import { expect } from 'chai'
3+
import { shallowMount } from '@vue/test-utils'
4+
import HelloWorld from '@/components/HelloWorld.vue'
5+
6+
describe('HelloWorld.vue', () => {
7+
it('renders props.msg when passed', () => {
8+
const msg = 'new message'
9+
const wrapper = shallowMount(HelloWorld, {
10+
propsData: { msg }
11+
})
12+
expect(wrapper.text()).to.include(msg)
13+
})
14+
})
15+
<%_ } _%>

0 commit comments

Comments
 (0)