Allow multiple instances of mock server #133
-
Hey, There are any way to have multiple instances for the mock server. For example: // service-1.mock.server.ts
import { mock } from 'pactum';
mock.addInteraction({
// my-service1-interaction
});
export {
start: mock.start,
stop: mock.end,
} // service-2.mock.server.ts
import { mock } from 'pactum';
mock.addInteraction({
// my-service2-interaction
});
export {
start: mock.start,
stop: mock.end,
} // my-service.integration.spec.ts
import service1MockServer from './mocks/service-1.mock.server';
import service2MockServer from './mocks/service-2.mock.server';
before(async () => {
await Promise.all([
service1MockServer.start(),
service2MockServer.start(),
])
});
afterAll(async () => {
await Promise.all([
service1MockServer.stop(),
service2MockServer.stop(),
])
})
describe('myService', () => {
it('should return the merge response', () => {
return pactum.spect()
.get('/my-service')
.expectStatus(200)
.expectJson({
// merged response from services
})
})
}) As I can see the mock module exports a static object shared across all the runtime envrionment. Maybe you can expose a helper method to create different mock instances servers and leave the default instance created. Something like this: function createMockServer() {
return {
_server: new Server(),
// ...the object as is
}
}
module.exports = createMockServer();
module.exports.createMockServer = createMockServer; What do you think? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @samuelsantia, Thank you for the suggestion. With current version of pactum there is no straightforward way to run multiple instances of mock server. In the earlier versions of this library, we used to support this feature but later it was decommissioned. For most of the use cases a single mock server is adequate to mock multiple services as most of them has unique endpoints and moreover pactum supports sophisticated matching rules to respond accurately. const { mock } = require('pactum');
// service one interactions
mock.addInteraction({
request: {
method: 'GET',
path: '/api/service-one/health'
},
response: {
status: 200
}
});
// service two interactions
mock.addInteraction({
request: {
method: 'GET',
path: '/api/service-two/health'
},
response: {
status: 200
}
});
mock.start(); |
Beta Was this translation helpful? Give feedback.
Hey @samuelsantia,
Thank you for the suggestion. With current version of pactum there is no straightforward way to run multiple instances of mock server. In the earlier versions of this library, we used to support this feature but later it was decommissioned.
For most of the use cases a single mock server is adequate to mock multiple services as most of them has unique endpoints and moreover pactum supports sophisticated matching rules to respond accurately.