index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. module.exports = (api, _, __, invoking) => {
  2. api.render('./template', {
  3. hasTS: api.hasPlugin('typescript')
  4. })
  5. api.extendPackage({
  6. devDependencies: {
  7. '@vue/test-utils': '1.0.0-beta.29',
  8. 'chai': '^4.1.2'
  9. },
  10. scripts: {
  11. 'test:unit': 'vue-cli-service test:unit'
  12. }
  13. })
  14. if (api.hasPlugin('eslint')) {
  15. applyESLint(api)
  16. api.extendPackage({
  17. eslintConfig: {
  18. overrides: [
  19. {
  20. files: ['**/__tests__/*.{j,t}s?(x)'],
  21. env: {
  22. mocha: true
  23. }
  24. }
  25. ]
  26. }
  27. })
  28. }
  29. if (api.hasPlugin('typescript')) {
  30. applyTS(api, invoking)
  31. }
  32. }
  33. const applyESLint = module.exports.applyESLint = api => {
  34. api.render(files => {
  35. files['tests/unit/.eslintrc.js'] = api.genJSConfig({
  36. env: { mocha: true }
  37. })
  38. })
  39. }
  40. const applyTS = module.exports.applyTS = (api, invoking) => {
  41. api.extendPackage({
  42. devDependencies: {
  43. '@types/mocha': '^5.2.4',
  44. '@types/chai': '^4.1.0'
  45. }
  46. })
  47. // inject mocha/chai types to tsconfig.json
  48. if (invoking) {
  49. api.render(files => {
  50. const tsconfig = files['tsconfig.json']
  51. if (tsconfig) {
  52. const parsed = JSON.parse(tsconfig)
  53. const types = parsed.compilerOptions.types
  54. if (types) {
  55. if (!types.includes('mocha')) {
  56. types.push('mocha')
  57. }
  58. if (!types.includes('chai')) {
  59. types.push('chai')
  60. }
  61. }
  62. files['tsconfig.json'] = JSON.stringify(parsed, null, 2)
  63. }
  64. })
  65. }
  66. }