apiCreateApp.spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. import { vi } from 'vitest'
  2. import {
  3. createApp,
  4. h,
  5. nodeOps,
  6. serializeInner,
  7. provide,
  8. inject,
  9. resolveComponent,
  10. resolveDirective,
  11. withDirectives,
  12. Plugin,
  13. ref,
  14. getCurrentInstance,
  15. defineComponent
  16. } from '@vue/runtime-test'
  17. describe('api: createApp', () => {
  18. test('mount', () => {
  19. const Comp = defineComponent({
  20. props: {
  21. count: {
  22. default: 0
  23. }
  24. },
  25. setup(props) {
  26. return () => props.count
  27. }
  28. })
  29. const root1 = nodeOps.createElement('div')
  30. createApp(Comp).mount(root1)
  31. expect(serializeInner(root1)).toBe(`0`)
  32. //#5571 mount multiple apps to the same host element
  33. createApp(Comp).mount(root1)
  34. expect(
  35. `There is already an app instance mounted on the host container`
  36. ).toHaveBeenWarned()
  37. // mount with props
  38. const root2 = nodeOps.createElement('div')
  39. const app2 = createApp(Comp, { count: 1 })
  40. app2.mount(root2)
  41. expect(serializeInner(root2)).toBe(`1`)
  42. // remount warning
  43. const root3 = nodeOps.createElement('div')
  44. app2.mount(root3)
  45. expect(serializeInner(root3)).toBe(``)
  46. expect(`already been mounted`).toHaveBeenWarned()
  47. })
  48. test('unmount', () => {
  49. const Comp = defineComponent({
  50. props: {
  51. count: {
  52. default: 0
  53. }
  54. },
  55. setup(props) {
  56. return () => props.count
  57. }
  58. })
  59. const root = nodeOps.createElement('div')
  60. const app = createApp(Comp)
  61. // warning
  62. app.unmount()
  63. expect(`that is not mounted`).toHaveBeenWarned()
  64. app.mount(root)
  65. app.unmount()
  66. expect(serializeInner(root)).toBe(``)
  67. })
  68. test('provide', () => {
  69. const Root = {
  70. setup() {
  71. // test override
  72. provide('foo', 3)
  73. return () => h(Child)
  74. }
  75. }
  76. const Child = {
  77. setup() {
  78. const foo = inject('foo')
  79. const bar = inject('bar')
  80. try {
  81. inject('__proto__')
  82. } catch (e: any) {}
  83. return () => `${foo},${bar}`
  84. }
  85. }
  86. const app = createApp(Root)
  87. app.provide('foo', 1)
  88. app.provide('bar', 2)
  89. const root = nodeOps.createElement('div')
  90. app.mount(root)
  91. expect(serializeInner(root)).toBe(`3,2`)
  92. expect('[Vue warn]: injection "__proto__" not found.').toHaveBeenWarned()
  93. const app2 = createApp(Root)
  94. app2.provide('bar', 1)
  95. app2.provide('bar', 2)
  96. expect(`App already provides property with key "bar".`).toHaveBeenWarned()
  97. })
  98. test('runWithContext', () => {
  99. const app = createApp({
  100. setup() {
  101. provide('foo', 'should not be seen')
  102. return () => h('div')
  103. }
  104. })
  105. app.provide('foo', 1)
  106. expect(app.runWithContext(() => inject('foo'))).toBe(1)
  107. // ensure the context is restored
  108. inject('foo')
  109. expect('inject() can only be used inside setup').toHaveBeenWarned()
  110. })
  111. test('component', () => {
  112. const Root = {
  113. // local override
  114. components: {
  115. BarBaz: () => 'barbaz-local!'
  116. },
  117. setup() {
  118. // resolve in setup
  119. const FooBar = resolveComponent('foo-bar') as any
  120. return () => {
  121. // resolve in render
  122. const BarBaz = resolveComponent('bar-baz') as any
  123. return h('div', [h(FooBar), h(BarBaz)])
  124. }
  125. }
  126. }
  127. const app = createApp(Root)
  128. const FooBar = () => 'foobar!'
  129. app.component('FooBar', FooBar)
  130. expect(app.component('FooBar')).toBe(FooBar)
  131. app.component('BarBaz', () => 'barbaz!')
  132. app.component('BarBaz', () => 'barbaz!')
  133. expect(
  134. 'Component "BarBaz" has already been registered in target app.'
  135. ).toHaveBeenWarnedTimes(1)
  136. const root = nodeOps.createElement('div')
  137. app.mount(root)
  138. expect(serializeInner(root)).toBe(`<div>foobar!barbaz-local!</div>`)
  139. })
  140. test('directive', () => {
  141. const spy1 = vi.fn()
  142. const spy2 = vi.fn()
  143. const spy3 = vi.fn()
  144. const Root = {
  145. // local override
  146. directives: {
  147. BarBaz: { mounted: spy3 }
  148. },
  149. setup() {
  150. // resolve in setup
  151. const FooBar = resolveDirective('foo-bar')!
  152. return () => {
  153. // resolve in render
  154. const BarBaz = resolveDirective('bar-baz')!
  155. return withDirectives(h('div'), [[FooBar], [BarBaz]])
  156. }
  157. }
  158. }
  159. const app = createApp(Root)
  160. const FooBar = { mounted: spy1 }
  161. app.directive('FooBar', FooBar)
  162. expect(app.directive('FooBar')).toBe(FooBar)
  163. app.directive('BarBaz', {
  164. mounted: spy2
  165. })
  166. app.directive('BarBaz', {
  167. mounted: spy2
  168. })
  169. expect(
  170. 'Directive "BarBaz" has already been registered in target app.'
  171. ).toHaveBeenWarnedTimes(1)
  172. const root = nodeOps.createElement('div')
  173. app.mount(root)
  174. expect(spy1).toHaveBeenCalled()
  175. expect(spy2).not.toHaveBeenCalled()
  176. expect(spy3).toHaveBeenCalled()
  177. app.directive('bind', FooBar)
  178. expect(
  179. `Do not use built-in directive ids as custom directive id: bind`
  180. ).toHaveBeenWarned()
  181. })
  182. test('mixin', () => {
  183. const calls: string[] = []
  184. const mixinA = {
  185. data() {
  186. return {
  187. a: 1
  188. }
  189. },
  190. created(this: any) {
  191. calls.push('mixinA created')
  192. expect(this.a).toBe(1)
  193. expect(this.b).toBe(2)
  194. expect(this.c).toBe(3)
  195. },
  196. mounted() {
  197. calls.push('mixinA mounted')
  198. }
  199. }
  200. const mixinB = {
  201. name: 'mixinB',
  202. data() {
  203. return {
  204. b: 2
  205. }
  206. },
  207. created(this: any) {
  208. calls.push('mixinB created')
  209. expect(this.a).toBe(1)
  210. expect(this.b).toBe(2)
  211. expect(this.c).toBe(3)
  212. },
  213. mounted() {
  214. calls.push('mixinB mounted')
  215. }
  216. }
  217. const Comp = {
  218. data() {
  219. return {
  220. c: 3
  221. }
  222. },
  223. created(this: any) {
  224. calls.push('comp created')
  225. expect(this.a).toBe(1)
  226. expect(this.b).toBe(2)
  227. expect(this.c).toBe(3)
  228. },
  229. mounted() {
  230. calls.push('comp mounted')
  231. },
  232. render(this: any) {
  233. return `${this.a}${this.b}${this.c}`
  234. }
  235. }
  236. const app = createApp(Comp)
  237. app.mixin(mixinA)
  238. app.mixin(mixinB)
  239. app.mixin(mixinA)
  240. app.mixin(mixinB)
  241. expect(
  242. 'Mixin has already been applied to target app'
  243. ).toHaveBeenWarnedTimes(2)
  244. expect(
  245. 'Mixin has already been applied to target app: mixinB'
  246. ).toHaveBeenWarnedTimes(1)
  247. const root = nodeOps.createElement('div')
  248. app.mount(root)
  249. expect(serializeInner(root)).toBe(`123`)
  250. expect(calls).toEqual([
  251. 'mixinA created',
  252. 'mixinB created',
  253. 'comp created',
  254. 'mixinA mounted',
  255. 'mixinB mounted',
  256. 'comp mounted'
  257. ])
  258. })
  259. test('use', () => {
  260. const PluginA: Plugin = app => app.provide('foo', 1)
  261. const PluginB: Plugin = {
  262. install: (app, arg1, arg2) => app.provide('bar', arg1 + arg2)
  263. }
  264. class PluginC {
  265. someProperty = {}
  266. static install() {
  267. app.provide('baz', 2)
  268. }
  269. }
  270. const PluginD: any = undefined
  271. const Root = {
  272. setup() {
  273. const foo = inject('foo')
  274. const bar = inject('bar')
  275. return () => `${foo},${bar}`
  276. }
  277. }
  278. const app = createApp(Root)
  279. app.use(PluginA)
  280. app.use(PluginB, 1, 1)
  281. app.use(PluginC)
  282. const root = nodeOps.createElement('div')
  283. app.mount(root)
  284. expect(serializeInner(root)).toBe(`1,2`)
  285. app.use(PluginA)
  286. expect(
  287. `Plugin has already been applied to target app`
  288. ).toHaveBeenWarnedTimes(1)
  289. app.use(PluginD)
  290. expect(
  291. `A plugin must either be a function or an object with an "install" ` +
  292. `function.`
  293. ).toHaveBeenWarnedTimes(1)
  294. })
  295. test('config.errorHandler', () => {
  296. const error = new Error()
  297. const count = ref(0)
  298. const handler = vi.fn((err, instance, info) => {
  299. expect(err).toBe(error)
  300. expect((instance as any).count).toBe(count.value)
  301. expect(info).toBe(`render function`)
  302. })
  303. const Root = {
  304. setup() {
  305. const count = ref(0)
  306. return {
  307. count
  308. }
  309. },
  310. render() {
  311. throw error
  312. }
  313. }
  314. const app = createApp(Root)
  315. app.config.errorHandler = handler
  316. app.mount(nodeOps.createElement('div'))
  317. expect(handler).toHaveBeenCalled()
  318. })
  319. test('config.warnHandler', () => {
  320. let ctx: any
  321. const handler = vi.fn((msg, instance, trace) => {
  322. expect(msg).toMatch(`Component is missing template or render function`)
  323. expect(instance).toBe(ctx.proxy)
  324. expect(trace).toMatch(`Hello`)
  325. })
  326. const Root = {
  327. name: 'Hello',
  328. setup() {
  329. ctx = getCurrentInstance()
  330. }
  331. }
  332. const app = createApp(Root)
  333. app.config.warnHandler = handler
  334. app.mount(nodeOps.createElement('div'))
  335. expect(handler).toHaveBeenCalledTimes(1)
  336. })
  337. describe('config.isNativeTag', () => {
  338. const isNativeTag = vi.fn(tag => tag === 'div')
  339. test('Component.name', () => {
  340. const Root = {
  341. name: 'div',
  342. render() {
  343. return null
  344. }
  345. }
  346. const app = createApp(Root)
  347. Object.defineProperty(app.config, 'isNativeTag', {
  348. value: isNativeTag,
  349. writable: false
  350. })
  351. app.mount(nodeOps.createElement('div'))
  352. expect(
  353. `Do not use built-in or reserved HTML elements as component id: div`
  354. ).toHaveBeenWarned()
  355. })
  356. test('Component.components', () => {
  357. const Root = {
  358. components: {
  359. div: () => 'div'
  360. },
  361. render() {
  362. return null
  363. }
  364. }
  365. const app = createApp(Root)
  366. Object.defineProperty(app.config, 'isNativeTag', {
  367. value: isNativeTag,
  368. writable: false
  369. })
  370. app.mount(nodeOps.createElement('div'))
  371. expect(
  372. `Do not use built-in or reserved HTML elements as component id: div`
  373. ).toHaveBeenWarned()
  374. })
  375. test('Component.directives', () => {
  376. const Root = {
  377. directives: {
  378. bind: () => {}
  379. },
  380. render() {
  381. return null
  382. }
  383. }
  384. const app = createApp(Root)
  385. Object.defineProperty(app.config, 'isNativeTag', {
  386. value: isNativeTag,
  387. writable: false
  388. })
  389. app.mount(nodeOps.createElement('div'))
  390. expect(
  391. `Do not use built-in directive ids as custom directive id: bind`
  392. ).toHaveBeenWarned()
  393. })
  394. test('register using app.component', () => {
  395. const app = createApp({
  396. render() {}
  397. })
  398. Object.defineProperty(app.config, 'isNativeTag', {
  399. value: isNativeTag,
  400. writable: false
  401. })
  402. app.component('div', () => 'div')
  403. app.mount(nodeOps.createElement('div'))
  404. expect(
  405. `Do not use built-in or reserved HTML elements as component id: div`
  406. ).toHaveBeenWarned()
  407. })
  408. })
  409. test('config.optionMergeStrategies', () => {
  410. let merged: string
  411. const App = defineComponent({
  412. render() {},
  413. mixins: [{ foo: 'mixin' }],
  414. extends: { foo: 'extends' },
  415. foo: 'local',
  416. beforeCreate() {
  417. merged = this.$options.foo
  418. }
  419. })
  420. const app = createApp(App)
  421. app.mixin({
  422. foo: 'global'
  423. })
  424. app.config.optionMergeStrategies.foo = (a, b) => (a ? `${a},` : ``) + b
  425. app.mount(nodeOps.createElement('div'))
  426. expect(merged!).toBe('global,extends,mixin,local')
  427. })
  428. test('config.globalProperties', () => {
  429. const app = createApp({
  430. render() {
  431. return this.foo
  432. }
  433. })
  434. app.config.globalProperties.foo = 'hello'
  435. const root = nodeOps.createElement('div')
  436. app.mount(root)
  437. expect(serializeInner(root)).toBe('hello')
  438. })
  439. test('return property "_" should not overwrite "ctx._", __isScriptSetup: false', () => {
  440. const Comp = defineComponent({
  441. setup() {
  442. return {
  443. _: ref(0) // return property "_" should not overwrite "ctx._"
  444. }
  445. },
  446. render() {
  447. return h('input', {
  448. ref: 'input'
  449. })
  450. }
  451. })
  452. const root1 = nodeOps.createElement('div')
  453. createApp(Comp).mount(root1)
  454. expect(
  455. `setup() return property "_" should not start with "$" or "_" which are reserved prefixes for Vue internals.`
  456. ).toHaveBeenWarned()
  457. })
  458. test('return property "_" should not overwrite "ctx._", __isScriptSetup: true', () => {
  459. const Comp = defineComponent({
  460. setup() {
  461. return {
  462. _: ref(0), // return property "_" should not overwrite "ctx._"
  463. __isScriptSetup: true // mock __isScriptSetup = true
  464. }
  465. },
  466. render() {
  467. return h('input', {
  468. ref: 'input'
  469. })
  470. }
  471. })
  472. const root1 = nodeOps.createElement('div')
  473. const app = createApp(Comp).mount(root1)
  474. // trigger
  475. app.$refs.input
  476. expect(
  477. `TypeError: Cannot read property '__isScriptSetup' of undefined`
  478. ).not.toHaveBeenWarned()
  479. })
  480. // config.compilerOptions is tested in packages/vue since it is only
  481. // supported in the full build.
  482. })