app-test-setup.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const path = require('path')
  2. const process = require('process')
  3. const mockFixtureDir = path.resolve(__dirname, '../fixtures')
  4. module.exports.fixtureDir = mockFixtureDir
  5. module.exports.initJest = (mockFixture) => {
  6. jest.setTimeout(720000)
  7. jest.mock('helpers/non-webpack-require', () => {
  8. return path => {
  9. const value = require('fs').readFileSync(path).toString()
  10. if (path.endsWith('.json')) {
  11. return JSON.parse(value)
  12. }
  13. return value
  14. }
  15. })
  16. jest.mock('helpers/app-paths', () => {
  17. const path = require('path')
  18. const appDir = path.join(mockFixtureDir, mockFixture)
  19. const tauriDir = path.join(appDir, 'src-tauri')
  20. return {
  21. appDir,
  22. tauriDir,
  23. resolve: {
  24. app: dir => path.join(appDir, dir),
  25. tauri: dir => path.join(tauriDir, dir)
  26. }
  27. }
  28. })
  29. jest.spyOn(process, 'exit').mockImplementation(() => {})
  30. }
  31. module.exports.startServer = (onReply) => {
  32. const http = require('http')
  33. const app = http.createServer((req, res) => {
  34. // Set CORS headers
  35. res.setHeader('Access-Control-Allow-Origin', '*')
  36. res.setHeader('Access-Control-Request-Method', '*')
  37. res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET')
  38. res.setHeader('Access-Control-Allow-Headers', '*')
  39. if (req.method === 'OPTIONS') {
  40. res.writeHead(200)
  41. res.end()
  42. return
  43. }
  44. if (req.method === 'POST') {
  45. if (req.url === '/reply') {
  46. let body = ''
  47. req.on('data', chunk => {
  48. body += chunk.toString()
  49. })
  50. req.on('end', () => {
  51. expect(JSON.parse(body)).toStrictEqual({
  52. msg: 'TEST'
  53. })
  54. server.close(onReply)
  55. })
  56. }
  57. }
  58. })
  59. const port = 7000
  60. const server = app.listen(port)
  61. return server
  62. }