{"id":7116,"date":"2019-06-23T12:57:09","date_gmt":"2019-06-23T12:57:09","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7116"},"modified":"2019-06-23T12:57:09","modified_gmt":"2019-06-23T12:57:09","slug":"jest-a-little-bit-more","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/jest-a-little-bit-more\/","title":{"rendered":"Jest (a little bit more)"},"content":{"rendered":"<p>We&#8217;ve covered rudimentary use of Jest as part of React testing, also in a stand alone, now let&#8217;s look a little more in depth at common testing requirements.<\/p>\n<p><strong>Unit Test<\/strong><\/p>\n<p>We write tests using Jest like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ntest('Some test description', () =&gt; {\r\n});\r\n<\/pre>\n<p><em>Note: there are various aliases such as &#8220;it&#8221;, &#8220;fit&#8221;, &#8220;xit&#8221; or &#8220;xtest&#8221; that can be used in place of &#8220;test&#8221;.<\/em><\/p>\n<p>Obviously the <em>&#8220;Some test description&#8221;<\/em> text should be something meaningful, i.e. what is being tested, any specific inputs and what&#8217;s the expectation. Ofcourse how much or how little you write as a description is upto the developer. I tend to write what method is being tested, with any special conditions and expected outcome simply because when the tests run I get a good set of information on what&#8217;s working and what&#8217;s not, however this is down to the developer&#8217;s specific tastes.<\/p>\n<p>We can group tests together within a <em>describe<\/em> function which may allow you to reduce the verbosity of your test descriptions as well as logically group tests, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ndescribe('fetch component', () =&gt; {\r\n    test(&quot;missing URL, expect empty response&quot;, () =&gt; {\r\n    });\r\n\r\n    test(&quot;missing invalid, expect error&quot;, () =&gt; {\r\n    });\r\n});\r\n<\/pre>\n<p><em>Note: there are a couple of aliases such as &#8220;fdescribe&#8221; and &#8220;xdescribe&#8221; which can be used in place of &#8220;describe&#8221;.<\/em><\/p>\n<p>The output of the Jest test runner would be something like this<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nfetch component\r\n   \u221a missing URL\r\n   \u221a missing invalid\r\n<\/pre>\n<p><strong>Timeout<\/strong><\/p>\n<p>The <em>test<\/em> function also accepts a third argument which can be a timeout (specified in milliseconds). The default timeout is 5 seconds. So for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ntest(&quot;missing URL, expect empty response&quot;, () =&gt; {\r\n}, 100);\r\n<\/pre>\n<p><strong>Paramterized tests<\/strong><\/p>\n<p>Jest supports paramterized tests using the <em>each<\/em> function, so for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ntest.each`\r\n   input         | output\r\n   ${1}          | ${10} \r\n   ${5}          | ${50} \r\n   ${10}         | ${100} \r\n  `(&quot;should return $output when $input is used&quot;, ({input, output}) =&gt; {\r\n   expect(input * 10).toBe(output);\r\n});\r\n<\/pre>\n<p>Using a &#8220;table&#8221;-like layout of data, the first row made up of column names which map to the input(s) and output along with subsequent rows of inputs and outputs to match against delimited using the pipe | operator. <\/p>\n<p>You can also pass in arrays instead of a table structure, i.e. <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ntest.each(&#x5B;&#x5B;1, 10], &#x5B;5, 50], &#x5B;10, 100]])\r\n   (`should return %i when %i is used`, (input, output) =&gt; {\r\n   expect(input * 10).toBe(output);\r\n});\r\n<\/pre>\n<p>We can also supply parameters using the each function of <em>describe<\/em> meaning we can pass the same parameters through multiple tests. Here&#8217;s an example of the above two way of passing parameterized data, but using describe<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ndescribe.each`\r\n  input         | output\r\n  ${1}          | ${10} \r\n  ${5}          | ${50} \r\n  ${10}         | ${100} \r\n  `(&quot;multiplication with input $input, output $output&quot;, ({input, output}) =&gt; {\r\n\r\n  test(&quot;should match&quot;, () =&gt; {\r\n    expect(input * 10).toBe(output);\r\n  });\r\n})  \r\n\r\ndescribe.each(&#x5B;&#x5B;1, 10], &#x5B;5, 50], &#x5B;10, 100]])\r\n(`multiplication %i, %i`, (input, output) =&gt; {\r\n\r\n  test(&quot;should match&quot;, () =&gt; {\r\n    expect(input * 10).toBe(output);\r\n  });\r\n});\r\n<\/pre>\n<p><strong>Only run these tests<\/strong><\/p>\n<p>During development we might wish to turn off tests whilst we work on a specific set of tests, we can use the <em>only<\/em> function on either describe or test to basically just run those tests with <em>only<\/em> specified. For example the following syntax (followed by the normal describe and test parameters)<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ndescribe.only \r\ndescribe.only.each\r\ntest.only\r\ntest.only.each\r\n<\/pre>\n<p><strong>Skipping tests<\/strong><\/p>\n<p>We can run <em>only<\/em> certain tests and the opposite (i.e. run all tests except these), skipping tests using the skip<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ndescribe.skip\r\ndescribe.skip.each\r\ntest.skip\r\ntest.skip.each\r\n<\/pre>\n<p><strong>Todo<\/strong><\/p>\n<p>With @types\/jest version 24 the <em>todo<\/em> function was added to <em>test<\/em>. With this we might have ideas for our tests and want to quickly create stub methods, so we would write<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ntest.todo('multiple number test');\r\n<\/pre>\n<p>No function should be supplied to a <em>todo test<\/em> and the Jest tester runner will show the test as a &#8220;todo&#8221; test within it&#8217;s output.<\/p>\n<p><strong>Lifecycle functions<\/strong><\/p>\n<p>Most unit testing frameworks include the ability to carry out some process before or after a test, for example setting up a test and cleanup, Jest is no different &#8211; including <em>beforeAll<\/em>, <em>beforeEach<\/em>, <em>afterAll<\/em> and <em>afterEach<\/em> functions.<\/p>\n<p>As the names suggest, <em>beforeAll<\/em> and <em>afterAll<\/em> are invoked before the tests start and after the tests all complete, respectively. As you&#8217;ll have worked out <em>beforeEach<\/em> and <em>afterEach<\/em> will be invoked before and after each test is run, respectively. <\/p>\n<p>All life cycle functions accept a function to invoke and an optional timeout.<\/p>\n<p><strong>Expectations<\/strong><\/p>\n<p>Obviously these previously mentioned tests will all succeed unless we include expectations\/assertions. Jest includes the <em>expect<\/em> function which you&#8217;ll often pass the value your system under test returned (or set) along with a function such as <em>toBe<\/em> which is what you expect the value to be, for using our previous multiplication test<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nexpect(input * 10).toBe(output);\r\n<\/pre>\n<p>There&#8217;s a whole host of functions around <em>expect<\/em> listed here <a href=\"https:\/\/jestjs.io\/docs\/en\/expect\" rel=\"noopener noreferrer\" target=\"_blank\">&#8220;Expect&#8221;<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve covered rudimentary use of Jest as part of React testing, also in a stand alone, now let&#8217;s look a little more in depth at common testing requirements. Unit Test We write tests using Jest like this test(&#8216;Some test description&#8217;, () =&gt; { }); Note: there are various aliases such as &#8220;it&#8221;, &#8220;fit&#8221;, &#8220;xit&#8221; or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[245],"tags":[],"class_list":["post-7116","post","type-post","status-publish","format-standard","hentry","category-jest"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7116"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7116\/revisions"}],"predecessor-version":[{"id":7243,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7116\/revisions\/7243"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}