dev.spec.js 2.4 KB

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