UNPKG

3.06 kBJavaScriptView Raw
1module.exports = api => {
2 api.chainWebpack(webpackConfig => {
3 if (process.env.NODE_ENV === 'test') {
4 webpackConfig.mode('none')
5
6 webpackConfig.merge({
7 target: 'node',
8 devtool: 'inline-cheap-module-source-map'
9 })
10
11 const { semver, loadModule } = require('@vue/cli-shared-utils')
12 const vue = loadModule('vue', api.service.context)
13 const isVue3 = (vue && semver.major(vue.version) === 3)
14
15 // when target === 'node', vue-loader will attempt to generate
16 // SSR-optimized code. We need to turn that off here.
17 webpackConfig.module
18 .rule('vue')
19 .use('vue-loader')
20 .tap(options => {
21 if (isVue3) {
22 options.isServerBuild = false
23 } else {
24 options.optimizeSSR = false
25 }
26
27 return options
28 })
29 }
30 })
31
32 api.registerCommand('test:unit', {
33 description: 'run unit tests with mochapack',
34 usage: 'vue-cli-service test:unit [options] [...files]',
35 options: {
36 '--watch, -w': 'run in watch mode',
37 '--grep, -g': 'only run tests matching <pattern>',
38 '--slow, -s': '"slow" test threshold in milliseconds',
39 '--timeout, -t': 'timeout threshold in milliseconds',
40 '--bail, -b': 'bail after first test failure',
41 '--require, -r': 'require the given module before running tests',
42 '--include': 'include the given module into test bundle',
43 '--inspect-brk': 'Enable inspector to debug the tests'
44 },
45 details: (
46 `The above list only includes the most commonly used options.\n` +
47 `For a full list of available options, see\n` +
48 `https://sysgears.github.io/mochapack/docs/installation/cli-usage.html`
49 )
50 }, (args, rawArgv) => {
51 let nodeArgs = []
52
53 const inspectPos = rawArgv.findIndex(arg => arg.startsWith('--inspect-brk'))
54 if (inspectPos !== -1) {
55 nodeArgs = rawArgv.splice(inspectPos, 1)
56 }
57 // for @vue/babel-preset-app <= v4.0.0-rc.7
58 process.env.VUE_CLI_BABEL_TARGET_NODE = true
59 // start runner
60 const { execa } = require('@vue/cli-shared-utils')
61 const bin = require.resolve('mochapack/bin/mochapack')
62 const hasInlineFilesGlob = args._ && args._.length
63 const argv = [
64 ...nodeArgs,
65 bin,
66 '--recursive',
67 '--require',
68 require.resolve('./setup.js'),
69 '--webpack-config',
70 require.resolve('@vue/cli-service/webpack.config.js'),
71 ...rawArgv,
72 ...(hasInlineFilesGlob
73 ? []
74 : [
75 api.hasPlugin('typescript')
76 ? `tests/unit/**/*.spec.[jt]s`
77 : `tests/unit/**/*.spec.js`
78 ])
79 ]
80
81 return new Promise((resolve, reject) => {
82 const child = execa('node', argv, { stdio: 'inherit' })
83 child.on('error', reject)
84 child.on('exit', code => {
85 if (code !== 0) {
86 reject(new Error(`mochapack exited with code ${code}.`))
87 } else {
88 resolve()
89 }
90 })
91 })
92 })
93}
94
95module.exports.defaultModes = {
96 'test:unit': 'test'
97}