Comments (4)
I think FIFO is the most correct.
The problem in your test is that you are not creating a new mock for each test.
from undici.
My real-life use case is a series of integration tests that cover a broad range of API requests (a few dozen). I don't want to think which requests will exactly be executed in a certain test, so I'd rather just take a full API mock and add needed overrides.
While a workaround is indeed fairly easy:
const createMockAgent = afterInit => {
const mockAgent = new MockAgent()
mockAgent.disableNetConnect()
const mockPool = mockAgent.get('https://api.example.com')
afterInit?.(mockPool) // a hook for adding overrides
// defaults
mockPool
.intercept({
path: '/folders/123',
})
.reply(200, {id: 123, name: 'mocked folder'})
.persist()
mockPool.intercept({path: '/folders/456'}).reply(404).persist()
return mockAgent
}
const mockAgent = createMockAgent(mockPool => {
// let's simulate a temporary downtime
mockPool
.intercept({
path: '/folders/123',
})
.reply(502)
.delay(1000)
.times(2)
})
I just think that it's not what the users are used to, at least the ones familiar with function mocking. The pattern of overriding an initial mock behavior is shown in the docs of Vitest (test 'should get with a mock'
) and Jest. Sinon.JS doesn't show it in the docs, but you can also change the return value of a stub in an imperative way.
Additionally, those libraries offer the way of resetting the mock/stub behavior (.mockReset()
, .resetBehavior()
). In Undici, after setting a persistent interceptor, there's no way back.
I see that Undici's MockAgent is heavily inspired by Nock, where behavior is exactly the same... Although there you can at least .deleteInterceptor()
😄 (kinda hacky though).
from undici.
Actually, when I'm thinking about it, it's not about FIFO vs LIFO per se, but rather the .persist()
having the same priority as one-time interceptors.
From the linked fragment of Jest docs:
When the mocked function runs out of implementations defined with
.mockImplementationOnce()
, it will execute the default implementation set withjest.fn(() => defaultValue)
or.mockImplementation(() => defaultValue)
if they were called:
And then it follows the intuition - on one hand, you can chain multiple calls of mockImplementationOnce
and it'll be FIFO, but on the other hand you don't have to ensure that the default implementation is provided as the last one.
Maybe .persist()
should allow setting the lowest priority? It could be an argument or a separate method.
from undici.
I'm not really in sync with changing the behaviour to sync with other test runners. Overall the intention of Undici
mocks seems to be as declarative as possible and scoped to single test scenarios (even if there are a few dozen, but that's more about test strategy and that's another topic).
That being said, I do see value in having a way to reset the mocks from a fake Agent.
from undici.
Related Issues (20)
- Improve docs on Interceptors HOT 1
- WHATWG handling of URLs that include username:password (credentials) HOT 14
- Large number of parallel requests always result in an error HOT 2
- Content-Length header should be ignored by fetch HOT 4
- Fetching headers of small files causes node process to terminate after 8 seconds since GHSA-9f24-jqhm-jfcw HOT 1
- Implement HTTP caching HOT 9
- Only use one network request when identical requests are made HOT 6
- TypeError and Access Issues in jsdom Environment Post Update to undici 6.16.0 HOT 4
- websocket: a number of conditions fail the connection but do not emit an error event
- websocket: handle parser errors more consistently
- running autobahn test suite HOT 4
- Failing autobahn tests HOT 6
- Interceptors: add response decompress interceptor HOT 4
- Implement Request/Response `.bytes()` method
- permessage-deflate support in websocket HOT 1
- Optimization of websocket masking HOT 9
- bug: spreading `Headers` object has breaking behavior in versions between Node 20.12 and 20.13 HOT 9
- Limit the maximum numbers of open sockets HOT 2
- interceptors: move `throwOnError` to interceptor HOT 1
- interceptors: move signal handling to interceptor
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 undici.