<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Marek Rozmus</title>
    <description>The latest articles on DEV Community by Marek Rozmus (@marekrozmus).</description>
    <link>https://dev.to/marekrozmus</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F579546%2Fcb0b40f9-ee1e-4992-8ce8-6de1cd037253.jpeg</url>
      <title>DEV Community: Marek Rozmus</title>
      <link>https://dev.to/marekrozmus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marekrozmus"/>
    <language>en</language>
    <item>
      <title>Mocking setTimeout with Jest</title>
      <dc:creator>Marek Rozmus</dc:creator>
      <pubDate>Sat, 10 Sep 2022 11:58:54 +0000</pubDate>
      <link>https://dev.to/marekrozmus/mocking-settimeout-with-jest-1jhl</link>
      <guid>https://dev.to/marekrozmus/mocking-settimeout-with-jest-1jhl</guid>
      <description>&lt;p&gt;The title of this post may be a little misleading as we won’t write any mocks but we need to use some functionality that Jest provides. Let’s start!&lt;/p&gt;

&lt;p&gt;Here is an example of a simple component:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The whole code can be found in this GitHub repository: &lt;a href="https://github.com/marekrozmus/blog_mocking_settimeout_with_jest"&gt;https://github.com/marekrozmus/blog_mocking_settimeout_with_jest&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is component with three buttons and every click handler contains a &lt;code&gt;setTimeout&lt;/code&gt; usage.&lt;/p&gt;

&lt;p&gt;Writing the (incorrect) test may result in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;very fast test run but with incorrect results— the test skips all the &lt;code&gt;setTimeout&lt;/code&gt; logic, it just doesn’t wait until &lt;code&gt;setTimeout&lt;/code&gt; finishes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;it will take long time (but 5 seconds at most) — the test is wating for &lt;code&gt;setTimeout&lt;/code&gt; to finish but in our case it is longer (10 seconds) than default test’s duration allowed in Jest. We will get a following error:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YLfRggjZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sww0nandbr8ddkm7odi0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YLfRggjZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sww0nandbr8ddkm7odi0.png" alt="Exceeded timeout of 5000 ms for a test" width="880" height="51"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We could increase timeout for unit tests but it is not a good practice. Usually when test exceeds timeout, it means that it is broken. Most probably &lt;code&gt;async&lt;/code&gt; / &lt;code&gt;await&lt;/code&gt; is missing somewhere or some API calls are missing mocks.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to do with &lt;code&gt;setTimeout&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;Jest has a built in mechanism to handle such situation — the &lt;a href="https://jestjs.io/docs/timer-mocks"&gt;timer mocks&lt;/a&gt;. When we enable them we can “fast-forward time” inside the test.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use fake timers
&lt;/h2&gt;

&lt;p&gt;We need to place the testing code between &lt;code&gt;jest.useFakeTimers()&lt;/code&gt; and &lt;code&gt;jest.useRealTimers()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The first one is setting up fake timers and the second one restores the original JS behaviour.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 1 — setTimeout calls some API
&lt;/h2&gt;

&lt;p&gt;The simplest case. The &lt;code&gt;setTimeout&lt;/code&gt; callback is calling some function.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We just tell the Jest that all timers should complete. After that we can test if the callback logic was executed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 2 — &lt;code&gt;setTimeout&lt;/code&gt; updates component’s state
&lt;/h2&gt;

&lt;p&gt;In this case we can encounter the “not wrapped in act(…)” error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--76sg7SqA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1ovwzkhgsyu3pmaql2b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--76sg7SqA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1ovwzkhgsyu3pmaql2b.png" alt="Not wrapped in act" width="880" height="68"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is because the &lt;code&gt;setTimeout&lt;/code&gt; callback is updating the component’s state. The Jest’s method that complete the timers needs to be wrapped in &lt;code&gt;act&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Example 3 — setTimeout throws an exception
&lt;/h2&gt;

