-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
213 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
packages/@vue/cli-plugin-unit-mocha-webpack/generator/template/test/unit/HelloWorld.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters