Skip to content

Commit 21187b4

Browse files
committed
feat: mocha-webpack plugin
1 parent 89d1932 commit 21187b4

File tree

9 files changed

+213
-28
lines changed

9 files changed

+213
-28
lines changed

packages/@vue/babel-preset-app/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ module.exports = (context, options = {}) => {
1616

1717
const envOptions = {
1818
modules: options.modules || false,
19-
useBuiltIns: 'usage',
20-
targets: options.targets
19+
targets: options.targets,
20+
useBuiltIns: 'usage'
2121
}
2222
delete envOptions.jsx
2323
// target running node version (this is set by unit testing plugins)

packages/@vue/cli-plugin-unit-jest/generator/template/test/unit/HelloWorld.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ describe('Hello.vue', () => {
55
it('renders props.msg when passed', () => {
66
const msg = 'new message'
77
const wrapper = shallow(HelloWorld, {
8-
context: { props: { msg }}
8+
propsData: { msg }
99
})
10-
expect(wrapper.text()).toBe(msg)
10+
expect(wrapper.text()).toContain(msg)
1111
})
1212
})

packages/@vue/cli-plugin-unit-mocha-webpack/generator/template/test/unit/HelloWorld.spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import { shallow } from 'vue-test-utils'
21
<%_ if (options.assertionLibrary === 'expect') { _%>
3-
import { expect } from 'expect'
2+
import expect from 'expect'
43
<%_ } _%>
54
<%_ if (options.assertionLibrary === 'chai') { _%>
65
import { expect } from 'chai'
76
<%_ } _%>
7+
import { shallow } from 'vue-test-utils'
88
import HelloWorld from '@/components/HelloWorld.vue'
99

1010
describe('Hello.vue', () => {
1111
it('renders props.msg when passed', () => {
1212
const msg = 'new message'
1313
const wrapper = shallow(HelloWorld, {
14-
context: { props: { msg } }
14+
propsData: { msg }
1515
})
1616
<%_ if (options.assertionLibrary === 'expect') { _%>
17-
expect(wrapper.text()).toBe(msg)
17+
expect(wrapper.text()).toContain(msg)
1818
<%_ } else if (options.assertionLibrary === 'chai') { _%>
19-
expect(wrapper.text()).to.equal(msg)
19+
expect(wrapper.text()).to.include(msg)
2020
<%_ } else { _%>
2121
// assert wrapper.text() equals msg
2222
<%_ } _%>
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
11
module.exports = api => {
2-
api.registerCommand('test', {
3-
description: 'run unit tests with mocha-webpack'
4-
}, args => {
5-
api.setMode('test')
6-
// for @vue/babel-preset-app
7-
process.env.VUE_CLI_BABEL_TARGET_NODE = true
8-
require('./runner')(api.resolveWebpackConfig(), args)
9-
})
10-
112
api.configureWebpack(webpackConfig => {
123
if (process.env.NODE_ENV === 'test') {
134
if (!webpackConfig.externals) {
145
webpackConfig.externals = []
156
}
16-
webpackConfig.externals = [].conact(
7+
webpackConfig.externals = [].concat(
178
webpackConfig.externals,
189
require('webpack-node-externals')()
1910
)
2011
webpackConfig.devtool = 'inline-cheap-module-source-map'
2112
}
2213
})
14+
15+
api.registerCommand('test', {
16+
description: 'run unit tests with mocha-webpack',
17+
usage: 'vue-cli-service test [options] [...files]',
18+
options: {
19+
'--watch, -w': 'run in watch mode',
20+
'--grep, -g': 'only run tests matching <pattern>',
21+
'--slow, -s': '"slow" test threshold in milliseconds',
22+
'--timeout, -t': 'timeout threshold in milliseconds',
23+
'--bail, -b': 'bail after first test failure',
24+
'--require, -r': 'require the given module before running tests',
25+
'--include': 'include the given module into test bundle'
26+
},
27+
details: (
28+
`The above list only includes the most commonly used options.\n` +
29+
`For a full list of available options, see\n` +
30+
`http://zinserjan.github.io/mocha-webpack/docs/installation/cli-usage.html`
31+
)
32+
}, (args, rawArgv) => {
33+
api.setMode('test')
34+
// for @vue/babel-preset-app
35+
process.env.VUE_CLI_BABEL_TARGET_NODE = true
36+
// setup JSDOM
37+
require('jsdom-global')()
38+
// start runner
39+
return require('./runner')(api.resolveWebpackConfig(), args, rawArgv)
40+
})
2341
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,132 @@
1-
module.exports = () => {
2-
require('jsdom-global')()
3-
console.log('running test!')
1+
module.exports = (webpackConfig, args, rawArgv) => {
2+
// The following is largely copied from
3+
// https://github.com/zinserjan/mocha-webpack/blob/master/src/cli/index.js
4+
// with small modifications to use an in-memory webpack config instead.
5+
// It would be nice if we can somehow use mocha-webpack's CLI directly.
6+
7+
/**
8+
* Copyright (c) 2016-2017 Jan-André Zinser
9+
* Licensed under MIT
10+
* https://github.com/zinserjan/mocha-webpack/blob/master/LICENSE.md
11+
*
12+
* modified by Yuxi Evan You
13+
*/
14+
15+
const fs = require('fs')
16+
const path = require('path')
17+
const _ = require('lodash')
18+
const parseArgv = require('mocha-webpack/lib/cli/parseArgv').default
19+
const createMochaWebpack = require('mocha-webpack/lib/createMochaWebpack')
20+
const { ensureGlob, extensionsToGlob } = require('mocha-webpack/lib/util/glob')
21+
22+
const options = Object.assign(
23+
{},
24+
parseArgv([]),
25+
parseArgv(rawArgv, true)
26+
)
27+
28+
if (!args._.length) {
29+
options.recursive = true
30+
}
31+
32+
function resolve (mod) {
33+
const absolute = fs.existsSync(mod) || fs.existsSync(`${mod}.js`)
34+
const file = absolute ? path.resolve(mod) : mod
35+
return file
36+
}
37+
38+
function exit (lazy, code) {
39+
if (lazy) {
40+
process.on('exit', () => {
41+
process.exit(code)
42+
})
43+
} else {
44+
process.exit(code)
45+
}
46+
}
47+
48+
options.require.forEach((mod) => {
49+
require(resolve(mod)) // eslint-disable-line global-require
50+
})
51+
52+
options.include = options.include.map(resolve)
53+
54+
options.webpackConfig = webpackConfig
55+
56+
const mochaWebpack = createMochaWebpack()
57+
58+
options.include.forEach((f) => mochaWebpack.addInclude(f))
59+
60+
const extensions = _.get(options.webpackConfig, 'resolve.extensions', ['.js'])
61+
const fallbackFileGlob = extensionsToGlob(extensions)
62+
const fileGlob = options.glob != null ? options.glob : fallbackFileGlob
63+
64+
options.files.forEach((f) => mochaWebpack.addEntry(ensureGlob(f, options.recursive, fileGlob)))
65+
66+
mochaWebpack.cwd(process.cwd())
67+
mochaWebpack.webpackConfig(options.webpackConfig)
68+
mochaWebpack.bail(options.bail)
69+
mochaWebpack.reporter(options.reporter, options.reporterOptions)
70+
mochaWebpack.ui(options.ui)
71+
mochaWebpack.interactive(options.interactive)
72+
73+
if (options.fgrep) {
74+
mochaWebpack.fgrep(options.fgrep)
75+
}
76+
77+
if (options.grep) {
78+
mochaWebpack.grep(options.grep)
79+
}
80+
81+
if (options.invert) {
82+
mochaWebpack.invert()
83+
}
84+
85+
if (options.checkLeaks) {
86+
mochaWebpack.ignoreLeaks(false)
87+
}
88+
89+
if (options.fullTrace) {
90+
mochaWebpack.fullStackTrace()
91+
}
92+
93+
mochaWebpack.useColors(options.colors)
94+
mochaWebpack.useInlineDiffs(options.inlineDiffs)
95+
mochaWebpack.timeout(options.timeout)
96+
97+
if (options.retries) {
98+
mochaWebpack.retries(options.retries)
99+
}
100+
101+
mochaWebpack.slow(options.slow)
102+
103+
if (options.asyncOnly) {
104+
mochaWebpack.asyncOnly()
105+
}
106+
107+
if (options.delay) {
108+
mochaWebpack.delay()
109+
}
110+
111+
if (options.growl) {
112+
mochaWebpack.growl()
113+
}
114+
115+
return Promise
116+
.resolve()
117+
.then(() => {
118+
if (options.watch) {
119+
return mochaWebpack.watch()
120+
}
121+
return mochaWebpack.run()
122+
})
123+
.then((failures) => {
124+
exit(options.exit, failures)
125+
})
126+
.catch((e) => {
127+
if (e) {
128+
console.error(e.stack); // eslint-disable-line
129+
}
130+
exit(options.exit, 1)
131+
})
4132
}

packages/@vue/cli-service/bin/vue-cli-service

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ if (!semver.satisfies(process.version, requiredVersion)) {
1515
const Service = require('../lib/Service')
1616
const service = new Service()
1717

18-
const args = require('minimist')(process.argv.slice(2))
18+
const rawArgv = process.argv.slice(2)
19+
const args = require('minimist')(rawArgv)
1920
const command = args._[0]
2021

21-
service.run(command, args).catch(err => {
22+
service.run(command, args, rawArgv).catch(err => {
2223
error(err)
2324
process.exit(1)
2425
})

packages/@vue/cli-service/lib/Service.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module.exports = class Service {
9090
}))
9191
}
9292

93-
run (name, args) {
93+
run (name, args, rawArgv) {
9494
let command = this.commands[name]
9595
if (!command && name) {
9696
error(`command "${name}" does not exist.`)
@@ -99,9 +99,10 @@ module.exports = class Service {
9999
command = this.commands.help
100100
} else {
101101
args._.shift() // remove command itself
102+
rawArgv.shift()
102103
}
103104
const { fn } = command
104-
return Promise.resolve(fn(args))
105+
return Promise.resolve(fn(args, rawArgv))
105106
}
106107

107108
resolveWebpackConfig () {

packages/@vue/cli-service/lib/commands/inspect.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ module.exports = (api, options) => {
33
description: 'inspect internal webpack config',
44
usage: 'vue-cli-service inspect [options] [...keys]',
55
options: {
6-
'--env': 'specify NODE_ENV (default: development)'
6+
'--mode': 'specify env mode (default: development)'
77
}
88
}, args => {
9-
api.setEnv(args.env || 'development')
9+
api.setMode(args.mode || 'development')
1010

1111
const stringify = require('javascript-stringify')
1212
const config = api.resolveWebpackConfig()

yarn.lock

+37
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,10 @@ assert@^1.1.1:
821821
dependencies:
822822
util "0.10.3"
823823

824+
assertion-error@^1.0.1:
825+
version "1.0.2"
826+
resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.0.2.tgz#13ca515d86206da0bac66e834dd397d87581094c"
827+
824828
astral-regex@^1.0.0:
825829
version "1.0.0"
826830
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9"
@@ -1784,6 +1788,17 @@ center-align@^0.1.1:
17841788
align-text "^0.1.3"
17851789
lazy-cache "^1.0.3"
17861790

1791+
chai@^4.1.2:
1792+
version "4.1.2"
1793+
resolved "https://registry.yarnpkg.com/chai/-/chai-4.1.2.tgz#0f64584ba642f0f2ace2806279f4f06ca23ad73c"
1794+
dependencies:
1795+
assertion-error "^1.0.1"
1796+
check-error "^1.0.1"
1797+
deep-eql "^3.0.0"
1798+
get-func-name "^2.0.0"
1799+
pathval "^1.0.0"
1800+
type-detect "^4.0.0"
1801+
17871802
17881803
version "2.1.0"
17891804
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.1.0.tgz#ac5becf14fa21b99c6c92ca7a7d7cfd5b17e743e"
@@ -1814,6 +1829,10 @@ chardet@^0.4.0:
18141829
version "0.4.2"
18151830
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.4.2.tgz#b5473b33dc97c424e5d98dc87d55d4d8a29c8bf2"
18161831

1832+
check-error@^1.0.1:
1833+
version "1.0.2"
1834+
resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82"
1835+
18171836
chokidar@^1.6.0, chokidar@^1.6.1, chokidar@^1.7.0:
18181837
version "1.7.0"
18191838
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.7.0.tgz#798e689778151c8076b4b360e5edd28cda2bb468"
@@ -2569,6 +2588,12 @@ dedent@^0.7.0:
25692588
version "0.7.0"
25702589
resolved "https://registry.yarnpkg.com/dedent/-/dedent-0.7.0.tgz#2495ddbaf6eb874abb0e1be9df22d2e5a544326c"
25712590

2591+
deep-eql@^3.0.0:
2592+
version "3.0.1"
2593+
resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-3.0.1.tgz#dfc9404400ad1c8fe023e7da1df1c147c4b444df"
2594+
dependencies:
2595+
type-detect "^4.0.0"
2596+
25722597
deep-equal@^1.0.1:
25732598
version "1.0.1"
25742599
resolved "https://registry.yarnpkg.com/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5"
@@ -3577,6 +3602,10 @@ get-caller-file@^1.0.1:
35773602
version "1.0.2"
35783603
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.2.tgz#f702e63127e7e231c160a80c1554acb70d5047e5"
35793604

3605+
get-func-name@^2.0.0:
3606+
version "2.0.0"
3607+
resolved "https://registry.yarnpkg.com/get-func-name/-/get-func-name-2.0.0.tgz#ead774abee72e20409433a066366023dd6887a41"
3608+
35803609
get-own-enumerable-property-symbols@^2.0.1:
35813610
version "2.0.1"
35823611
resolved "https://registry.yarnpkg.com/get-own-enumerable-property-symbols/-/get-own-enumerable-property-symbols-2.0.1.tgz#5c4ad87f2834c4b9b4e84549dc1e0650fb38c24b"
@@ -6008,6 +6037,10 @@ path-type@^2.0.0:
60086037
dependencies:
60096038
pify "^2.0.0"
60106039

6040+
pathval@^1.0.0:
6041+
version "1.1.0"
6042+
resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0"
6043+
60116044
pbkdf2@^3.0.3:
60126045
version "3.0.14"
60136046
resolved "https://registry.yarnpkg.com/pbkdf2/-/pbkdf2-3.0.14.tgz#a35e13c64799b06ce15320f459c230e68e73bade"
@@ -7730,6 +7763,10 @@ type-check@~0.3.2:
77307763
dependencies:
77317764
prelude-ls "~1.1.2"
77327765

7766+
type-detect@^4.0.0:
7767+
version "4.0.5"
7768+
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.5.tgz#d70e5bc81db6de2a381bcaca0c6e0cbdc7635de2"
7769+
77337770
type-is@~1.6.15:
77347771
version "1.6.15"
77357772
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.15.tgz#cab10fb4909e441c82842eafe1ad646c81804410"

0 commit comments

Comments
 (0)