New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rename "composeMocks" to better describe what it does #100
Comments
@hehehai, @redraushan, as valuable contributors, what is your opinion on this API change? What consequences do you see this introducing? |
Hi @kettanaito, Although,
What do you think @hehehai ? |
Dropping the
|
@kettanaito, anything finalised if we are keeping |
I do love how |
@kettanaito |
As MSW will eventually support mocking in non-browser environments, I think that such initializer functions like I suggest the following API:
|
‘useWorker()’ and ‘useServer()’ sounds clean to me, I think we can move forward with this. |
in React ‘use’ is the prefix for React hooks. I propose we use the name ‘run’. runMockServiceWorker |
@andreawyss, you're right about the hook prefix. We've had an internal discussion, and that React convention has came up as well. I like how I believe such API is misleading: import { runMockWorker } from 'msw'
// Now I expect that the worker runs after I call this function,
// but in reality it's not.
const { start, stop } = runMockWorker(...)
start() Perhaps, there's a closer replacement to |
// Browser usage
import { setupServiceWorker } from 'msw'
const { start, stop } = setupServiceWorker(options)
start()
// Node usage
import { setupMockServer } from 'msw'
setupMockServer(options) |
@andreawyss, that looks lovely! What do you think if we drop the "Service" part, and it becomes: // Browser usage
setupMockWorker()
// Node usage
setupMockServer() I'm also thinking that perhaps it would be a good idea to delegate "starting" the mocked server to the usage surface. That is mainly for two reasons:
This makes the request handler itself asynchronous.
import { setupMockServer } from 'msw'
import handlers from '../shared/handlers'
describe('Test', () => {
beforeAll(async () => {
const mock = setupMockServer(handlers)
// Or whichever method that's suitable in the context
await mock.open()
})
afterAll(async () => {
await mock.close()
})
}) |
Great. I like the symmetry of the setup function names. Also the default response latency (delay) would be about 700 ms for the mock worker and about 5ms for the mock server. These values can be changed in the options passed to the setup functions. const mockWorker = setupMockWorker({ handlers, latency: 1000 })
const mockServer = setupMockServer({ handlers, latency: 60 }) Using an option object will allow in the future to add new options as needed. |
@andreawyss, agree on the options object. Actually, version |
Thank you everybody for this discussion! I've decided to go with the following options:
I'm omitting the "mock" part taking in consideration that you import those functions from a library that does mocking already, and it should be a pre-requisite in understanding. |
Is your feature request related to a problem? Please describe.
I find it confusing that
composeMocks()
function composes request handlers, and not mocks.Describe the solution you'd like
I think renaming
composeMocks()
function tocomposeHandlers()
would provide a more domain-driven name, and won't confuse the users.Benefits of the change
Drawbacks of the change
Alternatives to consider
I was also thinking about simply naming it
compose()
, but I'm afraid this will clash with the similar functional composition utility that is often calledcompose()
. Also,composeMocks()
is not the traditional compose function per-say, as it's logic is specific to MSW.The text was updated successfully, but these errors were encountered: