defineOptions.spec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { assertCode, compileSFCScript as compile } from '../utils'
  2. describe('defineOptions()', () => {
  3. test('basic usage', () => {
  4. const { content } = compile(`
  5. <script setup>
  6. defineOptions({ name: 'FooApp' })
  7. </script>
  8. `)
  9. assertCode(content)
  10. // should remove defineOptions import and call
  11. expect(content).not.toMatch('defineOptions')
  12. // should include context options in default export
  13. expect(content).toMatch(
  14. `export default /*@__PURE__*/Object.assign({ name: 'FooApp' }, `,
  15. )
  16. })
  17. test('empty argument', () => {
  18. const { content } = compile(`
  19. <script setup>
  20. defineOptions()
  21. </script>
  22. `)
  23. assertCode(content)
  24. expect(content).toMatch(`export default {`)
  25. // should remove defineOptions import and call
  26. expect(content).not.toMatch('defineOptions')
  27. })
  28. it('should emit an error with two defineOptions', () => {
  29. expect(() =>
  30. compile(`
  31. <script setup>
  32. defineOptions({ name: 'FooApp' })
  33. defineOptions({ name: 'BarApp' })
  34. </script>
  35. `),
  36. ).toThrowError('[@vue/compiler-sfc] duplicate defineOptions() call')
  37. })
  38. it('should emit an error with props or emits property', () => {
  39. expect(() =>
  40. compile(`
  41. <script setup>
  42. defineOptions({ props: { foo: String } })
  43. </script>
  44. `),
  45. ).toThrowError(
  46. '[@vue/compiler-sfc] defineOptions() cannot be used to declare props. Use defineProps() instead.',
  47. )
  48. expect(() =>
  49. compile(`
  50. <script setup>
  51. defineOptions({ emits: ['update'] })
  52. </script>
  53. `),
  54. ).toThrowError(
  55. '[@vue/compiler-sfc] defineOptions() cannot be used to declare emits. Use defineEmits() instead.',
  56. )
  57. expect(() =>
  58. compile(`
  59. <script setup>
  60. defineOptions({ expose: ['foo'] })
  61. </script>
  62. `),
  63. ).toThrowError(
  64. '[@vue/compiler-sfc] defineOptions() cannot be used to declare expose. Use defineExpose() instead.',
  65. )
  66. expect(() =>
  67. compile(`
  68. <script setup>
  69. defineOptions({ slots: ['foo'] })
  70. </script>
  71. `),
  72. ).toThrowError(
  73. '[@vue/compiler-sfc] defineOptions() cannot be used to declare slots. Use defineSlots() instead.',
  74. )
  75. })
  76. it('should emit an error with type generic', () => {
  77. expect(() =>
  78. compile(`
  79. <script setup lang="ts">
  80. defineOptions<{ name: 'FooApp' }>()
  81. </script>
  82. `),
  83. ).toThrowError(
  84. '[@vue/compiler-sfc] defineOptions() cannot accept type arguments',
  85. )
  86. })
  87. it('should emit an error with type assertion', () => {
  88. expect(() =>
  89. compile(`
  90. <script setup lang="ts">
  91. defineOptions({ props: [] } as any)
  92. </script>
  93. `),
  94. ).toThrowError(
  95. '[@vue/compiler-sfc] defineOptions() cannot be used to declare props. Use defineProps() instead.',
  96. )
  97. })
  98. it('should emit an error with declaring props/emits/slots/expose', () => {
  99. expect(() =>
  100. compile(`
  101. <script setup>
  102. defineOptions({ props: ['foo'] })
  103. </script>
  104. `),
  105. ).toThrowError(
  106. '[@vue/compiler-sfc] defineOptions() cannot be used to declare props. Use defineProps() instead',
  107. )
  108. expect(() =>
  109. compile(`
  110. <script setup>
  111. defineOptions({ emits: ['update'] })
  112. </script>
  113. `),
  114. ).toThrowError(
  115. '[@vue/compiler-sfc] defineOptions() cannot be used to declare emits. Use defineEmits() instead',
  116. )
  117. expect(() =>
  118. compile(`
  119. <script setup>
  120. defineOptions({ expose: ['foo'] })
  121. </script>
  122. `),
  123. ).toThrowError(
  124. '[@vue/compiler-sfc] defineOptions() cannot be used to declare expose. Use defineExpose() instead',
  125. )
  126. expect(() =>
  127. compile(`
  128. <script setup lang="ts">
  129. defineOptions({ slots: Object })
  130. </script>
  131. `),
  132. ).toThrowError(
  133. '[@vue/compiler-sfc] defineOptions() cannot be used to declare slots. Use defineSlots() instead',
  134. )
  135. })
  136. })