You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Zimic currently only applies HTTP mocks to the process where the interceptor is called. This makes it unusable in multi-process scenarios.
An example that illustrates this case:
A route handler of a Next.js application is being tested using Vitest.
The Next.js server was started as a separate process using next dev.
The Vitest tests make HTTP requests to the route handler running on localhost.
The route handler access an external service. This service is not guaranteed to be running all the time and, as we have no control of its availability, the tests should not reach it directly.
This is a great use case for Zimic, since mocking external applications with type safety is one of our goals. However, this scenario is not currently supported. For example, a test of this application could look like:
The Vitest test initializes an interceptor and applies a mock to the external service. It is useful to keep the interceptor mock declarations in the test to easily simulate behaviors.
The route handler running on the second process is not affected and requests makes by the Vitest test to the route handler still reach the external service.
Archived proposals
Proposal 1: mocks applied directly to the remote process
Solution
A feature that would extend Zimic to this use case would be the introduction of "remote HTTP interceptors". Remote interceptors are similar to our current interceptors, but they apply mocks to remote processes instead of the one the interceptor is initialized.
Using the example above, the workflow using remote interceptors would be like:
The Next.js application would expose an endpoint (e.g. /zimic), available only in development and testing. This endpoint would be used by Zimic to receive mocks definitions and apply them to the Next.js process.
The Vitest test would create a remote interceptor, linking it to the endpoint made available by the Next.js application.
When applying mocks or doing any operation on the remote interceptor, a request would be issued to the endpoint on the Next.js application. In other words, declaring a mock in the Vitest test would actually send it to the Next,js application, where it would be applied. This would mean that all methods of remote interceptors and request trackers would need to be asynchronous.
The external service would no longer be reached in tests, as the Vitest test would be able to apply mocks remotely to the server application.
API
// creates a RemoteHttpInterceptorconstremoteInterceptor=createHttpInterceptor<{ ... }>({type: 'remote'// 'local' is the defaultremoteURL: 'http://localhost:3000/zimic',})constgetTracker=awaitremoteInterceptor.get('/').respond({status: 200,body: {success: true}})// mock applied to remote applicationconstgetRequests=awaitgetTracker.requests()expect(getRequests).toHaveLenght(1)
Proposal 2: Zimic mock server
Solution
A feature that would extend Zimic to this use case would be the introduction of remote HTTP interceptors. Remote interceptors are similar to our current interceptors, but they apply mocks to a remote Zimic server process, instead of the process where the interceptor is initialized.
Using the example above, the workflow using remote interceptors would be like:
In development or testing, a Zimic mock server would need to be started alongside the Next.js application. The Zimic server would be used to receive and apply mock definitions.
The Next.js application would route requests intent to the external service to the Zimic server.
The Vitest test would create a remote worker and interceptors, linking them to the Zimic mock server in use by the Next.js application.
When doing any operation on the remote interceptor, such as declaring mocks, the operation would be sent to the Zimic server to apply it. In other words, declaring a mock in the Vitest test would actually send it to the Zimic server. This would allow simulating scenarios of the external service responses in tests. For this to work, all methods of remote interceptors and request trackers would need to be asynchronous.
The external service would no longer be reached in tests, as the Vitest test would be able to apply mocks remotely to the Zimic mock server in use by the Next.js application. 🎉
API examples
Using local workers and interceptors:
typeInterceptorSchema=HttpInterceptorSchema.Root<{'/': {GET: {response: {200: {body: {success: true}};};};};}>;constlocalWorker=createWorker({type: 'local',});awaitlocalWorker.start();// starts the local worker, applies the mocks to the local process// creates an HttpInterceptorconstlocalInterceptor=createHttpInterceptor<InterceptorSchema>({worker: localWorker,baseURL: 'http://localhost:3000',});// applying remote mocks is a asynchronous operationconstgetTracker=localInterceptor.get('/').respond({status: 200,body: {success: true},});// mock applied to local processconstgetRequests=getTracker.requests();expect(getRequests).toHaveLength(1);
Using remote workers and interceptors:
typeInterceptorSchema=HttpInterceptorSchema.Root<{'/': {GET: {response: {200: {body: {success: true}};};};};}>;constremoteWorker=createWorker({type: 'remote',serverURL: 'http://localhost:3001',// the URL of the zimic mock server});// start the zimic mock server with:// zimic server start --port 3001 --on-ready 'npm test' --ephemeral trueawaitremoteWorker.start();// starts the remote worker and links it to the zimic mock server// creates a RemoteHttpInterceptorconstremoteInterceptor=createHttpInterceptor<InterceptorSchema>({worker: remoteWorker,// the application should use the base URL of the external service as http://localhost:3001/service1pathPrefix: '/service1',});// applying remote mocks is an asynchronous operationconstgetTracker=awaitremoteInterceptor.get('/').respond({status: 200,body: {success: true},});// mock applied to remote server:// any requests to http://localhost:3001 will be handled by the zimic mock server with the mock applied// getting requests is also an asynchronous operationconstgetRequests=awaitgetTracker.requests();expect(getRequests).toHaveLength(1);
The text was updated successfully, but these errors were encountered:
Context
Zimic currently only applies HTTP mocks to the process where the interceptor is called. This makes it unusable in multi-process scenarios.
An example that illustrates this case:
next dev
.localhost
.This is a great use case for Zimic, since mocking external applications with type safety is one of our goals. However, this scenario is not currently supported. For example, a test of this application could look like:
Archived proposals
Proposal 1: mocks applied directly to the remote process
Solution
A feature that would extend Zimic to this use case would be the introduction of "remote HTTP interceptors". Remote interceptors are similar to our current interceptors, but they apply mocks to remote processes instead of the one the interceptor is initialized.
Using the example above, the workflow using remote interceptors would be like:
/zimic
), available only in development and testing. This endpoint would be used by Zimic to receive mocks definitions and apply them to the Next.js process.API
Proposal 2: Zimic mock server
Solution
A feature that would extend Zimic to this use case would be the introduction of remote HTTP interceptors. Remote interceptors are similar to our current interceptors, but they apply mocks to a remote Zimic server process, instead of the process where the interceptor is initialized.
Using the example above, the workflow using remote interceptors would be like:
API examples
Using local workers and interceptors:
Using remote workers and interceptors:
The text was updated successfully, but these errors were encountered: