defineProps.spec.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. import { BindingTypes } from '@vue/compiler-core'
  2. import { compileSFCScript as compile, assertCode } from '../utils'
  3. describe('defineProps', () => {
  4. test('basic usage', () => {
  5. const { content, bindings } = compile(`
  6. <script setup>
  7. const props = defineProps({
  8. foo: String
  9. })
  10. const bar = 1
  11. </script>
  12. `)
  13. // should generate working code
  14. assertCode(content)
  15. // should analyze bindings
  16. expect(bindings).toStrictEqual({
  17. foo: BindingTypes.PROPS,
  18. bar: BindingTypes.LITERAL_CONST,
  19. props: BindingTypes.SETUP_REACTIVE_CONST
  20. })
  21. // should remove defineOptions import and call
  22. expect(content).not.toMatch('defineProps')
  23. // should generate correct setup signature
  24. expect(content).toMatch(`setup(__props, { expose: __expose }) {`)
  25. // should assign user identifier to it
  26. expect(content).toMatch(`const props = __props`)
  27. // should include context options in default export
  28. expect(content).toMatch(`export default {
  29. props: {
  30. foo: String
  31. },`)
  32. })
  33. test('w/ external definition', () => {
  34. const { content } = compile(`
  35. <script setup>
  36. import { propsModel } from './props'
  37. const props = defineProps(propsModel)
  38. </script>
  39. `)
  40. assertCode(content)
  41. expect(content).toMatch(`export default {
  42. props: propsModel,`)
  43. })
  44. // #4764
  45. test('w/ leading code', () => {
  46. const { content } = compile(`
  47. <script setup>import { x } from './x'
  48. const props = defineProps({})
  49. </script>
  50. `)
  51. // props declaration should be inside setup, not moved along with the import
  52. expect(content).not.toMatch(`const props = __props\nimport`)
  53. assertCode(content)
  54. })
  55. test('defineProps w/ runtime options', () => {
  56. const { content } = compile(`
  57. <script setup lang="ts">
  58. const props = defineProps({ foo: String })
  59. </script>
  60. `)
  61. assertCode(content)
  62. expect(content).toMatch(`export default /*#__PURE__*/_defineComponent({
  63. props: { foo: String },
  64. setup(__props, { expose: __expose }) {`)
  65. })
  66. test('w/ type', () => {
  67. const { content, bindings } = compile(`
  68. <script setup lang="ts">
  69. interface Test {}
  70. type Alias = number[]
  71. defineProps<{
  72. string: string
  73. number: number
  74. boolean: boolean
  75. object: object
  76. objectLiteral: { a: number }
  77. fn: (n: number) => void
  78. functionRef: Function
  79. objectRef: Object
  80. dateTime: Date
  81. array: string[]
  82. arrayRef: Array<any>
  83. tuple: [number, number]
  84. set: Set<string>
  85. literal: 'foo'
  86. optional?: any
  87. recordRef: Record<string, null>
  88. interface: Test
  89. alias: Alias
  90. method(): void
  91. symbol: symbol
  92. extract: Extract<1 | 2 | boolean, 2>
  93. exclude: Exclude<1 | 2 | boolean, 2>
  94. uppercase: Uppercase<'foo'>
  95. params: Parameters<(foo: any) => void>
  96. nonNull: NonNullable<string | null>
  97. objectOrFn: {
  98. (): void
  99. foo: string
  100. }
  101. union: string | number
  102. literalUnion: 'foo' | 'bar'
  103. literalUnionNumber: 1 | 2 | 3 | 4 | 5
  104. literalUnionMixed: 'foo' | 1 | boolean
  105. intersection: Test & {}
  106. intersection2: 'foo' & ('foo' | 'bar')
  107. foo: ((item: any) => boolean) | null
  108. unknown: UnknownType
  109. unknownUnion: UnknownType | string
  110. unknownIntersection: UnknownType & Object
  111. unknownUnionWithBoolean: UnknownType | boolean
  112. unknownUnionWithFunction: UnknownType | (() => any)
  113. }>()
  114. </script>`)
  115. assertCode(content)
  116. expect(content).toMatch(`string: { type: String, required: true }`)
  117. expect(content).toMatch(`number: { type: Number, required: true }`)
  118. expect(content).toMatch(`boolean: { type: Boolean, required: true }`)
  119. expect(content).toMatch(`object: { type: Object, required: true }`)
  120. expect(content).toMatch(`objectLiteral: { type: Object, required: true }`)
  121. expect(content).toMatch(`fn: { type: Function, required: true }`)
  122. expect(content).toMatch(`functionRef: { type: Function, required: true }`)
  123. expect(content).toMatch(`objectRef: { type: Object, required: true }`)
  124. expect(content).toMatch(`dateTime: { type: Date, required: true }`)
  125. expect(content).toMatch(`array: { type: Array, required: true }`)
  126. expect(content).toMatch(`arrayRef: { type: Array, required: true }`)
  127. expect(content).toMatch(`tuple: { type: Array, required: true }`)
  128. expect(content).toMatch(`set: { type: Set, required: true }`)
  129. expect(content).toMatch(`literal: { type: String, required: true }`)
  130. expect(content).toMatch(`optional: { type: null, required: false }`)
  131. expect(content).toMatch(`recordRef: { type: Object, required: true }`)
  132. expect(content).toMatch(`interface: { type: Object, required: true }`)
  133. expect(content).toMatch(`alias: { type: Array, required: true }`)
  134. expect(content).toMatch(`method: { type: Function, required: true }`)
  135. expect(content).toMatch(`symbol: { type: Symbol, required: true }`)
  136. expect(content).toMatch(
  137. `objectOrFn: { type: [Function, Object], required: true },`
  138. )
  139. expect(content).toMatch(`extract: { type: Number, required: true }`)
  140. expect(content).toMatch(
  141. `exclude: { type: [Number, Boolean], required: true }`
  142. )
  143. expect(content).toMatch(`uppercase: { type: String, required: true }`)
  144. expect(content).toMatch(`params: { type: Array, required: true }`)
  145. expect(content).toMatch(`nonNull: { type: String, required: true }`)
  146. expect(content).toMatch(`union: { type: [String, Number], required: true }`)
  147. expect(content).toMatch(`literalUnion: { type: String, required: true }`)
  148. expect(content).toMatch(
  149. `literalUnionNumber: { type: Number, required: true }`
  150. )
  151. expect(content).toMatch(
  152. `literalUnionMixed: { type: [String, Number, Boolean], required: true }`
  153. )
  154. expect(content).toMatch(`intersection: { type: Object, required: true }`)
  155. expect(content).toMatch(`intersection2: { type: String, required: true }`)
  156. expect(content).toMatch(`foo: { type: [Function, null], required: true }`)
  157. expect(content).toMatch(`unknown: { type: null, required: true }`)
  158. // uninon containing unknown type: skip check
  159. expect(content).toMatch(`unknownUnion: { type: null, required: true }`)
  160. // intersection containing unknown type: narrow to the known types
  161. expect(content).toMatch(
  162. `unknownIntersection: { type: Object, required: true },`
  163. )
  164. expect(content).toMatch(
  165. `unknownUnionWithBoolean: { type: Boolean, required: true, skipCheck: true },`
  166. )
  167. expect(content).toMatch(
  168. `unknownUnionWithFunction: { type: Function, required: true, skipCheck: true }`
  169. )
  170. expect(bindings).toStrictEqual({
  171. string: BindingTypes.PROPS,
  172. number: BindingTypes.PROPS,
  173. boolean: BindingTypes.PROPS,
  174. object: BindingTypes.PROPS,
  175. objectLiteral: BindingTypes.PROPS,
  176. fn: BindingTypes.PROPS,
  177. functionRef: BindingTypes.PROPS,
  178. objectRef: BindingTypes.PROPS,
  179. dateTime: BindingTypes.PROPS,
  180. array: BindingTypes.PROPS,
  181. arrayRef: BindingTypes.PROPS,
  182. tuple: BindingTypes.PROPS,
  183. set: BindingTypes.PROPS,
  184. literal: BindingTypes.PROPS,
  185. optional: BindingTypes.PROPS,
  186. recordRef: BindingTypes.PROPS,
  187. interface: BindingTypes.PROPS,
  188. alias: BindingTypes.PROPS,
  189. method: BindingTypes.PROPS,
  190. symbol: BindingTypes.PROPS,
  191. objectOrFn: BindingTypes.PROPS,
  192. extract: BindingTypes.PROPS,
  193. exclude: BindingTypes.PROPS,
  194. union: BindingTypes.PROPS,
  195. literalUnion: BindingTypes.PROPS,
  196. literalUnionNumber: BindingTypes.PROPS,
  197. literalUnionMixed: BindingTypes.PROPS,
  198. intersection: BindingTypes.PROPS,
  199. intersection2: BindingTypes.PROPS,
  200. foo: BindingTypes.PROPS,
  201. uppercase: BindingTypes.PROPS,
  202. params: BindingTypes.PROPS,
  203. nonNull: BindingTypes.PROPS,
  204. unknown: BindingTypes.PROPS,
  205. unknownUnion: BindingTypes.PROPS,
  206. unknownIntersection: BindingTypes.PROPS,
  207. unknownUnionWithBoolean: BindingTypes.PROPS,
  208. unknownUnionWithFunction: BindingTypes.PROPS
  209. })
  210. })
  211. test('w/ interface', () => {
  212. const { content, bindings } = compile(`
  213. <script setup lang="ts">
  214. interface Props { x?: number }
  215. defineProps<Props>()
  216. </script>
  217. `)
  218. assertCode(content)
  219. expect(content).toMatch(`x: { type: Number, required: false }`)
  220. expect(bindings).toStrictEqual({
  221. x: BindingTypes.PROPS
  222. })
  223. })
  224. test('w/ extends interface', () => {
  225. const { content, bindings } = compile(`
  226. <script lang="ts">
  227. interface Foo { x?: number }
  228. </script>
  229. <script setup lang="ts">
  230. interface Bar extends Foo { y?: number }
  231. interface Props extends Bar {
  232. z: number
  233. y: string
  234. }
  235. defineProps<Props>()
  236. </script>
  237. `)
  238. assertCode(content)
  239. expect(content).toMatch(`z: { type: Number, required: true }`)
  240. expect(content).toMatch(`y: { type: String, required: true }`)
  241. expect(content).toMatch(`x: { type: Number, required: false }`)
  242. expect(bindings).toStrictEqual({
  243. x: BindingTypes.PROPS,
  244. y: BindingTypes.PROPS,
  245. z: BindingTypes.PROPS
  246. })
  247. })
  248. test('w/ exported interface', () => {
  249. const { content, bindings } = compile(`
  250. <script setup lang="ts">
  251. export interface Props { x?: number }
  252. defineProps<Props>()
  253. </script>
  254. `)
  255. assertCode(content)
  256. expect(content).toMatch(`x: { type: Number, required: false }`)
  257. expect(bindings).toStrictEqual({
  258. x: BindingTypes.PROPS
  259. })
  260. })
  261. test('w/ exported interface in normal script', () => {
  262. const { content, bindings } = compile(`
  263. <script lang="ts">
  264. export interface Props { x?: number }
  265. </script>
  266. <script setup lang="ts">
  267. defineProps<Props>()
  268. </script>
  269. `)
  270. assertCode(content)
  271. expect(content).toMatch(`x: { type: Number, required: false }`)
  272. expect(bindings).toStrictEqual({
  273. x: BindingTypes.PROPS
  274. })
  275. })
  276. test('w/ type alias', () => {
  277. const { content, bindings } = compile(`
  278. <script setup lang="ts">
  279. type Props = { x?: number }
  280. defineProps<Props>()
  281. </script>
  282. `)
  283. assertCode(content)
  284. expect(content).toMatch(`x: { type: Number, required: false }`)
  285. expect(bindings).toStrictEqual({
  286. x: BindingTypes.PROPS
  287. })
  288. })
  289. test('w/ exported type alias', () => {
  290. const { content, bindings } = compile(`
  291. <script setup lang="ts">
  292. export type Props = { x?: number }
  293. defineProps<Props>()
  294. </script>
  295. `)
  296. assertCode(content)
  297. expect(content).toMatch(`x: { type: Number, required: false }`)
  298. expect(bindings).toStrictEqual({
  299. x: BindingTypes.PROPS
  300. })
  301. })
  302. test('w/ TS assertion', () => {
  303. const { content, bindings } = compile(`
  304. <script setup lang="ts">
  305. defineProps(['foo'])! as any
  306. </script>
  307. `)
  308. expect(content).toMatch(`props: ['foo']`)
  309. assertCode(content)
  310. expect(bindings).toStrictEqual({
  311. foo: BindingTypes.PROPS
  312. })
  313. })
  314. test('withDefaults (static)', () => {
  315. const { content, bindings } = compile(`
  316. <script setup lang="ts">
  317. const props = withDefaults(defineProps<{
  318. foo?: string
  319. bar?: number;
  320. baz: boolean;
  321. qux?(): number;
  322. quux?(): void
  323. quuxx?: Promise<string>;
  324. fred?: string
  325. }>(), {
  326. foo: 'hi',
  327. qux() { return 1 },
  328. ['quux']() { },
  329. async quuxx() { return await Promise.resolve('hi') },
  330. get fred() { return 'fred' }
  331. })
  332. </script>
  333. `)
  334. assertCode(content)
  335. expect(content).toMatch(
  336. `foo: { type: String, required: false, default: 'hi' }`
  337. )
  338. expect(content).toMatch(`bar: { type: Number, required: false }`)
  339. expect(content).toMatch(`baz: { type: Boolean, required: true }`)
  340. expect(content).toMatch(
  341. `qux: { type: Function, required: false, default() { return 1 } }`
  342. )
  343. expect(content).toMatch(
  344. `quux: { type: Function, required: false, default() { } }`
  345. )
  346. expect(content).toMatch(
  347. `quuxx: { type: Promise, required: false, async default() { return await Promise.resolve('hi') } }`
  348. )
  349. expect(content).toMatch(
  350. `fred: { type: String, required: false, get default() { return 'fred' } }`
  351. )
  352. expect(content).toMatch(`const props = __props`)
  353. expect(bindings).toStrictEqual({
  354. foo: BindingTypes.PROPS,
  355. bar: BindingTypes.PROPS,
  356. baz: BindingTypes.PROPS,
  357. qux: BindingTypes.PROPS,
  358. quux: BindingTypes.PROPS,
  359. quuxx: BindingTypes.PROPS,
  360. fred: BindingTypes.PROPS,
  361. props: BindingTypes.SETUP_CONST
  362. })
  363. })
  364. test('withDefaults (static) + normal script', () => {
  365. const { content } = compile(`
  366. <script lang="ts">
  367. interface Props {
  368. a?: string;
  369. }
  370. </script>
  371. <script setup lang="ts">
  372. const props = withDefaults(defineProps<Props>(), {
  373. a: "a",
  374. });
  375. </script>
  376. `)
  377. assertCode(content)
  378. })
  379. // #7111
  380. test('withDefaults (static) w/ production mode', () => {
  381. const { content } = compile(
  382. `
  383. <script setup lang="ts">
  384. const props = withDefaults(defineProps<{
  385. foo: () => void
  386. bar: boolean
  387. baz: boolean | (() => void)
  388. qux: string | number
  389. }>(), {
  390. baz: true,
  391. qux: 'hi'
  392. })
  393. </script>
  394. `,
  395. { isProd: true }
  396. )
  397. assertCode(content)
  398. expect(content).toMatch(`const props = __props`)
  399. // foo has no default value, the Function can be dropped
  400. expect(content).toMatch(`foo: {}`)
  401. expect(content).toMatch(`bar: { type: Boolean }`)
  402. expect(content).toMatch(`baz: { type: [Boolean, Function], default: true }`)
  403. expect(content).toMatch(`qux: { default: 'hi' }`)
  404. })
  405. test('withDefaults (dynamic)', () => {
  406. const { content } = compile(`
  407. <script setup lang="ts">
  408. import { defaults } from './foo'
  409. const props = withDefaults(defineProps<{
  410. foo?: string
  411. bar?: number
  412. baz: boolean
  413. }>(), { ...defaults })
  414. </script>
  415. `)
  416. assertCode(content)
  417. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  418. expect(content).toMatch(
  419. `
  420. _mergeDefaults({
  421. foo: { type: String, required: false },
  422. bar: { type: Number, required: false },
  423. baz: { type: Boolean, required: true }
  424. }, { ...defaults })`.trim()
  425. )
  426. })
  427. test('withDefaults (reference)', () => {
  428. const { content } = compile(`
  429. <script setup lang="ts">
  430. import { defaults } from './foo'
  431. const props = withDefaults(defineProps<{
  432. foo?: string
  433. bar?: number
  434. baz: boolean
  435. }>(), defaults)
  436. </script>
  437. `)
  438. assertCode(content)
  439. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  440. expect(content).toMatch(
  441. `
  442. _mergeDefaults({
  443. foo: { type: String, required: false },
  444. bar: { type: Number, required: false },
  445. baz: { type: Boolean, required: true }
  446. }, defaults)`.trim()
  447. )
  448. })
  449. // #7111
  450. test('withDefaults (dynamic) w/ production mode', () => {
  451. const { content } = compile(
  452. `
  453. <script setup lang="ts">
  454. import { defaults } from './foo'
  455. const props = withDefaults(defineProps<{
  456. foo: () => void
  457. bar: boolean
  458. baz: boolean | (() => void)
  459. qux: string | number
  460. }>(), { ...defaults })
  461. </script>
  462. `,
  463. { isProd: true }
  464. )
  465. assertCode(content)
  466. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  467. expect(content).toMatch(
  468. `
  469. _mergeDefaults({
  470. foo: { type: Function },
  471. bar: { type: Boolean },
  472. baz: { type: [Boolean, Function] },
  473. qux: {}
  474. }, { ...defaults })`.trim()
  475. )
  476. })
  477. test('withDefaults w/ dynamic object method', () => {
  478. const { content } = compile(`
  479. <script setup lang="ts">
  480. const props = withDefaults(defineProps<{
  481. foo?: () => 'string'
  482. }>(), {
  483. ['fo' + 'o']() { return 'foo' }
  484. })
  485. </script>
  486. `)
  487. assertCode(content)
  488. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  489. expect(content).toMatch(
  490. `
  491. _mergeDefaults({
  492. foo: { type: Function, required: false }
  493. }, {
  494. ['fo' + 'o']() { return 'foo' }
  495. })`.trim()
  496. )
  497. })
  498. test('runtime inference for Enum', () => {
  499. expect(
  500. compile(
  501. `<script setup lang="ts">
  502. const enum Foo { A = 123 }
  503. defineProps<{
  504. foo: Foo
  505. }>()
  506. </script>`,
  507. { hoistStatic: true }
  508. ).content
  509. ).toMatch(`foo: { type: Number`)
  510. expect(
  511. compile(
  512. `<script setup lang="ts">
  513. const enum Foo { A = '123' }
  514. defineProps<{
  515. foo: Foo
  516. }>()
  517. </script>`,
  518. { hoistStatic: true }
  519. ).content
  520. ).toMatch(`foo: { type: String`)
  521. expect(
  522. compile(
  523. `<script setup lang="ts">
  524. const enum Foo { A = '123', B = 123 }
  525. defineProps<{
  526. foo: Foo
  527. }>()
  528. </script>`,
  529. { hoistStatic: true }
  530. ).content
  531. ).toMatch(`foo: { type: [String, Number]`)
  532. expect(
  533. compile(
  534. `<script setup lang="ts">
  535. const enum Foo { A, B }
  536. defineProps<{
  537. foo: Foo
  538. }>()
  539. </script>`,
  540. { hoistStatic: true }
  541. ).content
  542. ).toMatch(`foo: { type: Number`)
  543. })
  544. // #8148
  545. test('should not override local bindings', () => {
  546. const { bindings } = compile(`
  547. <script setup lang="ts">
  548. import { computed } from 'vue'
  549. defineProps<{ bar: string }>()
  550. const bar = computed(() => 1)
  551. </script>
  552. `)
  553. expect(bindings).toStrictEqual({
  554. bar: BindingTypes.SETUP_REF,
  555. computed: BindingTypes.SETUP_CONST
  556. })
  557. })
  558. // #8289
  559. test('destructure without enabling reactive destructure', () => {
  560. const { content } = compile(
  561. `<script setup lang="ts">
  562. const { foo } = defineProps<{
  563. foo: Foo
  564. }>()
  565. </script>`
  566. )
  567. expect(content).toMatch(`const { foo } = __props`)
  568. assertCode(content)
  569. })
  570. describe('errors', () => {
  571. test('w/ both type and non-type args', () => {
  572. expect(() => {
  573. compile(`<script setup lang="ts">
  574. defineProps<{}>({})
  575. </script>`)
  576. }).toThrow(`cannot accept both type and non-type arguments`)
  577. })
  578. })
  579. })