index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.0-beta.29'
  11. },
  12. jest: {
  13. moduleFileExtensions: [
  14. 'js',
  15. 'jsx',
  16. 'json',
  17. // tell Jest to handle *.vue files
  18. 'vue'
  19. ],
  20. transform: {
  21. // process *.vue files with vue-jest
  22. '^.+\\.vue$': 'vue-jest',
  23. '.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$':
  24. 'jest-transform-stub'
  25. },
  26. 'transformIgnorePatterns': ['/node_modules/'],
  27. // support the same @ -> src alias mapping in source code
  28. moduleNameMapper: {
  29. '^@/(.*)$': '<rootDir>/src/$1'
  30. },
  31. // serializer for snapshots
  32. snapshotSerializers: ['jest-serializer-vue'],
  33. testMatch: [
  34. '**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'
  35. ],
  36. // https://github.com/facebook/jest/issues/6766
  37. testURL: 'http://localhost/',
  38. watchPlugins: [
  39. 'jest-watch-typeahead/filename',
  40. 'jest-watch-typeahead/testname'
  41. ]
  42. }
  43. })
  44. if (!api.hasPlugin('typescript')) {
  45. api.extendPackage({
  46. jest: {
  47. transform: {
  48. '^.+\\.jsx?$': 'babel-jest'
  49. }
  50. }
  51. })
  52. if (api.hasPlugin('babel')) {
  53. api.extendPackage({
  54. devDependencies: {
  55. 'babel-jest': '^23.6.0',
  56. // this is for now necessary to force babel-jest and vue-jest to use babel 7
  57. 'babel-core': '7.0.0-bridge.0'
  58. }
  59. })
  60. } else {
  61. // Jest's shipped babel-jest still uses babel 6,
  62. // so we cannot use extendPackage which renders babel.config.js.
  63. api.render(files => {
  64. files['.babelrc'] = JSON.stringify(
  65. {
  66. plugins: ['transform-es2015-modules-commonjs']
  67. },
  68. null,
  69. 2
  70. )
  71. })
  72. }
  73. } else {
  74. applyTS(api, invoking)
  75. }
  76. if (api.hasPlugin('eslint')) {
  77. applyESLint(api)
  78. }
  79. }
  80. const applyTS = (module.exports.applyTS = (api, invoking) => {
  81. api.extendPackage({
  82. jest: {
  83. moduleFileExtensions: ['ts', 'tsx'],
  84. transform: {
  85. '^.+\\.tsx?$': 'ts-jest'
  86. }
  87. },
  88. devDependencies: {
  89. '@types/jest': '^23.1.4',
  90. 'ts-jest': '^23.0.0'
  91. }
  92. })
  93. if (api.hasPlugin('babel')) {
  94. api.extendPackage({
  95. jest: {
  96. globals: {
  97. 'ts-jest': {
  98. // we need babel to transpile JSX
  99. babelConfig: true
  100. }
  101. }
  102. },
  103. devDependencies: {
  104. // this is for now necessary to force ts-jest and vue-jest to use babel 7
  105. 'babel-core': '7.0.0-bridge.0'
  106. }
  107. })
  108. }
  109. // inject jest type to tsconfig.json
  110. if (invoking) {
  111. api.render(files => {
  112. const tsconfig = files['tsconfig.json']
  113. if (tsconfig) {
  114. const parsed = JSON.parse(tsconfig)
  115. if (
  116. parsed.compilerOptions.types &&
  117. !parsed.compilerOptions.types.includes('jest')
  118. ) {
  119. parsed.compilerOptions.types.push('jest')
  120. }
  121. files['tsconfig.json'] = JSON.stringify(parsed, null, 2)
  122. }
  123. })
  124. }
  125. })
  126. const applyESLint = (module.exports.applyESLint = api => {
  127. api.render(files => {
  128. files['tests/unit/.eslintrc.js'] = api.genJSConfig({
  129. env: { jest: true }
  130. })
  131. })
  132. })