dev.spec.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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('dist/api/cli')
  30. return new Promise(async (resolve, reject) => {
  31. try {
  32. process.chdir(path.join(fixtureSetup.fixtureDir, 'app'))
  33. const { promise, pid } = dev({ config: tauriConfig })
  34. const isRunning = require('is-running')
  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. })