index.spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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']
  16. const timeoutLong = 900000
  17. const timeoutLittleLonger = 930000
  18. const logOut = false ? 'inherit' : 'pipe'
  19. beforeAll(async () => {
  20. const installCLI = await execa('yarn', [], {
  21. stdio: logOut,
  22. cwd: clijs,
  23. timeout: timeoutLong
  24. })
  25. const buildCLI = await execa('yarn', ['build-release'], {
  26. stdio: logOut,
  27. cwd: clijs,
  28. timeout: timeoutLong
  29. })
  30. const linkCLI = await execa('yarn', ['link'], {
  31. stdio: logOut,
  32. cwd: clijs,
  33. timeout: timeoutLong
  34. })
  35. const installAPI = await execa('yarn', [], {
  36. stdio: logOut,
  37. cwd: api,
  38. timeout: timeoutLong
  39. })
  40. const buildAPI = await execa('yarn', ['build'], {
  41. stdio: logOut,
  42. cwd: api,
  43. timeout: timeoutLong
  44. })
  45. const linkAPI = await execa('yarn', ['link'], {
  46. stdio: logOut,
  47. cwd: path.join(api, 'dist'),
  48. timeout: timeoutLong
  49. })
  50. }, timeoutLittleLonger)
  51. describe('CTA', () => {
  52. describe.each(recipes.map((recipe) => [recipe, 'tauri-app']))(
  53. `%s recipe`,
  54. (recipe: string, appName: string) => {
  55. it(
  56. 'runs',
  57. async () => {
  58. // creates a temp folder to run CTA within (this is our cwd)
  59. const folder = f.temp()
  60. const appFolder = path.join(folder, appName)
  61. // runs CTA with all args set to avoid any prompts
  62. const cta = await execa(
  63. 'node',
  64. [
  65. ctaBinary,
  66. '--manager',
  67. manager,
  68. '--recipe',
  69. recipe,
  70. '--ci',
  71. '--dev'
  72. ],
  73. {
  74. all: true,
  75. stdio: logOut,
  76. cwd: folder,
  77. timeout: timeoutLong
  78. }
  79. )
  80. // check to make certain it didn't fail anywhere
  81. expect(cta.failed).toBe(false)
  82. expect(cta.timedOut).toBe(false)
  83. expect(cta.isCanceled).toBe(false)
  84. expect(cta.killed).toBe(false)
  85. expect(cta.signal).toBe(undefined)
  86. const packageFileInitial: {
  87. [k: string]: string | object
  88. } = JSON.parse(
  89. await fs.promises.readFile(
  90. path.join(appFolder, 'package.json'),
  91. 'utf-8'
  92. )
  93. )
  94. expect(packageFileInitial['name']).toBe(appName)
  95. // run a tauri build to check if what we produced
  96. // can actually create an app
  97. // TODO long term we will want to hook this up to a real test harness
  98. // and then run that test suite instead
  99. let opts: string[] = []
  100. if (manager === 'npm') {
  101. opts =
  102. recipe == 'vuecli'
  103. ? ['run', 'tauri:build']
  104. : ['run', 'tauri', '--', 'build']
  105. } else if (manager === 'yarn') {
  106. opts = recipe == 'vuecli' ? ['tauri:build'] : ['tauri', 'build']
  107. }
  108. const tauriBuild = await execa(manager, opts, {
  109. all: true,
  110. stdio: logOut,
  111. cwd: appFolder,
  112. timeout: timeoutLong
  113. })
  114. expect(tauriBuild.failed).toBe(false)
  115. expect(tauriBuild.timedOut).toBe(false)
  116. expect(tauriBuild.isCanceled).toBe(false)
  117. expect(tauriBuild.killed).toBe(false)
  118. expect(tauriBuild.signal).toBe(undefined)
  119. const packageFileOutput: {
  120. [k: string]: string | object
  121. } = JSON.parse(
  122. await fs.promises.readFile(
  123. path.join(appFolder, 'package.json'),
  124. 'utf-8'
  125. )
  126. )
  127. expect(packageFileOutput['name']).toBe(appName)
  128. const assertCustom: { [k: string]: Function } = {
  129. vanillajs: () => {
  130. expect(packageFileOutput['scripts']).toMatchObject({
  131. tauri: 'tauri'
  132. })
  133. },
  134. cra: () => {
  135. expect(packageFileOutput['scripts']).toEqual(
  136. expect.objectContaining({
  137. tauri: 'tauri'
  138. })
  139. )
  140. },
  141. vite: () => {
  142. expect(packageFileOutput['scripts']).toEqual(
  143. expect.objectContaining({
  144. tauri: 'tauri'
  145. })
  146. )
  147. },
  148. vuecli: () => {
  149. expect(packageFileOutput['scripts']).toEqual(
  150. expect.objectContaining({
  151. 'tauri:build': expect.anything(),
  152. 'tauri:serve': expect.anything()
  153. })
  154. )
  155. }
  156. }
  157. const getCustomAsserts = assertCustom[recipe]
  158. if (getCustomAsserts) getCustomAsserts()
  159. },
  160. timeoutLittleLonger
  161. )
  162. }
  163. )
  164. })