Skip to content

Commit

Permalink
Adds "withMocks" test util to apply "setupServer" with handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Feb 14, 2021
1 parent e69f867 commit 2449c35
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
9 changes: 4 additions & 5 deletions test/oas/oas.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import fetch from 'cross-fetch'
import { setupServer } from 'msw/node'
import { fromOpenAPI } from 'src/oas/fromOpenAPI'
import { withMocks } from '../support/withMocks'

it('supports explicit "example" JSON in the response schema', async () => {
const document = require('./fixtures/response-example.json')
const handlers = await fromOpenAPI(document)
const server = setupServer(...handlers)
server.listen()

const res = await fetch('http://oas.source.com/user')
server.close()
const res = await withMocks(handlers, () => {
return fetch('http://oas.source.com/user')
})

expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toBe('application/json')
Expand Down
17 changes: 17 additions & 0 deletions test/support/withMocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { RequestHandler } from 'msw'
import { setupServer } from 'msw/node'

export async function withMocks(
handlers: RequestHandler[],
callback: () => Promise<any>,
) {
const server = setupServer(...handlers)
server.listen()

try {
const result = await callback()
return result
} finally {
server.close()
}
}

0 comments on commit 2449c35

Please sign in to comment.