Mock function supported #93
-
Hello, Does pactumjs supports mock functions? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Hey! Pactum library supports mock http server to mock out any external dependencies while testing an application. |
Beta Was this translation helpful? Give feedback.
-
I am trying to mock a function not a server, and store it in a variable, and do some assertion in the end, I have found an example with jest as you can see below.
|
Beta Was this translation helpful? Give feedback.
-
Welcome to PactumJS Discussions & thanks a lot for using this library.Pactum doesn't support mocking functions. Usually mocking independent functions happens at unit test level. If you are writing unit tests, we can use libraries like sinon or jest for mocking. If you are writing API tests, we generally mock the external dependencies. I do understand that at some point particular dependencies cannot be mocked. As a work around, I prefer using env variables to control the behaviour. For example function getSecret() {
if (process.env.NODE_ENV === 'test') { return 'secret'; }
// or else actual code will be executed to fetch the secret
} Running the server in test environment. export NODE_ENV=test && node server.js The challenge with the above code is, it is not dynamic. |
Beta Was this translation helpful? Give feedback.
-
Hello ASaiAnudeep, mock.addInteraction('getLayouts');
mock.start(); And when I have step where I want to get layout Given('I have the layout', async () => {
layoutId = await pactum.spec().get('/layouts').expectStatus(200).returns('data[0].id');
console.log('layout: ', layoutId);
}); layoutId is not taken from the mocked response but I had rlayout: 1ca5b9a0-b009-11ec-a376-5de1ad1d5b31 handler: 'handler.addInteractionHandler('getLayouts', () => {
return {
strict: false,
request: {
method: 'GET',
path: `/layouts`,
},
response: {
status: 200,
body: {
data: [
{
id: '5de65005-8c14-4459-9a5b-44b70b6f8047',
industryId: '2e9d94ad-db1d-422d-b7e1-f56afad49493',
name: 'Layout test',
usecaseId: 'd855d913-4402-4e2a-bba7-38cf76b50427',
},
],
},
},
};` I have information that |
Beta Was this translation helpful? Give feedback.
Welcome to PactumJS Discussions & thanks a lot for using this library.
Pactum doesn't support mocking functions. Usually mocking independent functions happens at unit test level. If you are writing unit tests, we can use libraries like sinon or jest for mocking.
If you are writing API tests, we generally mock the external dependencies. I do understand that at some point particular dependencies cannot be mocked. As a work around, I prefer using env variables to control the behaviour. For example
Running the server in test environment.
export N…