-
-
Notifications
You must be signed in to change notification settings - Fork 517
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
remove response delay in node #205
remove response delay in node #205
Conversation
I’m not sure that no delay at all is the correct approach. For example calling an API and asserting that the spinner is showing may not work if the result is returned right away with no delay. I think that in node the default delay should be some small amount. Additionally it would be nice if one could override the default delay amount |
Hey, @andreawyss. Those are definitely valid concerns. I'll elaborate below.
I think conceptually, we are making a server as a given valid functionality for our tests, so our testing focus can shift to the units we are testing (React components, helper functions, etc.).
To test a loading state I wouldn't use implicit default delay at all. As its value is random, you are subjecting your test to race conditions and coupling it with MSW's internal default response delay time. I would rather advice to set an explicit long delay for testing a loading state. Our main concern is that even 5ms of default response time in Node can become minutes when a big amount of tests is executed. Perhaps it's exaggerated, but I find the usefulness of realistic server response time in E2E tests and debugging, so the mocking feels natural, while in unit/integration tests I think the focus is to assert the right state. What do you think about that?
We should definitely consider this! |
If the default delay amount can be overridden then I will always set it to at least 1ms when running in node. |
@UsamaHameed, @andreawyss, what do you say if we make the default response time equal 5ms? |
5ms is a good default value for when mocks run in node. Detecting if the environment is node by testing (typeof window === 'undefined') may not work when using jest-dom. |
That's a good question: when you run a test in jsdom environment, do you expect a tested component to behave as in actual DOM (implying realistic server response), or being isolated, as in not focusing on the realistic network communication? |
It doesn't matter. From a default behavior there are only 2 scenarios:
When running in node we assume we are in a test environment and not in a Server-Side Rendering App. |
0d567bf
to
056fca5
Compare
I have set the response delay in node to 5ms. |
@UsamaHameed, thanks for the great work! I believe Andy is right saying that using Maybe we should base the conditional logic on
|
src/context/delay.ts
Outdated
const getRandomServerResponseTime = () => | ||
Math.floor( | ||
const getRandomServerResponseTime = () => { | ||
if (typeof window === 'undefined') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a thought here - I noticed the conversation around using a different mechanism for determining whether the process was running inside node.
There's a library here that does this: https://github.com/iliakan/detect-node
Their mechanism is very simple though and it's just this:
// Only Node.JS has a process variable that is of [[Class]] process
module.exports = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
Perhaps you could either just pull this lib in, or just directly inline this function?
If you do inline this function, it might be worth extracting it as a function named detectNode
or something equivalent, just because this might turn out to be useful for other functionality in the project.
Just a thought!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, @citypaul. Thanks for the suggestion!
I think we can start by including that snippet directly as a utility function in the library. You're right, we may want to parametrize the behavior in other parts, depending on whether it's running in Node or not.
Hey, @UsamaHameed. Please let me know if you need any help with these changes! |
I am a little swamped with some work. I will get back to this very soon. Thank you! |
No rush, approach this only when you have time. Thank you for your contributions! |
@UsamaHameed, may I please ask you to wait a little with these changes? It appears that #219 will introduce the |
056fca5
to
a16c0fd
Compare
Changes
ctx.delay()
) to5ms
, when run in a NodeJS environment.GitHub