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:
Artem Zakharchenko
2023-10-23 09:45:04 +02:00
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
440 changed files with 9001 additions and 7739 deletions
+46
View File
@@ -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)
})