Skip to content

Commit

Permalink
packages/api server tools move from test file to hooks.js
Browse files Browse the repository at this point in the history
  • Loading branch information
gobengo committed Nov 10, 2023
1 parent 675844f commit 94417f3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
35 changes: 35 additions & 0 deletions packages/api/test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,38 @@ export function createApiMiniflare ({ initialBindings = workerGlobals, bindings
}
})
}

/**
* @param {Promise<import('http').Server>} serverPromise
* @param {(server: import('http').Server) => Promise<void>} 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}`
}
37 changes: 1 addition & 36 deletions packages/api/test/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -602,38 +602,3 @@ describe('userLoginPost', function () {
})
})
})

/**
* @param {Promise<import('http').Server>} serverPromise
* @param {(server: import('http').Server) => Promise<void>} 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}`
}

0 comments on commit 94417f3

Please sign in to comment.