Skip to content

Commit

Permalink
feat: update jest to v27
Browse files Browse the repository at this point in the history
Fixes vuejs#6602

This commit updates the generated project to use:
- jest v27, as an explicit dependency
- ts-jest and babel-jest v27
- @vue/vue3-jest as a transformer for Vue 3 projects
- @vue/vue2-jest as a transformer for Vue 2 projects

The default Babel transformer has been updated to work with Jest v27 as well.
  • Loading branch information
cexbrayat committed Sep 9, 2021
1 parent 662153b commit e8cf86e
Show file tree
Hide file tree
Showing 12 changed files with 1,437 additions and 604 deletions.
107 changes: 107 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions docs/migrations/migrate-from-v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ Please consider switching to ESLint. You can check out [`tslint-to-eslint-config

### Unit-Jest Plugin

* For Vue 2 projects, `vue-jest` is now required as a peer dependency, please install `vue-jest@^4.0.1` as a dev dependency to the project.
* For TypeScript projects, `ts-jest` is now required as a peer dependency. Users need to install `ts-jest@26` manually to the project root.
* The underlying `jest`-related packages are upgraded from v24 to v26. For most users the transition would be seamless. See their corresponding changelogs for more detail:
* [jest, babel-jest](https://github.com/facebook/jest/blob/v26.6.3/CHANGELOG.md)
* [ts-jest](https://github.com/kulshekhar/ts-jest/blob/v26.4.4/CHANGELOG.md)
* For Vue 2 projects, `@vue/vue2-jest` is now required as a peer dependency, please install `@vue/vue2-jest` as a dev dependency to the project.
* For TypeScript projects, `ts-jest` is now required as a peer dependency. Users need to install `ts-jest@27` manually to the project root.
* The underlying `jest`-related packages are upgraded from v24 to v27. For most users the transition would be seamless. See their corresponding changelogs for more detail:
* [jest, babel-jest](https://github.com/facebook/jest/blob/v27.1.0/CHANGELOG.md)
* [ts-jest](https://github.com/kulshekhar/ts-jest/blob/v27.0.0/CHANGELOG.md)

### Unit-Mocha Plugin

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@vuepress/plugin-pwa": "^1.8.1",
"@vuepress/theme-vue": "^1.8.1",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "^26.6.3",
"babel-jest": "^27.0.6",
"chromedriver": "^92.0.0",
"debug": "^4.1.0",
"eslint": "^7.32.0",
Expand All @@ -57,7 +57,7 @@
"graphql": "^15.5.0",
"http-server": "^0.12.3",
"inquirer": "^8.0.0",
"jest": "^26.6.3",
"jest": "^27.0.6",
"lerna": "^4.0.0",
"lerna-changelog": "^2.1.0",
"lint-staged": "^11.1.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-plugin-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
"devDependencies": {
"@types/chai": "^4.2.15",
"@types/jest": "^26.0.20",
"@types/jest": "^27.0.1",
"@types/mocha": "^8.2.1",
"jscodeshift": "^0.13.0",
"typescript": "~4.1.5",
Expand Down
19 changes: 19 additions & 0 deletions packages/@vue/cli-plugin-unit-jest/__tests__/jestPlugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,24 @@ test('should work with Vue 3', async () => {
})
const pkg = JSON.parse(await project.read('package.json'))
expect(pkg.devDependencies['@vue/test-utils']).toMatch('^2')
expect(pkg.devDependencies['@vue/vue3-jest']).toMatch('^27')
expect(pkg.devDependencies.jest).toMatch('^27')
expect(pkg.devDependencies['babel-jest']).toMatch('^27')
await project.run(`vue-cli-service test:unit`)
})

test('should work with Vue 3 and TS', async () => {
const project = await createOutside('unit-jest-vue-3-ts', {
vueVersion: '3',
plugins: {
'@vue/cli-plugin-typescript': {},
'@vue/cli-plugin-unit-jest': {}
}
})
const pkg = JSON.parse(await project.read('package.json'))
expect(pkg.devDependencies['@vue/test-utils']).toMatch('^2')
expect(pkg.devDependencies['@vue/vue3-jest']).toMatch('^27')
expect(pkg.devDependencies.jest).toMatch('^27')
expect(pkg.devDependencies['ts-jest']).toMatch('^27')
await project.run(`vue-cli-service test:unit`)
})
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,24 @@ test('should work with Vue 3', async () => {
})
const pkg = JSON.parse(await project.read('package.json'))
expect(pkg.devDependencies['@vue/test-utils']).toMatch('^2')
expect(pkg.devDependencies['@vue/vue3-jest']).toMatch('^27')
expect(pkg.devDependencies.jest).toMatch('^27')
expect(pkg.devDependencies['babel-jest']).toMatch('^27')
await project.run(`vue-cli-service test:unit`)
})

test('should work with Vue 3 and TS', async () => {
const project = await createOutside('unit-jest-vue-3', {
vueVersion: '3',
plugins: {
'@vue/cli-plugin-typescript': {},
'@vue/cli-plugin-unit-jest': {}
}
})
const pkg = JSON.parse(await project.read('package.json'))
expect(pkg.devDependencies['@vue/test-utils']).toMatch('^2')
expect(pkg.devDependencies['@vue/vue3-jest']).toMatch('^27')
expect(pkg.devDependencies.jest).toMatch('^27')
expect(pkg.devDependencies['ts-jest']).toMatch('^27')
await project.run(`vue-cli-service test:unit`)
})
10 changes: 6 additions & 4 deletions packages/@vue/cli-plugin-unit-jest/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ module.exports = (api, options, rootOptions, invoking) => {
'test:unit': 'vue-cli-service test:unit'
},
devDependencies: {
'jest': '^26.6.3',
'vue-jest': isVue3 ? '^5.0.0-0' : '^4.0.1',
'babel-jest': '^27.0.6',
'jest': '^27.0.5',
'@vue/vue2-jest': isVue3 ? undefined : '^27.0.0-alpha.2',
'@vue/vue3-jest': isVue3 ? '^27.0.0-alpha.1' : undefined,
'@vue/test-utils': isVue3 ? '^2.0.0-0' : '^1.1.3'
},
jest: {
Expand Down Expand Up @@ -58,8 +60,8 @@ const applyTS = (module.exports.applyTS = (api, invoking) => {
: '@vue/cli-plugin-unit-jest/presets/typescript'
},
devDependencies: {
'@types/jest': '^26.0.20',
'ts-jest': '^26.5.3'
'@types/jest': '^27.0.1',
'ts-jest': '^27.0.4'
}
})

Expand Down
7 changes: 3 additions & 4 deletions packages/@vue/cli-plugin-unit-jest/migrator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module.exports = (api) => {
api.extendPackage(pkg => {
const newDevDeps = {
'jest': '^26.6.3'
'jest': '^27.1.0'
}

const allDeps = {
Expand All @@ -13,12 +13,11 @@ module.exports = (api) => {

if (!allDeps['vue-jest']) {
// Likely a Vue 2 project, and uses the builtin preset.
// Because we used to add `vue-jest` v5 to dev deps for Vue 3 projects.
newDevDeps['vue-jest'] = '^4.0.1'
newDevDeps['@vue/vue3-jest'] = '^27.0.0.alpha.1'
}

if (allDeps['@vue/cli-plugin-typescript'] && !allDeps['ts-jest']) {
newDevDeps['ts-jest'] = '^26.5.3'
newDevDeps['ts-jest'] = '^27.0.4'
}

const toMerge = { devDependencies: newDevDeps }
Expand Down
26 changes: 17 additions & 9 deletions packages/@vue/cli-plugin-unit-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,36 @@
},
"dependencies": {
"@babel/core": "^7.12.16",
"@babel/plugin-transform-modules-commonjs": "^7.12.13",
"@types/jest": "^26.0.20",
"@babel/plugin-transform-modules-commonjs": "^7.15.0",
"@types/jest": "^27.0.1",
"@vue/cli-shared-utils": "^5.0.0-beta.3",
"babel-jest": "^26.6.3",
"babel-jest": "^27.1.0",
"deepmerge": "^4.2.2",
"jest": "^26.6.3",
"jest": "^27.1.0",
"jest-serializer-vue": "^2.0.2",
"jest-transform-stub": "^2.0.0",
"jest-watch-typeahead": "^0.6.1"
},
"devDependencies": {
"@vue/test-utils": "^1.1.3",
"ts-jest": "^26.5.3",
"vue-jest": "^4.0.1"
"@vue/vue2-jest": "^27.0.0-alpha.2",
"@vue/vue3-jest": "^27.0.0-alpha.1",
"ts-jest": "^27.0.4"
},
"peerDependencies": {
"@vue/cli-service": "^3.0.0 || ^4.0.0 || ^5.0.0-0",
"jest": "^26.3.3",
"ts-jest": "^26.5.3",
"vue-jest": "^4.0.1 || ^5.0.0-0"
"@vue/vue2-jest": "^27.0.0-alpha.2",
"@vue/vue3-jest": "^27.0.0-alpha.1",
"jest": "^27.1.0",
"ts-jest": "^27.0.4"
},
"peerDependenciesMeta": {
"@vue/vue2-jest": {
"optional": true
},
"@vue/vue3-jest": {
"optional": true
},
"ts-jest": {
"optional": true
}
Expand Down
15 changes: 13 additions & 2 deletions packages/@vue/cli-plugin-unit-jest/presets/default/jest-preset.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
// eslint-disable-next-line node/no-extraneous-require
const semver = require('semver')

let vueVersion = 2
try {
// eslint-disable-next-line node/no-extraneous-require
const Vue = require('vue')
vueVersion = semver.major(Vue.version)
} catch (e) {}

let vueJest = null
try {
vueJest = require.resolve('vue-jest')
vueJest = require.resolve(`@vue/vue${vueVersion}-jest`)
} catch (e) {
throw new Error('Cannot resolve "vue-jest" module. Please make sure you have installed "vue-jest" as a dev dependency.')
throw new Error(`Cannot resolve "@vue/vue${vueVersion}-jest" module. Please make sure you have installed "@vue/vue${vueVersion}-jest" as a dev dependency.`)
}

module.exports = {
testEnvironment: 'jsdom',
moduleFileExtensions: [
'js',
'jsx',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const babelJest = require('babel-jest')
const babelJest = require('babel-jest').default

module.exports = babelJest.createTransformer({
plugins: ['@babel/plugin-transform-modules-commonjs'],
Expand Down

0 comments on commit e8cf86e

Please sign in to comment.