app-test-setup.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const path = require('path')
  2. const process = require('process')
  3. const mockFixtureDir = path.resolve(__dirname, '../fixtures')
  4. module.exports.fixtureDir = mockFixtureDir
  5. function mockResolvePath (basePath, dir) {
  6. return dir.startsWith('/') || /^\S:/g.test(dir)
  7. ? dir
  8. : path.resolve(basePath, dir)
  9. }
  10. module.exports.initJest = (mockFixture) => {
  11. jest.setTimeout(720000)
  12. jest.mock('helpers/non-webpack-require', () => {
  13. return path => {
  14. const value = require('fs').readFileSync(path).toString()
  15. if (path.endsWith('.json')) {
  16. return JSON.parse(value)
  17. }
  18. return value
  19. }
  20. })
  21. jest.mock('helpers/app-paths', () => {
  22. const path = require('path')
  23. const appDir = path.join(mockFixtureDir, mockFixture)
  24. const tauriDir = path.join(appDir, 'src-tauri')
  25. return {
  26. appDir,
  27. tauriDir,
  28. resolve: {
  29. app: dir => mockResolvePath(appDir, dir),
  30. tauri: dir => mockResolvePath(tauriDir, dir)
  31. }
  32. }
  33. })
  34. jest.spyOn(process, 'exit').mockImplementation(() => {})
  35. }
  36. module.exports.startServer = (onSuccess) => {
  37. const http = require('http')
  38. const responses = {
  39. writeFile: null,
  40. readFile: null,
  41. writeFileWithDir: null,
  42. readFileWithDir: null,
  43. readDir: null,
  44. readDirWithDir: null,
  45. copyFile: null,
  46. copyFileWithDir: null,
  47. createDir: null,
  48. createDirWithDir: null,
  49. removeDir: null,
  50. removeDirWithDir: null,
  51. renameFile: null,
  52. renameFileWithDir: null,
  53. removeFile: null,
  54. renameFileWithDir: null,
  55. listen: null
  56. }
  57. function addResponse (response) {
  58. responses[response.cmd] = true
  59. if (!Object.values(responses).some(c => c === null)) {
  60. server.close(onSuccess)
  61. }
  62. }
  63. const app = http.createServer((req, res) => {
  64. // Set CORS headers
  65. res.setHeader('Access-Control-Allow-Origin', '*')
  66. res.setHeader('Access-Control-Request-Method', '*')
  67. res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET')
  68. res.setHeader('Access-Control-Allow-Headers', '*')
  69. if (req.method === 'OPTIONS') {
  70. res.writeHead(200)
  71. res.end()
  72. return
  73. }
  74. if (req.method === 'POST') {
  75. let body = ''
  76. req.on('data', chunk => {
  77. body += chunk.toString()
  78. })
  79. if (req.url === '/reply') {
  80. req.on('end', () => {
  81. const json = JSON.parse(body)
  82. addResponse(json)
  83. res.writeHead(200)
  84. res.end()
  85. })
  86. }
  87. if (req.url === '/error') {
  88. req.on('end', () => {
  89. res.writeHead(200)
  90. res.end()
  91. throw new Error(body)
  92. })
  93. }
  94. }
  95. })
  96. const port = 7000
  97. const server = app.listen(port)
  98. return server
  99. }