Files
msw/test/node/rest-api/https.node.test.ts
T
8c8e959b49 feat!: adopt the global Fetch API (#1436)
Co-authored-by: Frederik Rabøl <[email protected]>
Co-authored-by: Christoph Fricke <[email protected]>
Co-authored-by: Piotr <[email protected]>
Co-authored-by: Kristján Oddsson <[email protected]>
Co-authored-by: thepassle <[email protected]>
Co-authored-by: Kevin Østerkilde <[email protected]>
Co-authored-by: Laryssa Rocha <[email protected]>
Co-authored-by: Matthew Costabile <[email protected]>
2023-10-23 09:45:04 +02:00

47 lines
982 B
TypeScript

/**
* @jest-environment node
*/
import https from 'https'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
const server = setupServer()
beforeAll(() => {
server.listen()
})
afterEach(() => {
server.resetHandlers()
})
afterAll(() => {
server.close()
})
test('intercepts and mocks a request made via "https"', (done) => {
server.use(
http.get('https://api.example.com/resource', () => {
return HttpResponse.text('Hello, world!')
}),
)
const request = https.get('https://api.example.com/resource')
request.on('response', (response) => {
const chunks: Array<Buffer> = []
response.on('data', (chunk) => chunks.push(Buffer.from(chunk)))
response.on('error', done)
response.once('end', () => {
expect(chunks).toHaveLength(1)
const responseText = Buffer.concat(chunks).toString('utf8')
expect(responseText).toBe('Hello, world!')
done()
})
})
request.on('error', done)
})