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