Skip to content

Commit

Permalink
Add test for mock agent with fetch in jest
Browse files Browse the repository at this point in the history
  • Loading branch information
hansottowirtz committed Jun 12, 2023
1 parent 2032e89 commit 7153a1c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion test/jest/mock-agent.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { request, setGlobalDispatcher, MockAgent } = require('../..')
const { setGlobalDispatcher, MockAgent } = require('../..')
const { getResponse } = require('../../lib/mock/mock-utils')

/* global describe, it, expect */
Expand Down Expand Up @@ -43,4 +43,21 @@ describe('MockAgent', () => {
const jsonResponse = JSON.parse(await getResponse(body))
expect(jsonResponse).toEqual({ foo: 'bar' })
})

it('should work in jest with fetch', async () => {
expect.assertions(1)

mockAgent = new MockAgent()
mockAgent.disableNetConnect()
setGlobalDispatcher(mockAgent)

let failed = false
try {
await fetch('http://localhost:9999')
} catch (e) {
if (e.cause?.code === 'UND_MOCK_ERR_MOCK_NOT_MATCHED') failed = true
}

expect(failed).toBe(true)
})
})

0 comments on commit 7153a1c

Please sign in to comment.