runtimeCompilerOptions.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { createApp } from 'vue'
  2. describe('config.compilerOptions', () => {
  3. test('isCustomElement', () => {
  4. const app = createApp({
  5. template: `<foo/>`,
  6. })
  7. app.config.compilerOptions.isCustomElement = (tag: string) => tag === 'foo'
  8. const root = document.createElement('div')
  9. app.mount(root)
  10. expect(root.innerHTML).toBe('<foo></foo>')
  11. })
  12. test('comments', () => {
  13. const app = createApp({
  14. template: `<div/><!--test--><div/>`,
  15. })
  16. app.config.compilerOptions.comments = true
  17. // the comments option is only relevant in production mode
  18. __DEV__ = false
  19. const root = document.createElement('div')
  20. app.mount(root)
  21. expect(root.innerHTML).toBe('<div></div><!--test--><div></div>')
  22. __DEV__ = true
  23. })
  24. test('whitespace', () => {
  25. const app = createApp({
  26. template: `<div><span/>\n <span/></div>`,
  27. })
  28. app.config.compilerOptions.whitespace = 'preserve'
  29. const root = document.createElement('div')
  30. app.mount(root)
  31. expect(root.firstChild!.childNodes.length).toBe(3)
  32. expect(root.firstChild!.childNodes[1].nodeType).toBe(Node.TEXT_NODE)
  33. })
  34. test('delimiters', () => {
  35. const app = createApp({
  36. data: () => ({ foo: 'hi' }),
  37. template: `[[ foo ]]`,
  38. })
  39. app.config.compilerOptions.delimiters = [`[[`, `]]`]
  40. const root = document.createElement('div')
  41. app.mount(root)
  42. expect(root.textContent).toBe('hi')
  43. })
  44. })
  45. describe('per-component compilerOptions', () => {
  46. test('isCustomElement', () => {
  47. const app = createApp({
  48. template: `<foo/>`,
  49. compilerOptions: {
  50. isCustomElement: (tag: string) => tag === 'foo',
  51. },
  52. })
  53. const root = document.createElement('div')
  54. app.mount(root)
  55. expect(root.innerHTML).toBe('<foo></foo>')
  56. })
  57. test('comments', () => {
  58. const app = createApp({
  59. template: `<div/><!--test--><div/>`,
  60. compilerOptions: {
  61. comments: true,
  62. },
  63. })
  64. app.config.compilerOptions.comments = false
  65. // the comments option is only relevant in production mode
  66. __DEV__ = false
  67. const root = document.createElement('div')
  68. app.mount(root)
  69. expect(root.innerHTML).toBe('<div></div><!--test--><div></div>')
  70. __DEV__ = true
  71. })
  72. test('whitespace', () => {
  73. const app = createApp({
  74. template: `<div><span/>\n <span/></div>`,
  75. compilerOptions: {
  76. whitespace: 'preserve',
  77. },
  78. })
  79. const root = document.createElement('div')
  80. app.mount(root)
  81. expect(root.firstChild!.childNodes.length).toBe(3)
  82. expect(root.firstChild!.childNodes[1].nodeType).toBe(Node.TEXT_NODE)
  83. })
  84. test('delimiters', () => {
  85. const app = createApp({
  86. data: () => ({ foo: 'hi' }),
  87. template: `[[ foo ]]`,
  88. compilerOptions: {
  89. delimiters: [`[[`, `]]`],
  90. },
  91. })
  92. const root = document.createElement('div')
  93. app.mount(root)
  94. expect(root.textContent).toBe('hi')
  95. })
  96. })