mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-09-29 16:57:06 +08:00
feat: WIP jest plugin
This commit is contained in:
@@ -47,7 +47,7 @@ yarn test -g <filenameRegex>
|
||||
|
||||
You can also pass `--watch` to run tests in watch mode.
|
||||
|
||||
Note that `jest -o` (running tests related to modified files) isn't always accurate because some tests spawn child processes.
|
||||
Note that `jest --onlyChanged` isn't always accurate because some tests spawn child processes.
|
||||
|
||||
### Plugin Development
|
||||
|
||||
|
||||
+2
-1
@@ -51,6 +51,7 @@
|
||||
"lint-staged": "^6.0.0",
|
||||
"memfs": "^2.6.0",
|
||||
"puppeteer": "^0.13.0",
|
||||
"yorkie": "^1.0.2"
|
||||
"yorkie": "^1.0.2",
|
||||
"babel-core": "^7.0.0-0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ module.exports = (context, options = {}) => {
|
||||
// JSX
|
||||
if (options.jsx !== false) {
|
||||
plugins.push(
|
||||
require('@babel/plugin-syntax-jsx').default,
|
||||
require('@babel/plugin-syntax-jsx'),
|
||||
require('babel-plugin-transform-vue-jsx'),
|
||||
require('babel-plugin-jsx-event-modifiers'),
|
||||
require('babel-plugin-jsx-v-model')
|
||||
@@ -26,15 +26,15 @@ module.exports = (context, options = {}) => {
|
||||
}
|
||||
// cli-plugin-jest sets this to true because Jest runs without bundling
|
||||
if (process.env.VUE_CLI_BABEL_TRANSPILE_MODULES) {
|
||||
envOptions.modules = true
|
||||
envOptions.modules = 'commonjs'
|
||||
}
|
||||
|
||||
// pass options along to babel-preset-env
|
||||
presets.push([require('@babel/preset-env').default, envOptions])
|
||||
presets.push([require('@babel/preset-env'), envOptions])
|
||||
|
||||
// stage 2. This includes some important transforms, e.g. dynamic import
|
||||
// and rest object spread.
|
||||
presets.push([require('@babel/preset-stage-2').default, {
|
||||
presets.push([require('@babel/preset-stage-2'), {
|
||||
useBuiltIns: true
|
||||
}])
|
||||
|
||||
|
||||
@@ -21,11 +21,11 @@
|
||||
},
|
||||
"homepage": "https://github.com/vuejs/vue-cli/packages/@vue/babel-preset-app#readme",
|
||||
"dependencies": {
|
||||
"@babel/plugin-syntax-jsx": "7 || ^7.0.0-beta || ^7.0.0-rc",
|
||||
"@babel/plugin-transform-runtime": "7 || ^7.0.0-beta || ^7.0.0-rc",
|
||||
"@babel/preset-env": "7 || ^7.0.0-beta || ^7.0.0-rc",
|
||||
"@babel/preset-stage-2": "7 || ^7.0.0-beta || ^7.0.0-rc",
|
||||
"@babel/runtime": "7 || ^7.0.0-beta || ^7.0.0-rc",
|
||||
"@babel/plugin-syntax-jsx": "^7.0.0-0",
|
||||
"@babel/plugin-transform-runtime": "^7.0.0-0",
|
||||
"@babel/preset-env": "^7.0.0-0",
|
||||
"@babel/preset-stage-2": "^7.0.0-0",
|
||||
"@babel/runtime": "^7.0.0-0",
|
||||
"babel-helper-vue-jsx-merge-props": "^2.0.3",
|
||||
"babel-plugin-jsx-event-modifiers": "^2.0.5",
|
||||
"babel-plugin-jsx-v-model": "^2.0.3",
|
||||
|
||||
@@ -19,8 +19,8 @@
|
||||
},
|
||||
"homepage": "https://github.com/vuejs/vue-cli/packages/@vue/cli-plugin-babel#readme",
|
||||
"dependencies": {
|
||||
"@babel/core": "7 || ^7.0.0-beta || ^7.0.0-rc",
|
||||
"babel-loader": "8 || ^8.0.0-beta || ^8.0.0-rc"
|
||||
"@babel/core": "^7.0.0-0",
|
||||
"babel-loader": "^7.1.2"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
|
||||
@@ -9,6 +9,49 @@ module.exports = api => {
|
||||
}
|
||||
})
|
||||
|
||||
const jestConfig = {
|
||||
'moduleFileExtensions': [
|
||||
'json',
|
||||
// tell Jest to handle *.vue files
|
||||
'vue'
|
||||
],
|
||||
'transform': {
|
||||
// process *.vue files with vue-jest
|
||||
'^.+\\.vue$': 'vue-jest'
|
||||
},
|
||||
// support the same @ -> src alias mapping in source code
|
||||
'moduleNameMapper': {
|
||||
'^@/(.*)$': '<rootDir>/src/$1'
|
||||
},
|
||||
// serializer for snapshots
|
||||
'snapshotSerializers': [
|
||||
'jest-serializer-vue'
|
||||
],
|
||||
'mapCoverage': true
|
||||
}
|
||||
|
||||
if (!api.hasPlugin('typescript')) {
|
||||
jestConfig.moduleFileExtensions.unshift('js', 'jsx')
|
||||
jestConfig.transform['^.+\\.jsx?$'] = 'babel-jest'
|
||||
api.extendPackage({
|
||||
devDependencies: {
|
||||
'babel-jest': '^22.0.4',
|
||||
// this is for now necessary to force babel-jest and vue-jest to use babel 7
|
||||
'babel-core': '^7.0.0-0'
|
||||
}
|
||||
})
|
||||
} else {
|
||||
jestConfig.moduleFileExtensions.unshift('ts', 'tsx')
|
||||
jestConfig.transform['^.+\\.tsx?$'] = 'ts-jest'
|
||||
api.extendPackage({
|
||||
devDependencies: {
|
||||
'ts-jest': '^22.0.1'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
api.extendPackage({ jest: jestConfig })
|
||||
|
||||
if (api.hasPlugin('eslint')) {
|
||||
api.render(files => {
|
||||
files['test/unit/.eslintrc'] = JSON.stringify({
|
||||
|
||||
@@ -1,12 +1,33 @@
|
||||
module.exports = api => {
|
||||
api.registerCommand('test', {
|
||||
description: 'run unit tests with jest'
|
||||
}, args => {
|
||||
description: 'run unit tests with jest',
|
||||
usage: 'vue-cli-service test [options] <regexForTestFiles>',
|
||||
options: {
|
||||
'--watch': 'run tests in watch mode'
|
||||
},
|
||||
details:
|
||||
`All jest command line options are supported.\n` +
|
||||
`See https://facebook.github.io/jest/docs/en/cli.html for more details.`
|
||||
}, (args, rawArgv) => {
|
||||
api.setMode('test')
|
||||
// for @vue/babel-preset-app
|
||||
process.env.VUE_CLI_BABEL_TARGET_NODE = true
|
||||
process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = true
|
||||
|
||||
// TODO execa jest w/ --config jest.config.js
|
||||
const execa = require('execa')
|
||||
const jestBinPath = require.resolve('jest/bin/jest')
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = execa(jestBinPath, rawArgv, { stdio: 'inherit' })
|
||||
child.on('error', reject)
|
||||
child.on('exit', code => {
|
||||
if (code !== 0) {
|
||||
reject(`jest exited with code ${code}.`)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
// TODO check if jest auto merges options
|
||||
// if not, merge manually
|
||||
|
||||
module.exports = {
|
||||
'moduleFileExtensions': [
|
||||
'js',
|
||||
'json',
|
||||
// tell Jest to handle *.vue files
|
||||
'vue'
|
||||
],
|
||||
'transform': {
|
||||
// process js with babel-jest
|
||||
'^.+\\.js$': require.resolve('babel-jest'),
|
||||
// process *.vue files with vue-jest
|
||||
'.*\\.(vue)$': require.resolve('vue-jest')
|
||||
},
|
||||
// support the same @ -> src alias mapping in source code
|
||||
'moduleNameMapper': {
|
||||
'^@/(.*)$': '<rootDir>/src/$1'
|
||||
},
|
||||
// serializer for snapshots
|
||||
'snapshotSerializers': [
|
||||
require.resolve('jest-serializer-vue')
|
||||
],
|
||||
'mapCoverage': true
|
||||
}
|
||||
@@ -22,13 +22,14 @@
|
||||
"access": "public"
|
||||
},
|
||||
"dependencies": {
|
||||
"babel-jest": "^22.0.4",
|
||||
"execa": "^0.8.0",
|
||||
"jest": "^22.0.5",
|
||||
"execa": "^0.9.0",
|
||||
"jest": "^22.1.1",
|
||||
"jest-serializer-vue": "^0.3.0",
|
||||
"vue-jest": "^1.4.0"
|
||||
"vue-jest": "yyx990803/vue-jest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vue/test-utils": "^1.0.0-beta.10"
|
||||
"@vue/test-utils": "^1.0.0-beta.10",
|
||||
"babel-jest": "^22.0.4",
|
||||
"ts-jest": "^22.0.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/vuejs/vue-cli/packages/@vue/cli-plugin-unit-mocha#readme",
|
||||
"dependencies": {
|
||||
"execa": "^0.8.0",
|
||||
"execa": "^0.9.0",
|
||||
"jsdom": "^11.5.1",
|
||||
"jsdom-global": "^3.0.2",
|
||||
"mocha": "^4.1.0",
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
"@vue/cli-overlay": "^3.0.0-alpha.1",
|
||||
"@vue/cli-shared-utils": "^3.0.0-alpha.1",
|
||||
"address": "^1.0.3",
|
||||
"autoprefixer": "^7.2.4",
|
||||
"autoprefixer": "^7.2.5",
|
||||
"case-sensitive-paths-webpack-plugin": "^2.1.1",
|
||||
"chalk": "^2.3.0",
|
||||
"copy-webpack-plugin": "^4.3.1",
|
||||
@@ -38,9 +38,9 @@
|
||||
"get-value": "^2.0.6",
|
||||
"html-webpack-plugin": "^2.30.1",
|
||||
"javascript-stringify": "^1.6.0",
|
||||
"launch-editor-middleware": "^2.1.0",
|
||||
"launch-editor-middleware": "^2.2.0",
|
||||
"minimist": "^1.2.0",
|
||||
"opn": "^5.1.0",
|
||||
"opn": "^5.2.0",
|
||||
"optimize-css-assets-webpack-plugin": "^3.2.0",
|
||||
"ora": "^1.3.0",
|
||||
"portfinder": "^1.0.13",
|
||||
@@ -58,7 +58,7 @@
|
||||
"vue-template-compiler": "^2.5.13",
|
||||
"webpack": "^3.10.0",
|
||||
"webpack-chain": "^4.5.0",
|
||||
"webpack-dev-server": "^2.10.1",
|
||||
"webpack-dev-server": "^2.11.0",
|
||||
"webpack-merge": "^4.1.1",
|
||||
"yorkie": "^1.0.3"
|
||||
},
|
||||
|
||||
@@ -18,9 +18,10 @@ module.exports = cli => {
|
||||
short: 'Mocha'
|
||||
},
|
||||
{
|
||||
name: 'Jest',
|
||||
name: 'Jest (WIP)',
|
||||
value: 'jest',
|
||||
short: 'Jest'
|
||||
short: 'Jest',
|
||||
disabled: true
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
+7
-2
@@ -22,7 +22,12 @@ const getRemoteVersion = async (pkg) => {
|
||||
if (versionCache[pkg]) {
|
||||
return versionCache[pkg]
|
||||
}
|
||||
const res = await axios.get(`http://registry.npmjs.org/${pkg}/latest`)
|
||||
let res
|
||||
try {
|
||||
res = await axios.get(`http://registry.npmjs.org/${pkg}/latest`)
|
||||
} catch (e) {
|
||||
return
|
||||
}
|
||||
const version = res.data.version
|
||||
versionCache[pkg] = version
|
||||
return version
|
||||
@@ -82,7 +87,7 @@ const flushWrite = () => {
|
||||
}
|
||||
local = local.replace(/^\^/, '')
|
||||
const remote = await getRemoteVersion(dep)
|
||||
if (checkUpdate(dep, filePath, local, remote)) {
|
||||
if (remote && checkUpdate(dep, filePath, local, remote)) {
|
||||
deps[dep] = `^${remote}`
|
||||
updatedDeps.add(dep)
|
||||
isUpdated = true
|
||||
|
||||
Reference in New Issue
Block a user