<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>
	Comments on: Functional Programming Unit Testing in Node &#8211; Part 4	</title>
	<atom:link href="https://jessewarden.com/2018/06/functional-programming-unit-testing-in-node-part-4.html/feed" rel="self" type="application/rss+xml" />
	<link>https://jessewarden.com/2018/06/functional-programming-unit-testing-in-node-part-4.html</link>
	<description>Software &#124; Fitness &#124; Gaming</description>
	<lastBuildDate>Thu, 23 Aug 2018 01:36:07 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>
		By: Jesse Warden		</title>
		<link>https://jessewarden.com/2018/06/functional-programming-unit-testing-in-node-part-4.html/comment-page-1#comment-250916</link>

		<dc:creator><![CDATA[Jesse Warden]]></dc:creator>
		<pubDate>Thu, 23 Aug 2018 01:36:07 +0000</pubDate>
		<guid isPermaLink="false">http://jessewarden.com/?p=5561#comment-250916</guid>

					<description><![CDATA[&lt;blockquote&gt;...focusing on building a set of tests for EACH individual function...&lt;/blockquote&gt;

Yep, agreed. If you follow Kent Beck&#039;s TDD ideas, you should only be testing the feature/public API, like you said &lt;code&gt;sendEmail&lt;/code&gt;. If you setup a few good stubs for that, you&#039;d get 100% coverage as well and could move onto the other tests. Then if you changed the implementation, you wouldn&#039;t have to change all the tests I wrote for the individual units.

I&#039;m not covering TDD, though, just why pure functions are cool: only need stubs, easy for you to test your ideas, and you have massive confidence when you wire them together. Nothing wrong, everything right with deleting these tests once your public interface is legit. Not a panacea, though, for sure.]]></description>
			<content:encoded><![CDATA[<blockquote><p>&#8230;focusing on building a set of tests for EACH individual function&#8230;</p></blockquote>
<p>Yep, agreed. If you follow Kent Beck&#8217;s TDD ideas, you should only be testing the feature/public API, like you said <code>sendEmail</code>. If you setup a few good stubs for that, you&#8217;d get 100% coverage as well and could move onto the other tests. Then if you changed the implementation, you wouldn&#8217;t have to change all the tests I wrote for the individual units.</p>
<p>I&#8217;m not covering TDD, though, just why pure functions are cool: only need stubs, easy for you to test your ideas, and you have massive confidence when you wire them together. Nothing wrong, everything right with deleting these tests once your public interface is legit. Not a panacea, though, for sure.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: jmilkiewicz		</title>
		<link>https://jessewarden.com/2018/06/functional-programming-unit-testing-in-node-part-4.html/comment-page-1#comment-250915</link>

		<dc:creator><![CDATA[jmilkiewicz]]></dc:creator>
		<pubDate>Wed, 22 Aug 2018 18:10:31 +0000</pubDate>
		<guid isPermaLink="false">http://jessewarden.com/?p=5561#comment-250915</guid>

					<description><![CDATA[Hey

I  am waiting for the update of this post.  Regarding your comment: I fully support having tests like unit, property, integration, integrated (e2e) tests. Unfortunately these terms are very often understood in a different ways by different people...
My biggest concern regarding what you showed in this series is focusing on building a set of tests for EACH individual function. 
I prefer to employ hexagonal architecture (ports and adapters) and build a set of unit tests for given &quot;functionality&quot; (input port functionality) instead of tests each &quot;internal function&quot; individually.  
In this case i would only create a set of unit tests (with stubs and spies as test doubles for external services like reading template, getUserEmail, creating transport ) for sendEmail function (having the same signature as the one you have). It would allow me to cover all scenarios (based on different input/different stubs) of this function  by asserting sendEmail  side effects (if we have any ) and  its return value. I will not test any of internal functions like : getEmailTemplateAndAttachments, renderEmailAndGetEmailService, filterCleanFilesAndMapToAttachments, createMailOptions and more. At the current stage these are not my &quot;units of reusability&quot; - they are only &quot;helpers&quot; (albeit very useful) for building sendEmail function.  All that allows me to easily refactor sendEmail - as long as all tests for sendEmail are passing i can ruthlessly refactor it (yes i will drive it to extract as many pure functions as possible).
 
After i am done with these unit tests, i would build some integration tests for code communicating with outside world (can use nock or real implementations) and at the end i would build a single e2e test which would execute sendEmail  function with real adapters passed.]]></description>
			<content:encoded><![CDATA[<p>Hey</p>
<p>I  am waiting for the update of this post.  Regarding your comment: I fully support having tests like unit, property, integration, integrated (e2e) tests. Unfortunately these terms are very often understood in a different ways by different people&#8230;<br />
My biggest concern regarding what you showed in this series is focusing on building a set of tests for EACH individual function.<br />
I prefer to employ hexagonal architecture (ports and adapters) and build a set of unit tests for given &#8220;functionality&#8221; (input port functionality) instead of tests each &#8220;internal function&#8221; individually.<br />
In this case i would only create a set of unit tests (with stubs and spies as test doubles for external services like reading template, getUserEmail, creating transport ) for sendEmail function (having the same signature as the one you have). It would allow me to cover all scenarios (based on different input/different stubs) of this function  by asserting sendEmail  side effects (if we have any ) and  its return value. I will not test any of internal functions like : getEmailTemplateAndAttachments, renderEmailAndGetEmailService, filterCleanFilesAndMapToAttachments, createMailOptions and more. At the current stage these are not my &#8220;units of reusability&#8221; &#8211; they are only &#8220;helpers&#8221; (albeit very useful) for building sendEmail function.  All that allows me to easily refactor sendEmail &#8211; as long as all tests for sendEmail are passing i can ruthlessly refactor it (yes i will drive it to extract as many pure functions as possible).</p>
<p>After i am done with these unit tests, i would build some integration tests for code communicating with outside world (can use nock or real implementations) and at the end i would build a single e2e test which would execute sendEmail  function with real adapters passed.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: JesterXL		</title>
		<link>https://jessewarden.com/2018/06/functional-programming-unit-testing-in-node-part-4.html/comment-page-1#comment-250909</link>

		<dc:creator><![CDATA[JesterXL]]></dc:creator>
		<pubDate>Mon, 20 Aug 2018 21:42:11 +0000</pubDate>
		<guid isPermaLink="false">http://jessewarden.com/?p=5561#comment-250909</guid>

					<description><![CDATA[You&#039;re right, thanks for the find! Nice catch. I&#039;ll fix the post.

Your mind is in the RIGHT place, this is exactly the weakness unit tests have whether they use FP programming with stubs, or non-FP with mocks and stubs: if the stub is wrong, and the test passes, it&#039;s a false positive despite 101% test coverage. This is exactly the reason for integration tests.

I&#039;d caution against &lt;code lang=&quot;javascript&quot;&gt;spy&lt;/code&gt;, though, as he&#039;s just a mock that can act as a stub; i.e. he is explicitly made for testing side effects. That&#039;s the point of this whole series; to avoid mocks, to avoid creating side effects.

&lt;blockquote&gt;&quot;...too much focused on particular functions (units) not on behaviour&quot;&lt;/blockquote&gt;

&lt;b&gt;YES!&lt;/b&gt; Absolutely. You want to test units first. If those fail, you&#039;re behavior will fail. Units are faster to run with stubs that have no time, no side effects like network calls, reading disk, etc. AND can be run concurrently. Once those pass, then you run the property tests, like &lt;a href=&quot;https://github.com/jsverify/jsverify&quot; rel=&quot;nofollow&quot;&gt;jsverify&lt;/a&gt;, which are longer, random, and tackle every avenue of positive/negative tests around data types. Finally, you do the integration tests to ensure your actual interfaces are correct. Now, some of those can be mocked (supertest, nock, etc), but even if your local server just returns stubbed data, you can guarantee your contracts are correct. What you found is exactly what we want to find in integration tests... they are just longer and probably won&#039;t work if your unit tests fail.

Unit (like these posts show), property testing, integration testing, and if you have UI stuff, Functional Testing. ... I only focused on unit to show people how they don&#039;t need mocks.


.... now, do we HAVE to use integration testing to surface these issues (beyond hiring you full time to code with me, heh)?

No, there are 2 other options:
1. Strong Typing
2. Total Functions

Types would of caught that error before I even attempted to run the tests, in a second whether &lt;a href=&quot;https://flow.org/&quot; rel=&quot;nofollow&quot;&gt;Flow&lt;/a&gt; or &lt;a href=&quot;https://www.typescriptlang.org/&quot; rel=&quot;nofollow&quot;&gt;TypeScript&lt;/a&gt; . This is another cost of dynamic languages: propertyThatExists.somethingThatDoesnt === undefined, no error.

The second option is to use &lt;a href=&quot;http://jessewarden.com/2018/06/pure-function-vs-total-function.html&quot;&gt;total functions&lt;/a&gt;. If we did, &lt;code lang=&quot;javascript&quot;&gt;createMailOptions&lt;/code&gt; would returned an Error or rejected the Promise after validating the from was undefined.]]></description>
			<content:encoded><![CDATA[<p>You&#8217;re right, thanks for the find! Nice catch. I&#8217;ll fix the post.</p>
<p>Your mind is in the RIGHT place, this is exactly the weakness unit tests have whether they use FP programming with stubs, or non-FP with mocks and stubs: if the stub is wrong, and the test passes, it&#8217;s a false positive despite 101% test coverage. This is exactly the reason for integration tests.</p>
<p>I&#8217;d caution against <code lang="javascript">spy</code>, though, as he&#8217;s just a mock that can act as a stub; i.e. he is explicitly made for testing side effects. That&#8217;s the point of this whole series; to avoid mocks, to avoid creating side effects.</p>
<blockquote><p>&#8220;&#8230;too much focused on particular functions (units) not on behaviour&#8221;</p></blockquote>
<p><b>YES!</b> Absolutely. You want to test units first. If those fail, you&#8217;re behavior will fail. Units are faster to run with stubs that have no time, no side effects like network calls, reading disk, etc. AND can be run concurrently. Once those pass, then you run the property tests, like <a href="https://github.com/jsverify/jsverify" rel="nofollow">jsverify</a>, which are longer, random, and tackle every avenue of positive/negative tests around data types. Finally, you do the integration tests to ensure your actual interfaces are correct. Now, some of those can be mocked (supertest, nock, etc), but even if your local server just returns stubbed data, you can guarantee your contracts are correct. What you found is exactly what we want to find in integration tests&#8230; they are just longer and probably won&#8217;t work if your unit tests fail.</p>
<p>Unit (like these posts show), property testing, integration testing, and if you have UI stuff, Functional Testing. &#8230; I only focused on unit to show people how they don&#8217;t need mocks.</p>
<p>&#8230;. now, do we HAVE to use integration testing to surface these issues (beyond hiring you full time to code with me, heh)?</p>
<p>No, there are 2 other options:<br />
1. Strong Typing<br />
2. Total Functions</p>
<p>Types would of caught that error before I even attempted to run the tests, in a second whether <a href="https://flow.org/" rel="nofollow">Flow</a> or <a href="https://www.typescriptlang.org/" rel="nofollow">TypeScript</a> . This is another cost of dynamic languages: propertyThatExists.somethingThatDoesnt === undefined, no error.</p>
<p>The second option is to use <a href="http://jessewarden.com/2018/06/pure-function-vs-total-function.html">total functions</a>. If we did, <code lang="javascript">createMailOptions</code> would returned an Error or rejected the Promise after validating the from was undefined.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: jmilkiewicz		</title>
		<link>https://jessewarden.com/2018/06/functional-programming-unit-testing-in-node-part-4.html/comment-page-1#comment-250877</link>

		<dc:creator><![CDATA[jmilkiewicz]]></dc:creator>
		<pubDate>Thu, 16 Aug 2018 08:54:05 +0000</pubDate>
		<guid isPermaLink="false">http://jessewarden.com/?p=5561#comment-250877</guid>

					<description><![CDATA[Hey

Unfortunately your code is broken. Function getEmailService is returning Maybe and you are not handling that Maybe anywhere.  In your  sendEmail function you treat emailService as a regular object but it will be either Just or Nothing. I was expecting this to be uncovered somehow... maybe with integration tests but in your integration test you simply copy&#039;n paste  (with minor modifications) sendEmailSafe from server.js to sandbox.js so you are testing only sending functionality not full end2end behaviour (which is actually not bad albeit it doesn&#039;t allow one to uncover the bug).
One solution would be to simply spy createTransport function to check  if it receives a valid parameter (an object with host, port and and secure:false)
 
My biggest concern regarding your approach is that it is too much  focused on particular functions (units) not on behaviour. It is a matter of TDD vs BDD]]></description>
			<content:encoded><![CDATA[<p>Hey</p>
<p>Unfortunately your code is broken. Function getEmailService is returning Maybe and you are not handling that Maybe anywhere.  In your  sendEmail function you treat emailService as a regular object but it will be either Just or Nothing. I was expecting this to be uncovered somehow&#8230; maybe with integration tests but in your integration test you simply copy&#8217;n paste  (with minor modifications) sendEmailSafe from server.js to sandbox.js so you are testing only sending functionality not full end2end behaviour (which is actually not bad albeit it doesn&#8217;t allow one to uncover the bug).<br />
One solution would be to simply spy createTransport function to check  if it receives a valid parameter (an object with host, port and and secure:false)</p>
<p>My biggest concern regarding your approach is that it is too much  focused on particular functions (units) not on behaviour. It is a matter of TDD vs BDD</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
