coffee.spec.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { shallow, mount } from 'vue-test-utils'
  2. import Coffee from './resources/Coffee.vue'
  3. import CoffeeScript from './resources/CoffeeScript.vue'
  4. import CoffeeES6 from './resources/CoffeeES6.vue'
  5. import CoffeeScriptES6 from './resources/CoffeeScriptES6.vue'
  6. import jestVue from '../vue-jest'
  7. import { resolve } from 'path'
  8. import {
  9. readFileSync,
  10. writeFileSync,
  11. renameSync
  12. } from 'fs'
  13. import clearModule from 'clear-module'
  14. import cache from '../lib/cache'
  15. describe('Test CoffeeScript - coffee.spec.js', () => {
  16. beforeEach(() => {
  17. cache.flushAll()
  18. clearModule.all()
  19. })
  20. test('processes .vue file with lang set to coffee', () => {
  21. shallow(Coffee)
  22. })
  23. test('processes .vue file with lang set to coffeescript', () => {
  24. shallow(CoffeeScript)
  25. })
  26. test('processes .vue file with lang set to coffee (ES6)', () => {
  27. shallow(CoffeeES6)
  28. })
  29. test('processes .vue file with lang set to coffeescript (ES6)', () => {
  30. shallow(CoffeeScriptES6)
  31. })
  32. test('processes .vue file with lang set to coffeescript (ES6)', () => {
  33. const wrapper = mount(CoffeeScriptES6)
  34. expect(typeof wrapper).toBe('object')
  35. })
  36. test('processes .vue files with lang set to coffeescript using .babelrc if there is no .babelrc', () => {
  37. const babelRcPath = resolve(__dirname, '../.babelrc')
  38. const tempPath = resolve(__dirname, '../.renamed')
  39. renameSync(babelRcPath, tempPath)
  40. const filePath = resolve(__dirname, './resources/CoffeeScriptES6.vue')
  41. const fileString = readFileSync(filePath, { encoding: 'utf8' })
  42. try {
  43. jestVue.process(fileString, filePath)
  44. } catch (err) {
  45. renameSync(tempPath, babelRcPath)
  46. throw err
  47. }
  48. renameSync(tempPath, babelRcPath)
  49. })
  50. test('processes .vue files with lang set to coffeescript, uses babelrc in package.json if none in .babelrc', () => {
  51. const babelRcPath = resolve(__dirname, '../.babelrc')
  52. const tempPath = resolve(__dirname, '../.renamed')
  53. const packagePath = resolve(__dirname, '../package.json')
  54. const packageOriginal = readFileSync(packagePath, { encoding: 'utf8' })
  55. writeFileSync(packagePath, '{ "babel": {"presets": ["env"],"plugins": ["istanbul"]}}')
  56. renameSync(babelRcPath, tempPath)
  57. const filePath = resolve(__dirname, './resources/CoffeeScriptES6.vue')
  58. const fileString = readFileSync(filePath, { encoding: 'utf8' })
  59. try {
  60. const output = jestVue.process(fileString, filePath)
  61. expect(output.code).toContain('coverageData.hash')
  62. } catch (err) {
  63. renameSync(tempPath, babelRcPath)
  64. writeFileSync(packagePath, packageOriginal)
  65. jest.resetModules()
  66. throw err
  67. }
  68. renameSync(tempPath, babelRcPath)
  69. writeFileSync(packagePath, packageOriginal)
  70. jest.resetModules()
  71. })
  72. test('processes .vue files with lang set to coffeescript using .babelrc if it exists in route', () => {
  73. const babelRcPath = resolve(__dirname, '../.babelrc')
  74. const babelRcOriginal = readFileSync(babelRcPath, { encoding: 'utf8' })
  75. writeFileSync(babelRcPath, '{"presets": ["env"],"plugins": ["istanbul"]}')
  76. const filePath = resolve(__dirname, './resources/CoffeeScriptES6.vue')
  77. const fileString = readFileSync(filePath, { encoding: 'utf8' })
  78. const output = jestVue.process(fileString, filePath)
  79. writeFileSync(babelRcPath, babelRcOriginal)
  80. // coverageData.hash is added by babel-plugin-istanbul, added to root .babelrc for this test only
  81. expect(output.code).toContain('coverageData.hash')
  82. })
  83. })