Skip to content

Commit

Permalink
feat: mocha-webpack plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 31, 2017
1 parent 89d1932 commit 21187b4
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/@vue/babel-preset-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module.exports = (context, options = {}) => {

const envOptions = {
modules: options.modules || false,
useBuiltIns: 'usage',
targets: options.targets
targets: options.targets,
useBuiltIns: 'usage'
}
delete envOptions.jsx
// target running node version (this is set by unit testing plugins)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ describe('Hello.vue', () => {
it('renders props.msg when passed', () => {
const msg = 'new message'
const wrapper = shallow(HelloWorld, {
context: { props: { msg }}
propsData: { msg }
})
expect(wrapper.text()).toBe(msg)
expect(wrapper.text()).toContain(msg)
})
})
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { shallow } from 'vue-test-utils'
<%_ if (options.assertionLibrary === 'expect') { _%>
import { expect } from 'expect'
import expect from 'expect'
<%_ } _%>
<%_ if (options.assertionLibrary === 'chai') { _%>
import { expect } from 'chai'
<%_ } _%>
import { shallow } from 'vue-test-utils'
import HelloWorld from '@/components/HelloWorld.vue'

describe('Hello.vue', () => {
it('renders props.msg when passed', () => {
const msg = 'new message'
const wrapper = shallow(HelloWorld, {
context: { props: { msg } }
propsData: { msg }
})
<%_ if (options.assertionLibrary === 'expect') { _%>
expect(wrapper.text()).toBe(msg)
expect(wrapper.text()).toContain(msg)
<%_ } else if (options.assertionLibrary === 'chai') { _%>
expect(wrapper.text()).to.equal(msg)
expect(wrapper.text()).to.include(msg)
<%_ } else { _%>
// assert wrapper.text() equals msg
<%_ } _%>
Expand Down
38 changes: 28 additions & 10 deletions packages/@vue/cli-plugin-unit-mocha-webpack/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
module.exports = api => {
api.registerCommand('test', {
description: 'run unit tests with mocha-webpack'
}, args => {
api.setMode('test')
// for @vue/babel-preset-app
process.env.VUE_CLI_BABEL_TARGET_NODE = true
require('./runner')(api.resolveWebpackConfig(), args)
})

api.configureWebpack(webpackConfig => {
if (process.env.NODE_ENV === 'test') {
if (!webpackConfig.externals) {
webpackConfig.externals = []
}
webpackConfig.externals = [].conact(
webpackConfig.externals = [].concat(
webpackConfig.externals,
require('webpack-node-externals')()
)
webpackConfig.devtool = 'inline-cheap-module-source-map'
}
})

api.registerCommand('test', {
description: 'run unit tests with mocha-webpack',
usage: 'vue-cli-service test [options] [...files]',
options: {
'--watch, -w': 'run in watch mode',
'--grep, -g': 'only run tests matching <pattern>',
'--slow, -s': '"slow" test threshold in milliseconds',
'--timeout, -t': 'timeout threshold in milliseconds',
'--bail, -b': 'bail after first test failure',
'--require, -r': 'require the given module before running tests',
'--include': 'include the given module into test bundle'
},
details: (
`The above list only includes the most commonly used options.\n` +
`For a full list of available options, see\n` +
`http://zinserjan.github.io/mocha-webpack/docs/installation/cli-usage.html`
)
}, (args, rawArgv) => {
api.setMode('test')
// for @vue/babel-preset-app
process.env.VUE_CLI_BABEL_TARGET_NODE = true
// setup JSDOM
require('jsdom-global')()
// start runner
return require('./runner')(api.resolveWebpackConfig(), args, rawArgv)
})
}
134 changes: 131 additions & 3 deletions packages/@vue/cli-plugin-unit-mocha-webpack/runner.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,132 @@
module.exports = () => {
require('jsdom-global')()
console.log('running test!')
module.exports = (webpackConfig, args, rawArgv) => {
// The following is largely copied from
// https://github.com/zinserjan/mocha-webpack/blob/master/src/cli/index.js
// with small modifications to use an in-memory webpack config instead.
// It would be nice if we can somehow use mocha-webpack's CLI directly.

/**
* Copyright (c) 2016-2017 Jan-André Zinser
* Licensed under MIT
* https://github.com/zinserjan/mocha-webpack/blob/master/LICENSE.md
*
* modified by Yuxi Evan You
*/

const fs = require('fs')
const path = require('path')
const _ = require('lodash')
const parseArgv = require('mocha-webpack/lib/cli/parseArgv').default
const createMochaWebpack = require('mocha-webpack/lib/createMochaWebpack')
const { ensureGlob, extensionsToGlob } = require('mocha-webpack/lib/util/glob')

const options = Object.assign(
{},
parseArgv([]),
parseArgv(rawArgv, true)
)

if (!args._.length) {
options.recursive = true
}

function resolve (mod) {
const absolute = fs.existsSync(mod) || fs.existsSync(`${mod}.js`)
const file = absolute ? path.resolve(mod) : mod
return file
}

function exit (lazy, code) {
if (lazy) {
process.on('exit', () => {
process.exit(code)
})
} else {
process.exit(code)
}
}

options.require.forEach((mod) => {
require(resolve(mod)) // eslint-disable-line global-require
})

options.include = options.include.map(resolve)

options.webpackConfig = webpackConfig

const mochaWebpack = createMochaWebpack()

options.include.forEach((f) => mochaWebpack.addInclude(f))

const extensions = _.get(options.webpackConfig, 'resolve.extensions', ['.js'])
const fallbackFileGlob = extensionsToGlob(extensions)
const fileGlob = options.glob != null ? options.glob : fallbackFileGlob

options.files.forEach((f) => mochaWebpack.addEntry(ensureGlob(f, options.recursive, fileGlob)))

mochaWebpack.cwd(process.cwd())
mochaWebpack.webpackConfig(options.webpackConfig)
mochaWebpack.bail(options.bail)
mochaWebpack.reporter(options.reporter, options.reporterOptions)
mochaWebpack.ui(options.ui)
mochaWebpack.interactive(options.interactive)

if (options.fgrep) {
mochaWebpack.fgrep(options.fgrep)
}

if (options.grep) {
mochaWebpack.grep(options.grep)
}

if (options.invert) {
mochaWebpack.invert()
}

if (options.checkLeaks) {
mochaWebpack.ignoreLeaks(false)
}

if (options.fullTrace) {
mochaWebpack.fullStackTrace()
}

mochaWebpack.useColors(options.colors)
mochaWebpack.useInlineDiffs(options.inlineDiffs)
mochaWebpack.timeout(options.timeout)

if (options.retries) {
mochaWebpack.retries(options.retries)
}

mochaWebpack.slow(options.slow)

if (options.asyncOnly) {
mochaWebpack.asyncOnly()
}

if (options.delay) {
mochaWebpack.delay()
}

if (options.growl) {
mochaWebpack.growl()
}

return Promise
.resolve()
.then(() => {
if (options.watch) {
return mochaWebpack.watch()
}
return mochaWebpack.run()
})
.then((failures) => {
exit(options.exit, failures)
})
.catch((e) => {
if (e) {
console.error(e.stack); // eslint-disable-line
}
exit(options.exit, 1)
})
}
5 changes: 3 additions & 2 deletions packages/@vue/cli-service/bin/vue-cli-service
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ if (!semver.satisfies(process.version, requiredVersion)) {
const Service = require('../lib/Service')
const service = new Service()

const args = require('minimist')(process.argv.slice(2))
const rawArgv = process.argv.slice(2)
const args = require('minimist')(rawArgv)
const command = args._[0]

service.run(command, args).catch(err => {
service.run(command, args, rawArgv).catch(err => {
error(err)
process.exit(1)
})
5 changes: 3 additions & 2 deletions packages/@vue/cli-service/lib/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module.exports = class Service {
}))
}

run (name, args) {
run (name, args, rawArgv) {
let command = this.commands[name]
if (!command && name) {
error(`command "${name}" does not exist.`)
Expand All @@ -99,9 +99,10 @@ module.exports = class Service {
command = this.commands.help
} else {
args._.shift() // remove command itself
rawArgv.shift()
}
const { fn } = command
return Promise.resolve(fn(args))
return Promise.resolve(fn(args, rawArgv))
}

resolveWebpackConfig () {
Expand Down
4 changes: 2 additions & 2 deletions packages/@vue/cli-service/lib/commands/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module.exports = (api, options) => {
description: 'inspect internal webpack config',
usage: 'vue-cli-service inspect [options] [...keys]',
options: {
'--env': 'specify NODE_ENV (default: development)'
'--mode': 'specify env mode (default: development)'
}
}, args => {
api.setEnv(args.env || 'development')
api.setMode(args.mode || 'development')

const stringify = require('javascript-stringify')
const config = api.resolveWebpackConfig()
Expand Down
37 changes: 37 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,10 @@ assert@^1.1.1:
dependencies:
util "0.10.3"

assertion-error@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.0.2.tgz#13ca515d86206da0bac66e834dd397d87581094c"

astral-regex@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9"
Expand Down Expand Up @@ -1784,6 +1788,17 @@ center-align@^0.1.1:
align-text "^0.1.3"
lazy-cache "^1.0.3"

chai@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/chai/-/chai-4.1.2.tgz#0f64584ba642f0f2ace2806279f4f06ca23ad73c"
dependencies:
assertion-error "^1.0.1"
check-error "^1.0.1"
deep-eql "^3.0.0"
get-func-name "^2.0.0"
pathval "^1.0.0"
type-detect "^4.0.0"

chalk@2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.1.0.tgz#ac5becf14fa21b99c6c92ca7a7d7cfd5b17e743e"
Expand Down Expand Up @@ -1814,6 +1829,10 @@ chardet@^0.4.0:
version "0.4.2"
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.4.2.tgz#b5473b33dc97c424e5d98dc87d55d4d8a29c8bf2"

check-error@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82"

chokidar@^1.6.0, chokidar@^1.6.1, chokidar@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.7.0.tgz#798e689778151c8076b4b360e5edd28cda2bb468"
Expand Down Expand Up @@ -2569,6 +2588,12 @@ dedent@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/dedent/-/dedent-0.7.0.tgz#2495ddbaf6eb874abb0e1be9df22d2e5a544326c"

deep-eql@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-3.0.1.tgz#dfc9404400ad1c8fe023e7da1df1c147c4b444df"
dependencies:
type-detect "^4.0.0"

deep-equal@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5"
Expand Down Expand Up @@ -3577,6 +3602,10 @@ get-caller-file@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.2.tgz#f702e63127e7e231c160a80c1554acb70d5047e5"

get-func-name@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/get-func-name/-/get-func-name-2.0.0.tgz#ead774abee72e20409433a066366023dd6887a41"

get-own-enumerable-property-symbols@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/get-own-enumerable-property-symbols/-/get-own-enumerable-property-symbols-2.0.1.tgz#5c4ad87f2834c4b9b4e84549dc1e0650fb38c24b"
Expand Down Expand Up @@ -6008,6 +6037,10 @@ path-type@^2.0.0:
dependencies:
pify "^2.0.0"

pathval@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0"

pbkdf2@^3.0.3:
version "3.0.14"
resolved "https://registry.yarnpkg.com/pbkdf2/-/pbkdf2-3.0.14.tgz#a35e13c64799b06ce15320f459c230e68e73bade"
Expand Down Expand Up @@ -7730,6 +7763,10 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"

type-detect@^4.0.0:
version "4.0.5"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.5.tgz#d70e5bc81db6de2a381bcaca0c6e0cbdc7635de2"

type-is@~1.6.15:
version "1.6.15"
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.15.tgz#cab10fb4909e441c82842eafe1ad646c81804410"
Expand Down

0 comments on commit 21187b4

Please sign in to comment.