index.js 2.6 KB

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