apiCreateApp.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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('component', () => {
  99. const Root = {
  100. // local override
  101. components: {
  102. BarBaz: () => 'barbaz-local!'
  103. },
  104. setup() {
  105. // resolve in setup
  106. const FooBar = resolveComponent('foo-bar') as any
  107. return () => {
  108. // resolve in render
  109. const BarBaz = resolveComponent('bar-baz') as any
  110. return h('div', [h(FooBar), h(BarBaz)])
  111. }
  112. }
  113. }
  114. const app = createApp(Root)
  115. const FooBar = () => 'foobar!'
  116. app.component('FooBar', FooBar)
  117. expect(app.component('FooBar')).toBe(FooBar)
  118. app.component('BarBaz', () => 'barbaz!')
  119. app.component('BarBaz', () => 'barbaz!')
  120. expect(
  121. 'Component "BarBaz" has already been registered in target app.'
  122. ).toHaveBeenWarnedTimes(1)
  123. const root = nodeOps.createElement('div')
  124. app.mount(root)
  125. expect(serializeInner(root)).toBe(`<div>foobar!barbaz-local!</div>`)
  126. })
  127. test('directive', () => {
  128. const spy1 = vi.fn()
  129. const spy2 = vi.fn()
  130. const spy3 = vi.fn()
  131. const Root = {
  132. // local override
  133. directives: {
  134. BarBaz: { mounted: spy3 }
  135. },
  136. setup() {
  137. // resolve in setup
  138. const FooBar = resolveDirective('foo-bar')!
  139. return () => {
  140. // resolve in render
  141. const BarBaz = resolveDirective('bar-baz')!
  142. return withDirectives(h('div'), [[FooBar], [BarBaz]])
  143. }
  144. }
  145. }
  146. const app = createApp(Root)
  147. const FooBar = { mounted: spy1 }
  148. app.directive('FooBar', FooBar)
  149. expect(app.directive('FooBar')).toBe(FooBar)
  150. app.directive('BarBaz', {
  151. mounted: spy2
  152. })
  153. app.directive('BarBaz', {
  154. mounted: spy2
  155. })
  156. expect(
  157. 'Directive "BarBaz" has already been registered in target app.'
  158. ).toHaveBeenWarnedTimes(1)
  159. const root = nodeOps.createElement('div')
  160. app.mount(root)
  161. expect(spy1).toHaveBeenCalled()
  162. expect(spy2).not.toHaveBeenCalled()
  163. expect(spy3).toHaveBeenCalled()
  164. app.directive('bind', FooBar)
  165. expect(
  166. `Do not use built-in directive ids as custom directive id: bind`
  167. ).toHaveBeenWarned()
  168. })
  169. test('mixin', () => {
  170. const calls: string[] = []
  171. const mixinA = {
  172. data() {
  173. return {
  174. a: 1
  175. }
  176. },
  177. created(this: any) {
  178. calls.push('mixinA created')
  179. expect(this.a).toBe(1)
  180. expect(this.b).toBe(2)
  181. expect(this.c).toBe(3)
  182. },
  183. mounted() {
  184. calls.push('mixinA mounted')
  185. }
  186. }
  187. const mixinB = {
  188. name: 'mixinB',
  189. data() {
  190. return {
  191. b: 2
  192. }
  193. },
  194. created(this: any) {
  195. calls.push('mixinB created')
  196. expect(this.a).toBe(1)
  197. expect(this.b).toBe(2)
  198. expect(this.c).toBe(3)
  199. },
  200. mounted() {
  201. calls.push('mixinB mounted')
  202. }
  203. }
  204. const Comp = {
  205. data() {
  206. return {
  207. c: 3
  208. }
  209. },
  210. created(this: any) {
  211. calls.push('comp created')
  212. expect(this.a).toBe(1)
  213. expect(this.b).toBe(2)
  214. expect(this.c).toBe(3)
  215. },
  216. mounted() {
  217. calls.push('comp mounted')
  218. },
  219. render(this: any) {
  220. return `${this.a}${this.b}${this.c}`
  221. }
  222. }
  223. const app = createApp(Comp)
  224. app.mixin(mixinA)
  225. app.mixin(mixinB)
  226. app.mixin(mixinA)
  227. app.mixin(mixinB)
  228. expect(
  229. 'Mixin has already been applied to target app'
  230. ).toHaveBeenWarnedTimes(2)
  231. expect(
  232. 'Mixin has already been applied to target app: mixinB'
  233. ).toHaveBeenWarnedTimes(1)
  234. const root = nodeOps.createElement('div')
  235. app.mount(root)
  236. expect(serializeInner(root)).toBe(`123`)
  237. expect(calls).toEqual([
  238. 'mixinA created',
  239. 'mixinB created',
  240. 'comp created',
  241. 'mixinA mounted',
  242. 'mixinB mounted',
  243. 'comp mounted'
  244. ])
  245. })
  246. test('use', () => {
  247. const PluginA: Plugin = app => app.provide('foo', 1)
  248. const PluginB: Plugin = {
  249. install: (app, arg1, arg2) => app.provide('bar', arg1 + arg2)
  250. }
  251. class PluginC {
  252. someProperty = {}
  253. static install() {
  254. app.provide('baz', 2)
  255. }
  256. }
  257. const PluginD: any = undefined
  258. const Root = {
  259. setup() {
  260. const foo = inject('foo')
  261. const bar = inject('bar')
  262. return () => `${foo},${bar}`
  263. }
  264. }
  265. const app = createApp(Root)
  266. app.use(PluginA)
  267. app.use(PluginB, 1, 1)
  268. app.use(PluginC)
  269. const root = nodeOps.createElement('div')
  270. app.mount(root)
  271. expect(serializeInner(root)).toBe(`1,2`)
  272. app.use(PluginA)
  273. expect(
  274. `Plugin has already been applied to target app`
  275. ).toHaveBeenWarnedTimes(1)
  276. app.use(PluginD)
  277. expect(
  278. `A plugin must either be a function or an object with an "install" ` +
  279. `function.`
  280. ).toHaveBeenWarnedTimes(1)
  281. })
  282. test('config.errorHandler', () => {
  283. const error = new Error()
  284. const count = ref(0)
  285. const handler = vi.fn((err, instance, info) => {
  286. expect(err).toBe(error)
  287. expect((instance as any).count).toBe(count.value)
  288. expect(info).toBe(`render function`)
  289. })
  290. const Root = {
  291. setup() {
  292. const count = ref(0)
  293. return {
  294. count
  295. }
  296. },
  297. render() {
  298. throw error
  299. }
  300. }
  301. const app = createApp(Root)
  302. app.config.errorHandler = handler
  303. app.mount(nodeOps.createElement('div'))
  304. expect(handler).toHaveBeenCalled()
  305. })
  306. test('config.warnHandler', () => {
  307. let ctx: any
  308. const handler = vi.fn((msg, instance, trace) => {
  309. expect(msg).toMatch(`Component is missing template or render function`)
  310. expect(instance).toBe(ctx.proxy)
  311. expect(trace).toMatch(`Hello`)
  312. })
  313. const Root = {
  314. name: 'Hello',
  315. setup() {
  316. ctx = getCurrentInstance()
  317. }
  318. }
  319. const app = createApp(Root)
  320. app.config.warnHandler = handler
  321. app.mount(nodeOps.createElement('div'))
  322. expect(handler).toHaveBeenCalledTimes(1)
  323. })
  324. describe('config.isNativeTag', () => {
  325. const isNativeTag = vi.fn(tag => tag === 'div')
  326. test('Component.name', () => {
  327. const Root = {
  328. name: 'div',
  329. render() {
  330. return null
  331. }
  332. }
  333. const app = createApp(Root)
  334. Object.defineProperty(app.config, 'isNativeTag', {
  335. value: isNativeTag,
  336. writable: false
  337. })
  338. app.mount(nodeOps.createElement('div'))
  339. expect(
  340. `Do not use built-in or reserved HTML elements as component id: div`
  341. ).toHaveBeenWarned()
  342. })
  343. test('Component.components', () => {
  344. const Root = {
  345. components: {
  346. div: () => 'div'
  347. },
  348. render() {
  349. return null
  350. }
  351. }
  352. const app = createApp(Root)
  353. Object.defineProperty(app.config, 'isNativeTag', {
  354. value: isNativeTag,
  355. writable: false
  356. })
  357. app.mount(nodeOps.createElement('div'))
  358. expect(
  359. `Do not use built-in or reserved HTML elements as component id: div`
  360. ).toHaveBeenWarned()
  361. })
  362. test('Component.directives', () => {
  363. const Root = {
  364. directives: {
  365. bind: () => {}
  366. },
  367. render() {
  368. return null
  369. }
  370. }
  371. const app = createApp(Root)
  372. Object.defineProperty(app.config, 'isNativeTag', {
  373. value: isNativeTag,
  374. writable: false
  375. })
  376. app.mount(nodeOps.createElement('div'))
  377. expect(
  378. `Do not use built-in directive ids as custom directive id: bind`
  379. ).toHaveBeenWarned()
  380. })
  381. test('register using app.component', () => {
  382. const app = createApp({
  383. render() {}
  384. })
  385. Object.defineProperty(app.config, 'isNativeTag', {
  386. value: isNativeTag,
  387. writable: false
  388. })
  389. app.component('div', () => 'div')
  390. app.mount(nodeOps.createElement('div'))
  391. expect(
  392. `Do not use built-in or reserved HTML elements as component id: div`
  393. ).toHaveBeenWarned()
  394. })
  395. })
  396. test('config.optionMergeStrategies', () => {
  397. let merged: string
  398. const App = defineComponent({
  399. render() {},
  400. mixins: [{ foo: 'mixin' }],
  401. extends: { foo: 'extends' },
  402. foo: 'local',
  403. beforeCreate() {
  404. merged = this.$options.foo
  405. }
  406. })
  407. const app = createApp(App)
  408. app.mixin({
  409. foo: 'global'
  410. })
  411. app.config.optionMergeStrategies.foo = (a, b) => (a ? `${a},` : ``) + b
  412. app.mount(nodeOps.createElement('div'))
  413. expect(merged!).toBe('global,extends,mixin,local')
  414. })
  415. test('config.globalProperties', () => {
  416. const app = createApp({
  417. render() {
  418. return this.foo
  419. }
  420. })
  421. app.config.globalProperties.foo = 'hello'
  422. const root = nodeOps.createElement('div')
  423. app.mount(root)
  424. expect(serializeInner(root)).toBe('hello')
  425. })
  426. test('return property "_" should not overwrite "ctx._", __isScriptSetup: false', () => {
  427. const Comp = defineComponent({
  428. setup() {
  429. return {
  430. _: ref(0) // return property "_" should not overwrite "ctx._"
  431. }
  432. },
  433. render() {
  434. return h('input', {
  435. ref: 'input'
  436. })
  437. }
  438. })
  439. const root1 = nodeOps.createElement('div')
  440. createApp(Comp).mount(root1)
  441. expect(
  442. `setup() return property "_" should not start with "$" or "_" which are reserved prefixes for Vue internals.`
  443. ).toHaveBeenWarned()
  444. })
  445. test('return property "_" should not overwrite "ctx._", __isScriptSetup: true', () => {
  446. const Comp = defineComponent({
  447. setup() {
  448. return {
  449. _: ref(0), // return property "_" should not overwrite "ctx._"
  450. __isScriptSetup: true // mock __isScriptSetup = true
  451. }
  452. },
  453. render() {
  454. return h('input', {
  455. ref: 'input'
  456. })
  457. }
  458. })
  459. const root1 = nodeOps.createElement('div')
  460. const app = createApp(Comp).mount(root1)
  461. // trigger
  462. app.$refs.input
  463. expect(
  464. `TypeError: Cannot read property '__isScriptSetup' of undefined`
  465. ).not.toHaveBeenWarned()
  466. })
  467. // config.compilerOptions is tested in packages/vue since it is only
  468. // supported in the full build.
  469. })