Skip to content

Commit 2e1e92b

Browse files
authored
refactor: move jest configs into presets (vuejs#4597)
* refactor: move jest configs into a preset This could significantly reduce the size of default boilerplate, and gives us more control on the internal config details, thus potentially making future upgrades easier. * fix: add back applyTS
1 parent cc06091 commit 2e1e92b

File tree

9 files changed

+125
-122
lines changed

9 files changed

+125
-122
lines changed

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,10 @@ test('base', async () => {
2323
expect(pkg.scripts['test:unit']).toBe('vue-cli-service test:unit')
2424
expect(pkg.devDependencies).toHaveProperty('@vue/test-utils')
2525

26-
// should inject babel-jest
27-
expect(pkg.devDependencies).toHaveProperty('babel-jest')
28-
expect(pkg.devDependencies).toHaveProperty('@babel/core')
29-
// eslint
30-
expect(files['tests/unit/.eslintrc.js']).toMatch('jest: true')
31-
3226
const spec = files['tests/unit/example.spec.js']
3327
expect(spec).toMatch(`expect(wrapper.text()).toMatch(msg)`)
3428
})
3529

36-
test('without babel/eslint', async () => {
37-
const { pkg, files } = await generateWithPlugin([
38-
{
39-
id: 'unit-jest',
40-
apply: require('../generator'),
41-
options: {}
42-
}
43-
])
44-
45-
expect(pkg.devDependencies).not.toHaveProperty('babel-jest')
46-
expect(files['tests/unit/.eslintrc.js']).toBeUndefined()
47-
})
48-
4930
test('with TS', async () => {
5031
const { files } = await generateWithPlugin([
5132
{

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,13 @@ test('should work with tsx', async () => {
111111

112112
await run(`vue-cli-service test:unit`)
113113
})
114+
115+
test('should correctly configured eslint', async () => {
116+
const project = await create('unit-jest-eslint', {
117+
plugins: {
118+
'@vue/cli-plugin-eslint': {},
119+
'@vue/cli-plugin-unit-jest': {}
120+
}
121+
})
122+
await project.run(`vue-cli-service lint`)
123+
})

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

Lines changed: 17 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -11,78 +11,21 @@ module.exports = (api, _, __, invoking) => {
1111
'@vue/test-utils': '1.0.0-beta.29'
1212
},
1313
jest: {
14-
moduleFileExtensions: [
15-
'js',
16-
'jsx',
17-
'json',
18-
// tell Jest to handle *.vue files
19-
'vue'
20-
],
21-
transform: {
22-
// process *.vue files with vue-jest
23-
'^.+\\.vue$': 'vue-jest',
24-
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$':
25-
'jest-transform-stub'
26-
},
27-
'transformIgnorePatterns': ['/node_modules/'],
28-
// support the same @ -> src alias mapping in source code
29-
moduleNameMapper: {
30-
'^@/(.*)$': '<rootDir>/src/$1'
31-
},
32-
// serializer for snapshots
33-
snapshotSerializers: ['jest-serializer-vue'],
34-
testMatch: [
35-
'**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'
36-
],
37-
// https://github.com/facebook/jest/issues/6766
38-
testURL: 'http://localhost/',
39-
watchPlugins: [
40-
'jest-watch-typeahead/filename',
41-
'jest-watch-typeahead/testname'
42-
]
14+
preset: api.hasPlugin('babel')
15+
? '@vue/cli-plugin-unit-jest/preset'
16+
: '@vue/cli-plugin-unit-jest/preset/no-babel'
4317
}
4418
})
4519

46-
if (!api.hasPlugin('typescript')) {
47-
api.extendPackage({
48-
jest: {
49-
transform: {
50-
'^.+\\.jsx?$': 'babel-jest'
51-
}
52-
}
53-
})
54-
if (api.hasPlugin('babel')) {
55-
api.extendPackage({
56-
devDependencies: {
57-
'babel-jest': '^24.8.0',
58-
'@babel/core': '^7.4.5'
59-
}
60-
})
61-
} else {
62-
// ts-jest still does not support babel.config.js
63-
// https://github.com/kulshekhar/ts-jest/issues/933
64-
api.render(files => {
65-
files['.babelrc'] = JSON.stringify(
66-
{
67-
plugins: ['@babel/plugin-transform-modules-commonjs']
68-
},
69-
null,
70-
2
71-
)
72-
})
73-
}
74-
} else {
75-
applyTS(api, invoking)
76-
}
77-
7820
if (api.hasPlugin('eslint')) {
79-
applyESLint(api)
80-
8121
api.extendPackage({
8222
eslintConfig: {
8323
overrides: [
8424
{
85-
files: ['**/__tests__/*.{j,t}s?(x)'],
25+
files: [
26+
'**/__tests__/*.{j,t}s?(x)',
27+
'**/tests/unit/**/*.spec.{j,t}s?(x)'
28+
],
8629
env: {
8730
jest: true
8831
}
@@ -91,40 +34,26 @@ module.exports = (api, _, __, invoking) => {
9134
}
9235
})
9336
}
37+
38+
if (api.hasPlugin('typescript')) {
39+
applyTS(api, invoking)
40+
}
9441
}
9542

9643
const applyTS = (module.exports.applyTS = (api, invoking) => {
9744
api.extendPackage({
9845
jest: {
99-
moduleFileExtensions: ['ts', 'tsx'],
100-
transform: {
101-
'^.+\\.tsx?$': 'ts-jest'
102-
}
46+
preset: api.hasPlugin('babel')
47+
? '@vue/cli-plugin-unit-jest/preset/typescript-and-babel'
48+
: '@vue/cli-plugin-unit-jest/preset/typescript'
10349
},
10450
devDependencies: {
105-
'@types/jest': '^24.0.11',
106-
'ts-jest': '^24.0.2'
51+
'@types/jest': '^24.0.11'
10752
}
10853
})
109-
if (api.hasPlugin('babel')) {
110-
api.extendPackage({
111-
jest: {
112-
globals: {
113-
'ts-jest': {
114-
// we need babel to transpile JSX
115-
babelConfig: true
116-
}
117-
}
118-
},
119-
devDependencies: {
120-
// this is for now necessary to force ts-jest and vue-jest to use babel 7
121-
'@babel/core': '^7.4.5',
122-
'babel-core': '7.0.0-bridge.0'
123-
}
124-
})
125-
}
126-
// inject jest type to tsconfig.json
54+
12755
if (invoking) {
56+
// inject jest type to tsconfig.json
12857
api.render(files => {
12958
const tsconfig = files['tsconfig.json']
13059
if (tsconfig) {
@@ -140,11 +69,3 @@ const applyTS = (module.exports.applyTS = (api, invoking) => {
14069
})
14170
}
14271
})
143-
144-
const applyESLint = (module.exports.applyESLint = api => {
145-
api.render(files => {
146-
files['tests/unit/.eslintrc.js'] = api.genJSConfig({
147-
env: { jest: true }
148-
})
149-
})
150-
})

packages/@vue/cli-plugin-unit-jest/package.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,23 @@
2323
"access": "public"
2424
},
2525
"dependencies": {
26-
"@babel/core": "^7.4.5",
26+
"@babel/core": "^7.0.0",
2727
"@babel/plugin-transform-modules-commonjs": "^7.4.4",
28+
"@types/jest": "^24.0.18",
2829
"@vue/cli-shared-utils": "^4.0.0-rc.3",
29-
"babel-core": "7.0.0-bridge.0",
30-
"babel-jest": "^24.8.0",
30+
"babel-core": "^7.0.0-bridge.0",
31+
"babel-jest": "^24.9.0",
32+
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
33+
"deepmerge": "^4.0.0",
3134
"jest": "^24.8.0",
3235
"jest-serializer-vue": "^2.0.2",
3336
"jest-transform-stub": "^2.0.0",
34-
"jest-watch-typeahead": "^0.3.1",
35-
"vue-jest": "^3.0.4"
37+
"jest-watch-typeahead": "^0.4.0",
38+
"ts-jest": "^24.1.0",
39+
"vue-jest": "^3.0.5"
3640
},
3741
"devDependencies": {
38-
"@vue/test-utils": "1.0.0-beta.29",
39-
"ts-jest": "^24.0.2"
42+
"@vue/test-utils": "1.0.0-beta.29"
4043
},
4144
"peerDependencies": {
4245
"@vue/cli-service": "^3.0.0 || ^4.0.0-0"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module.exports = {
2+
moduleFileExtensions: [
3+
'js',
4+
'jsx',
5+
'json',
6+
// tell Jest to handle *.vue files
7+
'vue'
8+
],
9+
transform: {
10+
// process *.vue files with vue-jest
11+
'^.+\\.vue$': require.resolve('vue-jest'),
12+
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$':
13+
require.resolve('jest-transform-stub'),
14+
'^.+\\.jsx?$': require.resolve('babel-jest')
15+
},
16+
transformIgnorePatterns: ['/node_modules/'],
17+
// support the same @ -> src alias mapping in source code
18+
moduleNameMapper: {
19+
'^@/(.*)$': '<rootDir>/src/$1'
20+
},
21+
// serializer for snapshots
22+
snapshotSerializers: [
23+
'jest-serializer-vue'
24+
],
25+
testMatch: [
26+
'**/tests/unit/**/*.spec.[jt]s?(x)',
27+
'**/__tests__/*.[jt]s?(x)'
28+
],
29+
// https://github.com/facebook/jest/issues/6766
30+
testURL: 'http://localhost/',
31+
watchPlugins: [
32+
require.resolve('jest-watch-typeahead/filename'),
33+
require.resolve('jest-watch-typeahead/testname')
34+
]
35+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const babelJest = require('babel-jest')
2+
3+
module.exports = babelJest.createTransformer({
4+
plugins: ['@babel/plugin-transform-modules-commonjs'],
5+
babelrc: false,
6+
configFile: false
7+
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const deepmerge = require('deepmerge')
2+
const defaultPreset = require('../jest-preset')
3+
4+
// If no default babel preset exists,
5+
// we need to use a customized babel transformer to deal with es modules
6+
7+
module.exports = deepmerge(
8+
defaultPreset,
9+
{
10+
transform: {
11+
'^.+\\.jsx?$': require.resolve('./esmoduleTransformer')
12+
},
13+
globals: {
14+
'vue-jest': {
15+
babelConfig: {
16+
plugins: [require('babel-plugin-transform-es2015-modules-commonjs')]
17+
}
18+
}
19+
}
20+
}
21+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const deepmerge = require('deepmerge')
2+
const defaultTsPreset = require('../typescript/jest-preset')
3+
4+
module.exports = deepmerge(
5+
defaultTsPreset,
6+
{
7+
globals: {
8+
'ts-jest': {
9+
babelConfig: true
10+
}
11+
}
12+
}
13+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const deepmerge = require('deepmerge')
2+
const defaultPreset = require('../jest-preset')
3+
4+
module.exports = deepmerge(
5+
defaultPreset,
6+
{
7+
moduleFileExtensions: ['ts', 'tsx'],
8+
transform: {
9+
'^.+\\.tsx?$': require.resolve('ts-jest')
10+
}
11+
}
12+
)

0 commit comments

Comments
 (0)