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

New rule to disallow using mock API in end-to-end test files #1256

Closed
mrazauskas opened this issue Oct 3, 2022 · 6 comments · Fixed by #1257
Closed

New rule to disallow using mock API in end-to-end test files #1256

mrazauskas opened this issue Oct 3, 2022 · 6 comments · Fixed by #1257
Labels

Comments

@mrazauskas
Copy link
Contributor

What about adding a rule which would disallows using mock APIs? I though it could be activated for a directory where end-to-end tests live and could ensure that test fixtures are used instead of mocks.

Usually I prefer to leave mocks and spies for unit tests and to avoid them in end-to-end tests. Just wanted to check what people smarter than me think? (;

@SimenB
Copy link
Member

SimenB commented Oct 3, 2022

I think a rule disallowing using module mocking makes sense. Would you also want fake time, jest.fn etc to be disallowed, or only jest.mock, jest.doMock etc.?

@SimenB SimenB added the new rule label Oct 3, 2022
@mrazauskas
Copy link
Contributor Author

Let’s think.

jest.fn, jest.mock, jest.spyOn, jest.doMock, jest.setMock – are good candidates be banned.

Disallowing fake time sounds tricky. I think there should be an option allowing to ban everything except faking time. Testing server side APIs or web apps might depend on fake time in the end-to-end tests. In general it sounds good to have this configuration option.

@SimenB
Copy link
Member

SimenB commented Oct 3, 2022

Sounds like you want to ban usage of jest.* in general. What APIs would you want? Only thing I can think of that's not mocking modules or mocking time is jest.retryTimes - at which point that one usage can probably be enabled via an eslint comment

@mrazauskas
Copy link
Contributor Author

Also jest.setTimeout should be there. Probably.

@G-Rath
Copy link
Collaborator

G-Rath commented Oct 3, 2022

It sounds like we should do a no-restricted-jest-methods to complement no-restricted-matchers, and then you'd want to use both together for your specific use-case (as you'd want to disable use of matchers like toHaveBeenCalled)

@mrazauskas
Copy link
Contributor Author

Yep. That sounds right.

Actually my first idea was to suggest adding no-restricted-jest-methods, but it felt too abstract. The purpose did not look clear. Now after comments from Simen and you, I see that the first idea was right. The rest was pure overthinking ;D

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

Successfully merging a pull request may close this issue.

3 participants