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