Skip to content

Commit

Permalink
feat: WIP jest plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 15, 2018
1 parent 52dad9d commit bb5d968
Show file tree
Hide file tree
Showing 14 changed files with 459 additions and 737 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
8 changes: 4 additions & 4 deletions packages/@vue/babel-preset-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
}])

Expand Down
10 changes: 5 additions & 5 deletions packages/@vue/babel-preset-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/@vue/cli-plugin-babel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
43 changes: 43 additions & 0 deletions packages/@vue/cli-plugin-unit-jest/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
25 changes: 23 additions & 2 deletions packages/@vue/cli-plugin-unit-jest/index.js
Original file line number Diff line number Diff line change
@@ -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()
}
})
})
})
}
26 changes: 0 additions & 26 deletions packages/@vue/cli-plugin-unit-jest/jest.config.js

This file was deleted.

11 changes: 6 additions & 5 deletions packages/@vue/cli-plugin-unit-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion packages/@vue/cli-plugin-unit-mocha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions packages/@vue/cli-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/@vue/cli/lib/promptModules/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ module.exports = cli => {
short: 'Mocha'
},
{
name: 'Jest',
name: 'Jest (WIP)',
value: 'jest',
short: 'Jest'
short: 'Jest',
disabled: true
}
]
})
Expand Down
9 changes: 7 additions & 2 deletions scripts/syncDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit bb5d968

Please sign in to comment.