Skip to content
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

Create manual mock for logger #2466

Closed
luminousleek opened this issue Mar 16, 2024 · 2 comments
Closed

Create manual mock for logger #2466

luminousleek opened this issue Mar 16, 2024 · 2 comments

Comments

@luminousleek
Copy link
Contributor

Please confirm that you have searched existing issues in the repo

Yes, I have searched the existing issues

Any related issues?

#2099

What is the area that this feature belongs to?

Testing

Is your feature request related to a problem? Please describe.

To test for logger input, the logger is mocked and then calls to the logger are checked (see #2463 for an example). However, this must be done every for every file that the logger is mocked in, which is a bit annoying.

Describe the solution you'd like

Create a manual mock of the logger, so that we can just call jest.mock('logger'); at the start of each test. The default implementation of manual mocks can also be overridden though it's a bit more finnicky to do so.

Describe alternatives you've considered

No response

Additional context

No response

@kaixin-hc
Copy link
Contributor

Cool suggestion, I think this makes sense if the setup code for the logger is long. Just wondering which functions you want to mock with the logger? If the mock is just warn, info, error for example, there might be an argument made for mocking them at the start of each test case instead so that the tests are totally independent and not based on outside "logic"

@luminousleek
Copy link
Contributor Author

Cool suggestion, I think this makes sense if the setup code for the logger is long. Just wondering which functions you want to mock with the logger? If the mock is just warn, info, error for example, there might be an argument made for mocking them at the start of each test case instead so that the tests are totally independent and not based on outside "logic"

Actually after thinking and reading about this a bit more I realise there isn't much need to do a manual mock for the logger since as discussed in #2099 it makes more sense to test for logger calls than logger outputs. So the automatic mock of jest.mock('/path/to/logger'); would work just fine since it replaces all the methods with another method that just returns undefined.

I think I'll close this issue then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants