<?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: Mike</title>
    <description>The latest articles on DEV Community by Mike (@thecodingways).</description>
    <link>https://dev.to/thecodingways</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%2F293065%2F5e9095ea-a276-4617-85ab-045c0f5c461a.png</url>
      <title>DEV Community: Mike</title>
      <link>https://dev.to/thecodingways</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thecodingways"/>
    <language>en</language>
    <item>
      <title>5 Javascript Unit Testing Mistakes You Must Avoid in React and Angular</title>
      <dc:creator>Mike</dc:creator>
      <pubDate>Sat, 22 Feb 2020 09:24:11 +0000</pubDate>
      <link>https://dev.to/thecodingways/5-javascript-unit-testing-mistakes-you-must-avoid-in-react-and-angular-56f2</link>
      <guid>https://dev.to/thecodingways/5-javascript-unit-testing-mistakes-you-must-avoid-in-react-and-angular-56f2</guid>
      <description>&lt;h3&gt;
  
  
  (My humble experience after writing 2349+ frontend tests)
&lt;/h3&gt;

&lt;p&gt;Exactly 2 years, one of the developers on my team told me that my code sucked because it didn’t have 100% test coverage.&lt;/p&gt;

&lt;p&gt;So, fair enough, I’d start listening to how much of holy grail testing was, and after listening to him, I found myself at a complete loss of words.&lt;/p&gt;

&lt;p&gt;Everything he said made sense. That’s why all my previous applications were so buggy and hard to change.&lt;/p&gt;

&lt;p&gt;“Hard to change” was an understatement, and “buggy” was a compliment.&lt;/p&gt;

&lt;p&gt;Every time I wanted to introduce a change to the code base, it felt like inserting a piece into a continent landmass-sized house of dominoes.&lt;/p&gt;

&lt;p&gt;You know, fixing a bug by creating 10 other bugs kind of situation.&lt;/p&gt;

&lt;p&gt;By the time I finished listening to my friend, I couldn’t hold it anymore.&lt;/p&gt;

&lt;p&gt;I stopped typing the code I was supposed to deliver tomorrow and typed “How to Test FrontEnd applications” into Google.&lt;/p&gt;

&lt;p&gt;What I discovered blew my mind.&lt;/p&gt;

&lt;p&gt;Anyway, fast-forward 4 years later, and I’ve developed my personal “do’s and dont’s” to testing frontend applications. It only took writing 2349+ tests of varying “usefulness” to realize that.&lt;/p&gt;

&lt;p&gt;So here they are. My top 5 mistakes you or your team should avoid when writing frontend tests.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. You Are Postponing Tests for “Later”, which never comes
&lt;/h2&gt;

&lt;p&gt;A lot of developers don’t like writing front end tests.&lt;br&gt;
Actually, some straight-up hate them and would rather become a Cobol developer than write a single HTML / Javascript test.&lt;/p&gt;

&lt;p&gt;Here are a few seemingly logical reasons:&lt;br&gt;
“Testing slows down the development!”&lt;br&gt;
“If my code is good, I don’t need to test it! Let the QA do the testing job”&lt;br&gt;
“I tried front end tests before, and they were absolutely useless!”&lt;/p&gt;

&lt;p&gt;Logical is a dangerous word. It is logical based on their previous experience with tests, and usually, that experience hasn’t been good.&lt;/p&gt;

&lt;p&gt;You see, automated tests have a lot of benefits :&lt;/p&gt;

&lt;h4&gt;
  
  
  Benefit #1: You are able to come back to your previous code after a few months and change it with minimal fear.
&lt;/h4&gt;

&lt;p&gt;New requirements came in? If you have good tests, you will have good confidence that the modified system works as intended.&lt;/p&gt;

&lt;p&gt;Suddenly realised that your architecture was subpar? It is very easy to change up your code without affecting previous end-user experience and functionality.&lt;/p&gt;

&lt;h4&gt;
  
  
  Benefit #2: New developers can more confidently change up code without breaking the system as a side-effect
&lt;/h4&gt;

&lt;p&gt;Let’s say you have a sign-up flow with a bunch of edge cases, and a new developer comes in.&lt;/p&gt;

