index.js 1.7 KB

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