Comments (3)
I've tested and this works as expected up to v0.28.4, it breaks in https://github.com/mswjs/interceptors/releases/tag/v0.29.0
from interceptors.
Please also make sure to update Undici to the latest version. There were bugs that broke your code in unapparent ways.
I will take a look at this once I have a minute.
from interceptors.
I've just tried this on the latest version of Interceptors and the test passes without issues:
import http from 'node:http'
import { beforeAll, afterEach, afterAll, it, expect } from 'vitest'
import { FetchInterceptor } from '../../../../src/interceptors/fetch'
const httpServer = http.createServer((req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('hello world')
})
const interceptor = new FetchInterceptor()
beforeAll(async () => {
interceptor.apply()
await new Promise<void>((resolve) => {
httpServer.listen(3000, 'localhost', resolve)
})
})
afterEach(() => {
interceptor.removeAllListeners()
})
afterAll(async () => {
interceptor.dispose()
await new Promise<void>((resolve, reject) => {
httpServer.close((error) => {
if (error) reject(error)
resolve()
})
})
})
it('responds to a fetch request', async () => {
interceptor.on('request', ({ request }) => {
request.respondWith(new Response('mock'))
})
const response = await fetch('http://localhost:3000/')
expect(response.status).toBe(200)
await expect(response.text()).resolves.toBe('mock')
})
This is the exact test case from above + a proper test setup to spawn the server/apply the interceptor properly in test (using the hooks).
Update to the latest version of the library, the issue seems to have been fixed. If not, please provide a reproduction repository I can clone and run. Thanks!
from interceptors.
Related Issues (20)
- Await all "response" listeners to finish HOT 6
- Protocol "https:" not supported. Expected "http:" when using "proxy-agent" HOT 1
- Simplify the npm package files HOT 3
- XMLHttpRequest upload (upload.onprogress) event is not supported HOT 4
- Nock compatibility HOT 3
- Unclear how to throw an actual error HOT 3
- Proper documentation on how to use it HOT 1
- RemoteHttpResolver example in README is missing the ".apply()" call HOT 2
- `@mswjs/[email protected]` breaks `URL` as fetch argument HOT 1
- should pass filteringPath options HOT 1
- ERR_MODULE_NOT_FOUND Cannot find module HOT 1
- Support reusing the same Socket for multiple requests HOT 1
- Cannot find module '@mswjs/interceptors/presets/browser' or its corresponding type declarations.
- Support Response.error() in RemoteInterceptor
- http not defined when used with bun HOT 2
- WebSocket: `wasClean` is incorrectly set to `true` on clean closures HOT 1
- XHR: Calculate `total` of the response body if no `Content-Length` header was provided HOT 2
- XHR: set `lengthComputable` event property to `false` if computing length failed
- controller.errorWith ends up in the response path if the error is not Error instance
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from interceptors.