mirror of
https://github.com/hanzoai/msw.git
synced 2026-09-24 17:00:34 +00:00
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]>
This commit is contained in:
committed by
GitHub
co-authored by
Frederik Rabøl
Christoph Fricke
Piotr
Kristján Oddsson
thepassle
Kevin Østerkilde
Laryssa Rocha
Matthew Costabile
parent
2496c3cde3
commit
8c8e959b49
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @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)
|
||||
})
|
||||
Reference in New Issue
Block a user