dev.spec.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. server.close(() => reject("App didn't reply"))
  38. }
  39. }, 2000)
  40. const server = fixtureSetup.startServer(async () => {
  41. success = true
  42. clearInterval(checkIntervalId)
  43. // wait for the app process to be killed
  44. setTimeout(async () => {
  45. try {
  46. await runner.stop()
  47. } catch {}
  48. resolve()
  49. }, 2000)
  50. })
  51. await promise
  52. } catch (error) {
  53. reject(error)
  54. }
  55. })
  56. }
  57. describe('Tauri Dev', () => {
  58. const build = {
  59. distDir: distDir
  60. }
  61. const devServer = startDevServer()
  62. it.each`
  63. url
  64. ${devServer.url}
  65. ${distDir}
  66. `('works with dev pointing to $url', ({ url }) => {
  67. const runningDevServer = url.startsWith('http')
  68. const promise = runDevTest({
  69. build: {
  70. ...build,
  71. devPath: url
  72. },
  73. ctx: {
  74. debug: true,
  75. dev: true
  76. }
  77. })
  78. promise.then(() => {
  79. if (runningDevServer) {
  80. devServer.server.close()
  81. }
  82. })
  83. return promise
  84. })
  85. })