dev.spec.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import path from 'path'
  2. import isRunning from 'is-running'
  3. import http from 'http'
  4. import { statSync, createReadStream } from 'fs'
  5. import * as fixtureSetup from '../fixtures/app-test-setup.js'
  6. const distDir = path.resolve(fixtureSetup.fixtureDir, 'app', 'dist')
  7. function startDevServer() {
  8. const app = http.createServer((req, res) => {
  9. if (req.method === 'GET') {
  10. if (req.url === '/') {
  11. const indexPath = path.join(distDir, 'index.html')
  12. const stat = statSync(indexPath)
  13. res.writeHead(200, {
  14. 'Content-Type': 'text/html',
  15. 'Content-Length': stat.size
  16. })
  17. createReadStream(indexPath).pipe(res)
  18. }
  19. }
  20. })
  21. const port = 7001
  22. const server = app.listen(port)
  23. return {
  24. server,
  25. url: `http://localhost:${port}`
  26. }
  27. }
  28. function runDevTest(tauriConfig) {
  29. fixtureSetup.initJest('app')
  30. return new Promise(async (resolve, reject) => {
  31. try {
  32. process.chdir(path.join(fixtureSetup.fixtureDir, 'app'))
  33. const { dev } = await import('dist/api/cli')
  34. const { promise, pid } = await dev({ config: tauriConfig })
  35. let success = false
  36. const checkIntervalId = setInterval(async () => {
  37. if (!isRunning(pid) && !success) {
  38. const failedCommands = Object.keys(responses)
  39. .filter((k) => responses[k] === null)
  40. .join(', ')
  41. server.close(() => reject("App didn't reply to " + failedCommands))
  42. }
  43. }, 2000)
  44. const { server, responses } = fixtureSetup.startServer(async () => {
  45. success = true
  46. clearInterval(checkIntervalId)
  47. // wait for the app process to be killed
  48. setTimeout(async () => {
  49. try {
  50. process.kill(pid)
  51. } catch {}
  52. resolve()
  53. }, 2000)
  54. })
  55. await promise
  56. } catch (error) {
  57. reject(error)
  58. }
  59. })
  60. }
  61. describe('Tauri Dev', () => {
  62. const build = {
  63. distDir: distDir,
  64. withGlobalTauri: true
  65. }
  66. const devServer = startDevServer()
  67. it.each`
  68. url
  69. ${devServer.url}
  70. ${distDir}
  71. `('works with dev pointing to $url', ({ url }) => {
  72. const runningDevServer = url.startsWith('http')
  73. const promise = runDevTest({
  74. build: {
  75. ...build,
  76. devPath: url
  77. }
  78. })
  79. promise.then(() => {
  80. if (runningDevServer) {
  81. devServer.server.close()
  82. }
  83. })
  84. return promise
  85. })
  86. })