&lt;p&gt;The callback method is throwing an exception after 10 seconds. In this case we need to pass a function to &lt;code&gt;expect&lt;/code&gt; as following:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In above example first we expect that after 9 seconds there won’t be an exception. We expect it after the timer’s finish.&lt;/p&gt;

&lt;h2&gt;
  
  
  fireEvent vs userEvent
&lt;/h2&gt;

&lt;p&gt;There is one more thing worth to mention. The main difference in writing test when using &lt;code&gt;fireEvent&lt;/code&gt; or &lt;code&gt;userEvent&lt;/code&gt; to click the button.&lt;/p&gt;

&lt;p&gt;When using &lt;code&gt;fireEvent&lt;/code&gt; we get synchronous call to click. No additional setup is needed to test &lt;code&gt;setTimeout&lt;/code&gt; with fake timers.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;userEvent&lt;/code&gt; it is different. Since version &lt;a href="https://github.com/testing-library/user-event/releases/tag/v14.0.0"&gt;14.0.0&lt;/a&gt; the APIs always return a Promise. Because of that we need to make the whole test async and await on clicking with &lt;code&gt;userEvent&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Other thing is that we need to &lt;a href="https://testing-library.com/docs/user-event/setup"&gt;setup&lt;/a&gt; the &lt;code&gt;userEvent&lt;/code&gt; instance to properly behave with Jest’s fake timers. We need to set it up as following:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That’s it. Thank you for reading and leave me a comment if you have some question or proposal for making this post better.&lt;/p&gt;

&lt;p&gt;Check also my other posts — happy testing :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/froostrat"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6xuvF4dx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g1tifscty74ayzn0vxpl.png" alt="Buy me a coffee" width="250" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check also my other posts about unit testing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://marek-rozmus.medium.com/mocking-window-object-d316050ae7a5?source=friends_link&amp;amp;sk=9d8ddce0b5bc37cdab0c721cbf617c6e"&gt;Mocking window object with Jest&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://marek-rozmus.medium.com/jest-mock-and-spy-mockclear-vs-mockreset-vs-mockrestore-f52395581950?source=friends_link&amp;amp;sk=0958f2572071d35d8b3061491040f3ed"&gt;When to use mockClear, mockReset and mockRestore with mock and spyOn.&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>testing</category>
      <category>jest</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Swipeable list component for react</title>
      <dc:creator>Marek Rozmus</dc:creator>
      <pubDate>Mon, 15 Feb 2021 13:37:28 +0000</pubDate>
      <link>https://dev.to/marekrozmus/swipeable-list-component-for-react-1g24</link>
      <guid>https://dev.to/marekrozmus/swipeable-list-component-for-react-1g24</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FeHfKN7R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cqk3vvnvxn0sb21nnswl.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FeHfKN7R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cqk3vvnvxn0sb21nnswl.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I've created a react component that imitates iOS and Android swipe actions. It works both with mouse and touch events so it should be OK on mobile.&lt;/p&gt;

&lt;p&gt;There are several props that can change behaviour of this component and tried to used as much as possible &lt;a href="https://kentcdodds.com/blog/compound-components-with-react-hooks"&gt;Compound Components&lt;/a&gt; and hooks. I was wondering how these react patterns will work in a module.&lt;/p&gt;

&lt;p&gt;You can find this component on &lt;a href="https://github.com/marekrozmus/react-swipeable-list"&gt;GitHub&lt;/a&gt;, check how this work on &lt;a href="https://marekrozmus.github.io/react-swipeable-list/"&gt;this site&lt;/a&gt; or try it on &lt;a href="https://codesandbox.io/s/github/marekrozmus/react-swipeable-list/tree/main/examples"&gt;CodeSandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Still working on "Microsoft Outlook" version so stay tuned :)&lt;/p&gt;

&lt;p&gt;All comments are welcome :)&lt;/p&gt;

</description>
      <category>react</category>
      <category>showdev</category>
      <category>webdev</category>
      <category>news</category>
    </item>
  </channel>
</rss>