&lt;p&gt;The best good case is he or she will ask the team a bunch of questions, then the code will go through the PR process, and the other developers will actually take the time to review and test the code to avoid bugs.&lt;/p&gt;

&lt;p&gt;But in reality, your team are people, and bugs will slip through despite the best PR review processes.&lt;/p&gt;

&lt;p&gt;Besides, there are enough teams where code reviews are not even that strict, and you can get a “LGTM!” If you ask nicely enough.&lt;/p&gt;

&lt;p&gt;This is why developers should put effort into writing good tests.The type of tests that will solidify the existing app behavior, validate a few edge cases, check some variations, and run predictably each time you or anyone else makes changes to the code base.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Stop Testing Your Implementation
&lt;/h2&gt;

&lt;p&gt;You might be asking…&lt;/p&gt;

&lt;p&gt;“Mike, what the hell do you mean when you say I should stop testing implementation?”&lt;/p&gt;

&lt;p&gt;Well, let’s say we have this component.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SomeComponent {
    @Output viewUpdated = new EventEmitter(); 

    clickButton = () =&amp;gt;  {
      fetch(‘…/../‘).then(r =&amp;gt; r.json()).then(() =&amp;gt; this.updateView());
    } 

    updateView = () =&amp;gt; {
      this.viewUpdated.emit(true);
    }
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Do you find this test useful?&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe(’SomeComponent’, () =&amp;gt; {
/** Setup code… */
    it(‘should react to clicking the button’, fakeAsync(() =&amp;gt; {
        const updateViewSpy = spyOn(component, ‘updateView’);
        component.clickButton();
        tick();
        expect(updateViewSpy).toHaveBeenCalled();
    }));
}); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Hm, well… do you?&lt;/p&gt;

&lt;p&gt;It is not useful, and let me tell you a few reasons why,:&lt;/p&gt;

&lt;p&gt;It tests that a button was clicked, but honestly, it doesn’t  test that “clickButton” was wired correctly to the HTML. It is too “shallow” and tied to the Typescript/Javascript implementation.&lt;/p&gt;

&lt;p&gt;It is testing that calling one component method evokes another “method”… That is too “tangled” to the Typescript/Javascript  implementation. You literally are testing that “the code I wrote is the way I wrote it”.&lt;/p&gt;

&lt;p&gt;That is not how the user uses your system. The user doesn’t “expect” that a component’s internal method was called when another method was called. (Of course, other developers can be your users, but they would probably care more that an event was emitted.&lt;/p&gt;

&lt;p&gt;I think this is the biggest problem with that test. The test is validating that:&lt;br&gt;
“The code I wrote the code is the way I wrote the code.”&lt;/p&gt;

&lt;p&gt;The problem with testing your “implementation” is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;These tests are way too brittle to any implementation changes or re-factoring&lt;/li&gt;
&lt;li&gt;These tests don’t even test that the system behaves as expected to the user.&lt;/li&gt;
&lt;li&gt;If someone tries to read your test to understand how the system behaves, he or she’ll probably waste a lot of time without learning much.
Also, what if that test above where one method calls another method is hiding a bug?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, the API URL could be incorrectly supplied, or the internal work of the methods was incorrectly updating the component’s state. Either of that happens, and you get a “false positive” – a test that is passing even though the system is buggy.&lt;/p&gt;

&lt;p&gt;Time and time again, I’ve found that whenever I wrote tests that tested how I wrote my classes or functions I wasn’t helping with identifying or preventing any bugs.&lt;/p&gt;

&lt;p&gt;I’ve learned that the mindset during testing should instead be:&lt;/p&gt;

&lt;p&gt;“Regardless of the way I wrote the code, the app satisfies the business requirements and behaves as expected to the user.”&lt;/p&gt;

&lt;p&gt;See how I added the phrase “Regardless of the way I wrote the code”?&lt;/p&gt;

&lt;p&gt;That’s the critical part of “resilient” tests.&lt;/p&gt;

&lt;p&gt;Full article and remaining points available at: &lt;a href="https://www.effortlesscoding.com/javascript-unit-testing-mistakes/"&gt;https://www.effortlesscoding.com/javascript-unit-testing-mistakes/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>angular</category>
      <category>javascript</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
