|  | 
| 1 | 1 | import * as http from 'http'; | 
| 2 | 2 | import { AddressInfo } from 'net'; | 
| 3 |  | -import { createRequestHandler as createFastifyRequestHandler } from '@mcansh/remix-fastify'; | 
| 4 |  | -import { createRequestHandler as createExpressRequestHandler } from '@remix-run/express'; | 
| 5 |  | -import { wrapExpressCreateRequestHandler, wrapFastifyCreateRequestHandler } from '@sentry/remix'; | 
|  | 3 | +import { createRequestHandler } from '@remix-run/express'; | 
|  | 4 | +import { wrapExpressCreateRequestHandler } from '@sentry/remix'; | 
| 6 | 5 | import express from 'express'; | 
| 7 |  | -import fastify from 'fastify'; | 
| 8 |  | -import formBody from '@fastify/formbody' | 
| 9 |  | - | 
| 10 | 6 | import { TestEnv } from '../../../../../../../dev-packages/node-integration-tests/utils'; | 
| 11 | 7 | 
 | 
| 12 | 8 | export * from '../../../../../../../dev-packages/node-integration-tests/utils'; | 
| 13 | 9 | 
 | 
| 14 |  | -export const enum Adapter { | 
| 15 |  | -  Builtin = 'builtin', | 
| 16 |  | -  Express = 'express', | 
| 17 |  | -  Fastify = 'fastify', | 
| 18 |  | -} | 
| 19 |  | - | 
| 20 |  | -const adapters = { | 
| 21 |  | -  [Adapter.Builtin]: createExpressRequestHandler, | 
| 22 |  | -  [Adapter.Express]: wrapExpressCreateRequestHandler(createExpressRequestHandler), | 
| 23 |  | -  [Adapter.Fastify]: wrapFastifyCreateRequestHandler(createFastifyRequestHandler), | 
| 24 |  | -}; | 
| 25 |  | - | 
| 26 |  | -const runExpressApp = (adapter: Adapter.Builtin | Adapter.Express): Promise<http.Server> => new Promise( | 
| 27 |  | -  res => { | 
| 28 |  | -  const app = express(); | 
| 29 |  | -  app.all('*', adapters[adapter]({ build: require('../../../build') })); | 
| 30 |  | -  res(app.listen(0)); | 
| 31 |  | -  } | 
| 32 |  | -) | 
| 33 |  | - | 
| 34 |  | -const runFastifyApp = (): Promise<http.Server> => new Promise(res => { | 
| 35 |  | -  const app = fastify(); | 
| 36 |  | -  app.register(formBody); | 
| 37 |  | -  // @ts-ignore | 
| 38 |  | -  app.all('*', adapters[Adapter.Fastify]({ build: require('../../../build') })); | 
| 39 |  | -  app.listen({port: 0}, (_err, _addr) => { | 
| 40 |  | -    res(app.server) | 
| 41 |  | -  }); | 
| 42 |  | -}) | 
| 43 |  | - | 
| 44 | 10 | export class RemixTestEnv extends TestEnv { | 
| 45 | 11 |   private constructor(public readonly server: http.Server, public readonly url: string) { | 
| 46 | 12 |     super(server, url); | 
| 47 | 13 |   } | 
| 48 | 14 | 
 | 
| 49 |  | -  public static async init(adapter: Adapter): Promise<RemixTestEnv> { | 
| 50 |  | -    const srv = adapter === Adapter.Fastify ? await runFastifyApp() : await runExpressApp(adapter); | 
| 51 |  | -    const port = (srv.address() as AddressInfo).port | 
| 52 |  | -    return new RemixTestEnv(srv, `http://localhost:${port}`); | 
|  | 15 | +  public static async init(adapter: string = 'builtin'): Promise<RemixTestEnv> { | 
|  | 16 | +    const requestHandlerFactory = | 
|  | 17 | +      adapter === 'express' ? wrapExpressCreateRequestHandler(createRequestHandler) : createRequestHandler; | 
|  | 18 | + | 
|  | 19 | +    let serverPort; | 
|  | 20 | +    const server = await new Promise<http.Server>(resolve => { | 
|  | 21 | +      const app = express(); | 
|  | 22 | + | 
|  | 23 | +      app.all('*', requestHandlerFactory({ build: require('../../../build') })); | 
|  | 24 | + | 
|  | 25 | +      const server = app.listen(0, () => { | 
|  | 26 | +        serverPort = (server.address() as AddressInfo).port; | 
|  | 27 | +        resolve(server); | 
|  | 28 | +      }); | 
|  | 29 | +    }); | 
|  | 30 | + | 
|  | 31 | +    return new RemixTestEnv(server, `http://localhost:${serverPort}`); | 
| 53 | 32 |   } | 
| 54 | 33 | } | 
0 commit comments