app-test-setup.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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) ?
  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. }
  35. module.exports.startServer = (onSuccess) => {
  36. const http = require('http')
  37. const responses = {
  38. writeFile: null,
  39. readFile: null,
  40. writeFileWithDir: null,
  41. readFileWithDir: null,
  42. readDir: null,
  43. readDirWithDir: null,
  44. copyFile: null,
  45. copyFileWithDir: null,
  46. createDir: null,
  47. createDirWithDir: null,
  48. removeDir: null,
  49. removeDirWithDir: null,
  50. renameFile: null,
  51. renameFileWithDir: null,
  52. removeFile: null,
  53. renameFileWithDir: null,
  54. listen: null
  55. }
  56. function addResponse(response) {
  57. responses[response.cmd] = true
  58. if (!Object.values(responses).some(c => c === null)) {
  59. server.close(onSuccess)
  60. }
  61. }
  62. const app = http.createServer((req, res) => {
  63. // Set CORS headers
  64. res.setHeader('Access-Control-Allow-Origin', '*')
  65. res.setHeader('Access-Control-Request-Method', '*')
  66. res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET')
  67. res.setHeader('Access-Control-Allow-Headers', '*')
  68. if (req.method === 'OPTIONS') {
  69. res.writeHead(200)
  70. res.end()
  71. return
  72. }
  73. if (req.method === 'POST') {
  74. let body = ''
  75. req.on('data', chunk => {
  76. body += chunk.toString()
  77. })
  78. if (req.url === '/reply') {
  79. req.on('end', () => {
  80. const json = JSON.parse(body)
  81. addResponse(json)
  82. res.writeHead(200)
  83. res.end()
  84. })
  85. }
  86. if (req.url === '/error') {
  87. req.on('end', () => {
  88. res.writeHead(200)
  89. res.end()
  90. throw new Error(body)
  91. })
  92. }
  93. }
  94. })
  95. const port = 7000
  96. const server = app.listen(port)
  97. return {
  98. server,
  99. responses
  100. }
  101. }