UNPKG

2.07 kBJavaScriptView Raw
1module.exports = (api, options, rootOptions, invoking) => {
2 const isVue3 = rootOptions && rootOptions.vueVersion === '3'
3
4 api.render('./template', {
5 isVue3,
6 hasTS: api.hasPlugin('typescript'),
7 hasRouter: api.hasPlugin('router')
8 })
9
10 api.extendPackage({
11 scripts: {
12 'test:unit': 'vue-cli-service test:unit'
13 },
14 devDependencies: {
15 'babel-jest': '^27.0.6',
16 'jest': '^27.0.5',
17 '@vue/vue2-jest': isVue3 ? undefined : '^27.0.0-alpha.2',
18 '@vue/vue3-jest': isVue3 ? '^27.0.0-alpha.1' : undefined,
19 '@vue/test-utils': isVue3 ? '^2.0.0-0' : '^1.1.3'
20 },
21 jest: {
22 preset: api.hasPlugin('babel')
23 ? '@vue/cli-plugin-unit-jest'
24 : '@vue/cli-plugin-unit-jest/presets/no-babel'
25 }
26 })
27
28 if (api.hasPlugin('eslint')) {
29 applyESLint(api)
30 }
31
32 if (api.hasPlugin('typescript')) {
33 applyTS(api, invoking)
34 }
35}
36
37const applyESLint = module.exports.applyESLint = api => {
38 api.extendPackage({
39 eslintConfig: {
40 overrides: [
41 {
42 files: [
43 '**/__tests__/*.{j,t}s?(x)',
44 '**/tests/unit/**/*.spec.{j,t}s?(x)'
45 ],
46 env: {
47 jest: true
48 }
49 }
50 ]
51 }
52 })
53}
54
55const applyTS = (module.exports.applyTS = (api, invoking) => {
56 api.extendPackage({
57 jest: {
58 preset: api.hasPlugin('babel')
59 ? '@vue/cli-plugin-unit-jest/presets/typescript-and-babel'
60 : '@vue/cli-plugin-unit-jest/presets/typescript'
61 },
62 devDependencies: {
63 '@types/jest': '^27.0.1',
64 'ts-jest': '^27.0.4'
65 }
66 })
67
68 if (invoking) {
69 // inject jest type to tsconfig.json
70 api.render(files => {
71 const tsconfig = files['tsconfig.json']
72 if (tsconfig) {
73 const parsed = JSON.parse(tsconfig)
74 if (
75 parsed.compilerOptions.types &&
76 !parsed.compilerOptions.types.includes('jest')
77 ) {
78 parsed.compilerOptions.types.push('jest')
79 }
80 files['tsconfig.json'] = JSON.stringify(parsed, null, 2) + '\n'
81 }
82 })
83 }
84})