test: test for vue invoke

This commit is contained in:
Evan You
2018-01-14 23:27:18 -05:00
parent 06af371c04
commit 563636c33f
3 changed files with 58 additions and 8 deletions
@@ -0,0 +1,30 @@
const create = require('@vue/cli-test-utils/createTestProject')
test('invoke single generator', async () => {
const project = await create('invoke', {
plugins: {
'@vue/cli-plugin-babel': {}
}
})
// mock install
const pkg = JSON.parse(await project.read('package.json'))
pkg.devDependencies['@vue/cli-plugin-eslint'] = '*'
await project.write('package.json', JSON.stringify(pkg, null, 2))
const cliBinPath = require.resolve('../bin/vue')
await project.run(`${cliBinPath} invoke eslint --config airbnb --lintOn save,commit`)
const updatedPkg = JSON.parse(await project.read('package.json'))
expect(updatedPkg.scripts.lint).toBe('vue-cli-service lint')
expect(updatedPkg.devDependencies).toHaveProperty('eslint-plugin-vue')
expect(updatedPkg.devDependencies).toHaveProperty('lint-staged')
expect(updatedPkg.eslintConfig).toEqual({
extends: ['plugin:vue/essential', '@vue/airbnb']
})
expect(updatedPkg.gitHooks).toEqual({
'pre-commit': 'lint-staged'
})
const lintedMain = await project.read('src/main.js')
expect(lintedMain).toMatch(';') // should've been linted in post-generate hook
})
+6 -2
View File
@@ -18,8 +18,12 @@ if (!semver.satisfies(process.version, requiredVersion)) {
}
// enter debug mode when creating test repo
if (slash(process.cwd()).indexOf('/packages/test') > 0 &&
fs.existsSync(path.resolve(process.cwd(), '../@vue'))) {
if (
slash(process.cwd()).indexOf('/packages/test') > 0 && (
fs.existsSync(path.resolve(process.cwd(), '../@vue')) ||
fs.existsSync(path.resolve(process.cwd(), '../../@vue'))
)
) {
process.env.VUE_CLI_DEBUG = true
}
+22 -6
View File
@@ -3,9 +3,13 @@ const path = require('path')
const chalk = require('chalk')
const resolve = require('resolve')
const Generator = require('./Generator')
const { loadOptions } = require('./options')
const installDeps = require('./util/installDeps')
const clearConsole = require('./util/clearConsole')
const {
log,
error,
hasYarn,
logWithSpinner,
stopSpinner
} = require('@vue/cli-shared-utils')
@@ -58,13 +62,25 @@ async function invoke (pluginName, options) {
createCompleteCbs
)
clearConsole()
logWithSpinner('🚀', `Invoking generator for ${resolvedPluginName}...`)
await generator.generate()
// TODO check if package.json was changed,
// if yes installDeps
logWithSpinner('📦', `Installing additional dependencies...`)
const isTestOrDebug = process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG
const newDeps = generator.pkg.dependencies
const newDevDeps = generator.pkg.devDependencies
const depsChanged = (
JSON.stringify(newDeps) !== JSON.stringify(pkg.dependencies) ||
JSON.stringify(newDevDeps) !== JSON.stringify(pkg.devDependencies)
)
if (createCompleteCbs.lenght) {
if (!isTestOrDebug && depsChanged) {
logWithSpinner('📦', `Installing additional dependencies...`)
const packageManager = loadOptions().packageManager || (hasYarn ? 'yarn' : 'npm')
await installDeps(context, packageManager)
}
if (createCompleteCbs.length) {
logWithSpinner('⚓', `Running completion hooks...`)
for (const cb of createCompleteCbs) {
await cb()
@@ -73,8 +89,8 @@ async function invoke (pluginName, options) {
stopSpinner()
log()
log(` Successfully invoked generator for plugin: ${chalk.cyan(resolvedPluginName)}`)
log(` You should review and commit the changes.`)
log(` Successfully invoked generator for plugin: ${chalk.cyan(resolvedPluginName)}`)
log(` You should review and commit the changes.`)
log()
}