diff --git a/packages/api/test/hooks.js b/packages/api/test/hooks.js index d07c0de539..120d016301 100644 --- a/packages/api/test/hooks.js +++ b/packages/api/test/hooks.js @@ -148,3 +148,38 @@ export function createApiMiniflare ({ initialBindings = workerGlobals, bindings } }) } + +/** + * @param {Promise} serverPromise + * @param {(server: import('http').Server) => Promise} withServerCb + */ +export function useServer (serverPromise, withServerCb) { + const use = async () => { + const server = await serverPromise + try { + await withServerCb(server) + } finally { + await closeServer(server) + } + } + return use() +} + +/** + * @param {import('http').Server} server + */ +export async function closeServer (server) { + return new Promise((resolve, reject) => { + server.close(error => error ? reject(error) : resolve(undefined)) + }) +} + +/** + * @param {import('http').Server} server + */ +export function getServerUrl (server) { + const address = server.address() + if (!address) { throw new Error('no address') } + if (typeof address !== 'object') { throw new Error(`unexpected address type ${address}`) } + return `http://localhost:${address.port}` +} diff --git a/packages/api/test/user.spec.js b/packages/api/test/user.spec.js index cfa79c0c6e..5596d32c29 100644 --- a/packages/api/test/user.spec.js +++ b/packages/api/test/user.spec.js @@ -9,7 +9,7 @@ import { userLoginPost } from '../src/user.js' import { Magic } from '@magic-sdk/admin' import { createMockCustomerService, createMockSubscriptionsService, createMockUserCustomerService } from '../src/utils/billing.js' import { createMagicTestModeToken } from '../src/magic.link.js' -import { createApiMiniflare } from './hooks.js' +import { createApiMiniflare, useServer, getServerUrl } from './hooks.js' describe('GET /user/account', () => { it('error if not authenticated with magic.link', async () => { @@ -602,38 +602,3 @@ describe('userLoginPost', function () { }) }) }) - -/** - * @param {Promise} serverPromise - * @param {(server: import('http').Server) => Promise} withServerCb - */ -function useServer (serverPromise, withServerCb) { - const use = async () => { - const server = await serverPromise - try { - await withServerCb(server) - } finally { - await closeServer(server) - } - } - return use() -} - -/** - * @param {import('http').Server} server - */ -async function closeServer (server) { - return new Promise((resolve, reject) => { - server.close(error => error ? reject(error) : resolve(undefined)) - }) -} - -/** - * @param {import('http').Server} server - */ -function getServerUrl (server) { - const address = server.address() - if (!address) { throw new Error('no address') } - if (typeof address !== 'object') { throw new Error(`unexpected address type ${address}`) } - return `http://localhost:${address.port}` -}