Babel.spec.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import Vue from 'vue'
  2. import Basic from './resources/Basic.vue'
  3. import BasicSrc from './resources/BasicSrc.vue'
  4. import jestVue from '../vue-jest'
  5. import { resolve } from 'path'
  6. import {
  7. readFileSync,
  8. writeFileSync,
  9. renameSync
  10. } from 'fs'
  11. import clearModule from 'clear-module'
  12. import cache from '../lib/cache'
  13. beforeEach(() => {
  14. cache.flushAll()
  15. clearModule.all()
  16. })
  17. test('processes .vue files', () => {
  18. const vm = new Vue(Basic).$mount()
  19. vm.toggleClass()
  20. expect(typeof vm.$el).toBe('object')
  21. })
  22. test('processes .vue files using src attributes', () => {
  23. const vm = new Vue(BasicSrc).$mount()
  24. expect(typeof vm.$el).toBe('object')
  25. })
  26. test('processes .vue files with default babel if there is no .babelrc', () => {
  27. const babelRcPath = resolve(__dirname, '../.babelrc')
  28. const tempPath = resolve(__dirname, '../.renamed')
  29. renameSync(babelRcPath, tempPath)
  30. const filePath = resolve(__dirname, './resources/Basic.vue')
  31. const fileString = readFileSync(filePath, { encoding: 'utf8' })
  32. try {
  33. jestVue.process(fileString, filePath)
  34. } catch (err) {
  35. renameSync(tempPath, babelRcPath)
  36. throw err
  37. }
  38. renameSync(tempPath, babelRcPath)
  39. })
  40. test('logs info when there is no .babelrc', () => {
  41. const babelRcPath = resolve(__dirname, '../.babelrc')
  42. const tempPath = resolve(__dirname, '../.renamed')
  43. renameSync(babelRcPath, tempPath)
  44. const info = jest.spyOn(global.console, 'info')
  45. const filePath = resolve(__dirname, './resources/Basic.vue')
  46. const fileString = readFileSync(filePath, { encoding: 'utf8' })
  47. jestVue.process(fileString, filePath)
  48. try {
  49. expect(info).toHaveBeenCalledWith('\n[vue-jest] Info: no .babelrc found, defaulting to default babel options\n')
  50. } catch (err) {
  51. renameSync(tempPath, babelRcPath)
  52. throw err
  53. }
  54. renameSync(tempPath, babelRcPath)
  55. jest.resetModules()
  56. })
  57. test('uses babelrc in package.json if none in .babelrc', () => {
  58. const babelRcPath = resolve(__dirname, '../.babelrc')
  59. const tempPath = resolve(__dirname, '../.renamed')
  60. const packagePath = resolve(__dirname, '../package.json')
  61. const packageOriginal = readFileSync(packagePath, { encoding: 'utf8' })
  62. writeFileSync(packagePath, '{ "babel": {"presets": ["env"],"plugins": ["istanbul"]}}')
  63. renameSync(babelRcPath, tempPath)
  64. const filePath = resolve(__dirname, './resources/Basic.vue')
  65. const fileString = readFileSync(filePath, { encoding: 'utf8' })
  66. try {
  67. const output = jestVue.process(fileString, filePath)
  68. expect(output.code).toContain('coverageData.hash')
  69. } catch (err) {
  70. renameSync(tempPath, babelRcPath)
  71. writeFileSync(packagePath, packageOriginal)
  72. jest.resetModules()
  73. throw err
  74. }
  75. renameSync(tempPath, babelRcPath)
  76. writeFileSync(packagePath, packageOriginal)
  77. jest.resetModules()
  78. })
  79. test('processes .vue files using .babelrc if it exists in route', () => {
  80. const babelRcPath = resolve(__dirname, '../.babelrc')
  81. const babelRcOriginal = readFileSync(babelRcPath, { encoding: 'utf8' })
  82. writeFileSync(babelRcPath, '{"presets": ["env"],"plugins": ["istanbul"]}')
  83. const filePath = resolve(__dirname, './resources/Basic.vue')
  84. const fileString = readFileSync(filePath, { encoding: 'utf8' })
  85. const output = jestVue.process(fileString, filePath)
  86. writeFileSync(babelRcPath, babelRcOriginal)
  87. // coverageData.hash is added by babel-plugin-istanbul, added to root .babelrc for this test only
  88. expect(output.code).toContain('coverageData.hash')
  89. })
  90. test('generates inline sourcemap', () => {
  91. const filePath = resolve(__dirname, './resources/Basic.vue')
  92. const fileString = readFileSync(filePath, { encoding: 'utf8' })
  93. const output = jestVue.process(fileString, filePath)
  94. expect(output.map).toMatchSnapshot()
  95. })
  96. test('generates inline sourcemap for .vue files using src attributes', () => {
  97. const filePath = resolve(__dirname, './resources/BasicSrc.vue')
  98. const fileString = readFileSync(filePath, { encoding: 'utf8' })
  99. const output = jestVue.process(fileString, filePath)
  100. expect(output.map).toMatchSnapshot()
  101. })