app-test-setup.js 1.8 KB

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