index.spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. import execa from 'execa'
  5. import fixtures from 'fixturez'
  6. const f = fixtures(__dirname)
  7. import path from 'path'
  8. import fs from 'fs'
  9. const ctaBinary = path.resolve('./bin/create-tauri-app.js')
  10. const clijs = path.resolve('../cli.js/')
  11. const api = path.resolve('../api/')
  12. const manager = process.env.TAURI_RUN_MANAGER ?? 'npm'
  13. const recipes = process.env.TAURI_RECIPE
  14. ? [process.env.TAURI_RECIPE]
  15. : ['vanillajs', 'cra', 'vite', 'vuecli', 'ngcli', 'svelte']
  16. const timeoutLong = 900000
  17. const timeoutLittleLonger = 930000
  18. const logOut = false ? 'inherit' : 'pipe'
  19. describe('CTA', () => {
  20. console.warn(
  21. 'NOTE: You need to have installed and built cli.js and api before running the tests.'
  22. )
  23. describe.each(recipes.map((recipe) => [recipe, 'tauri-app']))(
  24. `%s recipe`,
  25. (recipe: string, appName: string) => {
  26. it(
  27. 'runs',
  28. async () => {
  29. // creates a temp folder to run CTA within (this is our cwd)
  30. const folder = f.temp()
  31. const appFolder = path.join(folder, appName)
  32. // runs CTA with all args set to avoid any prompts
  33. const cta = await execa(
  34. 'node',
  35. [
  36. ctaBinary,
  37. '--manager',
  38. manager,
  39. '--recipe',
  40. recipe,
  41. '--ci',
  42. '--dev'
  43. ],
  44. {
  45. all: true,
  46. stdio: logOut,
  47. cwd: folder,
  48. timeout: timeoutLong
  49. }
  50. )
  51. // check to make certain it didn't fail anywhere
  52. expect(cta.failed).toBe(false)
  53. expect(cta.timedOut).toBe(false)
  54. expect(cta.isCanceled).toBe(false)
  55. expect(cta.killed).toBe(false)
  56. expect(cta.signal).toBe(undefined)
  57. const packageFileInitial: {
  58. [k: string]: string | object
  59. } = JSON.parse(
  60. await fs.promises.readFile(
  61. path.join(appFolder, 'package.json'),
  62. 'utf-8'
  63. )
  64. )
  65. expect(packageFileInitial['name']).toBe(appName)
  66. // run a tauri build to check if what we produced
  67. // can actually create an app
  68. // TODO long term we will want to hook this up to a real test harness
  69. // and then run that test suite instead
  70. let opts: string[] = []
  71. if (manager === 'npm') {
  72. opts =
  73. recipe == 'vuecli'
  74. ? ['run', 'tauri:build']
  75. : ['run', 'tauri', '--', 'build']
  76. } else if (manager === 'yarn') {
  77. opts = recipe == 'vuecli' ? ['tauri:build'] : ['tauri', 'build']
  78. }
  79. const tauriBuild = await execa(manager, opts, {
  80. all: true,
  81. stdio: logOut,
  82. cwd: appFolder,
  83. timeout: timeoutLong
  84. })
  85. expect(tauriBuild.failed).toBe(false)
  86. expect(tauriBuild.timedOut).toBe(false)
  87. expect(tauriBuild.isCanceled).toBe(false)
  88. expect(tauriBuild.killed).toBe(false)
  89. expect(tauriBuild.signal).toBe(undefined)
  90. const packageFileOutput: {
  91. [k: string]: string | object
  92. } = JSON.parse(
  93. await fs.promises.readFile(
  94. path.join(appFolder, 'package.json'),
  95. 'utf-8'
  96. )
  97. )
  98. expect(packageFileOutput['name']).toBe(appName)
  99. const assertCustom: { [k: string]: Function } = {
  100. vanillajs: () => {
  101. expect(packageFileOutput['scripts']).toMatchObject({
  102. tauri: 'tauri'
  103. })
  104. },
  105. cra: () => {
  106. expect(packageFileOutput['scripts']).toEqual(
  107. expect.objectContaining({
  108. tauri: 'tauri'
  109. })
  110. )
  111. },
  112. vite: () => {
  113. expect(packageFileOutput['scripts']).toEqual(
  114. expect.objectContaining({
  115. tauri: 'tauri'
  116. })
  117. )
  118. },
  119. vuecli: () => {
  120. expect(packageFileOutput['scripts']).toEqual(
  121. expect.objectContaining({
  122. 'tauri:build': expect.anything(),
  123. 'tauri:serve': expect.anything()
  124. })
  125. )
  126. },
  127. ngcli: () => {
  128. expect(packageFileOutput['scripts']).toEqual(
  129. expect.objectContaining({
  130. ng: 'ng',
  131. tauri: 'tauri'
  132. })
  133. )
  134. },
  135. svelte: () => {
  136. expect(packageFileOutput['scripts']).toEqual(
  137. expect.objectContaining({
  138. tauri: 'tauri'
  139. })
  140. )
  141. }
  142. }
  143. const getCustomAsserts = assertCustom[recipe]
  144. if (getCustomAsserts) getCustomAsserts()
  145. },
  146. timeoutLittleLonger
  147. )
  148. }
  149. )
  150. })