✅ Mock options in test
              
              #1039
            
            Replies: 3 comments 2 replies
-
| 
         Hello @orimdominic , thank you for appreciating the efforts of me and contributors. In order to achieve your desire of mocking the email sending function, In your test, before importing the endpoint, you should mock the module containing that function. Please refer to Jest documentation on own modules mocking: https://jestjs.io/docs/manual-mocks#mocking-user-modules And StackOverflow. Use the  Without  const sendMail = jest.fn();
jest.mock('./mailUtil', () => ({ sendMail }));
import { myEndpoint } from './endpoint';
// in test:
expect(sendMail).toHaveBeenCalledWith();Something like that.  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         🚀 The whole thing about   | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         @orimdominic , there is now   | 
  
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello @RobinTail,
I must say that I am impressed by the developer experience that this library provides. Oh! I wish that this was Express itself.
I have also gone through pull requests and issues and oh my! Your work rate on this is amazing. I appreciate that!
I would like to know if we can create a mock for the
optionsobject that is passed to a request handler in tests, the same way we can forrequestandloggerusingrequestPropsandloggerPropsrespectively.A use case will be for a handler that attaches a method to
optionsthat sends emails, but during tests, we want this method to be mocked to avoid making requests to the mailing server. Let me show you what I mean in code:We have this endpoint:
We test it
We can also mock
options.sendMailto reject or throw in separate test and then test that an error message and an error status code were returned.Is there any way we can make this work? I am willing to collaborate to see it come to life.
Beta Was this translation helpful? Give feedback.
All reactions