<?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>Forem: code plato</title>
    <description>The latest articles on Forem by code plato (@codeplato).</description>
    <link>https://forem.com/codeplato</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%2F477442%2F435281f3-b39d-4efc-8c3e-2fc4e4016730.png</url>
      <title>Forem: code plato</title>
      <link>https://forem.com/codeplato</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/codeplato"/>
    <language>en</language>
    <item>
      <title>[Enterprise Git Workflow Best Practice] Use a Sub-Project Branch When Developing Large Features</title>
      <dc:creator>code plato</dc:creator>
      <pubDate>Sat, 22 Mar 2025 20:15:47 +0000</pubDate>
      <link>https://forem.com/codeplato/enterprise-git-workflow-best-practice-use-a-sub-project-branch-when-developing-large-features-3a4a</link>
      <guid>https://forem.com/codeplato/enterprise-git-workflow-best-practice-use-a-sub-project-branch-when-developing-large-features-3a4a</guid>
      <description>&lt;p&gt;In a large project team, when developing major features, we may assign a few people to form a sub-project team. From the perspective of the overall project, this temporary group is working on a single, independent feature as a whole. However, within the temporary project, each member has their own sub-feature development tasks.  &lt;/p&gt;

&lt;p&gt;In this case, individual developers should not commit their code directly to the main development branch, such as &lt;code&gt;develop&lt;/code&gt; . Instead, a &lt;strong&gt;sub-project branch&lt;/strong&gt; should be created. Each team member commits their code to the &lt;strong&gt;sub-project branch&lt;/strong&gt; first, and at a designated point in time, the &lt;strong&gt;sub-project branch&lt;/strong&gt; is merged into the main development branch.&lt;/p&gt;

&lt;h1&gt;
  
  
  Terminology Definitions
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;feature branch:&lt;/strong&gt; A Git branch created when developing a new feature. Normally, only one person commits to this branch. You can also think of it as a personal branch. It is often named &lt;code&gt;feature/&amp;lt;feature-name&amp;gt;&lt;/code&gt; or &lt;code&gt;feature-&amp;lt;feature-name&amp;gt;&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0g4rk8bn0b17gf0yhuvn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0g4rk8bn0b17gf0yhuvn.png" alt="Image description" width="151" height="388"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;sub-project branch:&lt;/strong&gt; When developing a large feature, a sub-project team consisting of several developers is formed. In this case, a larger development branch can be created for this sub-project. This branch can span multiple sprints. The sub-project developers first commit their changes to the project branch, and at a certain point in time, the project branch is merged into the main branch. The project branch can be named &lt;code&gt;feature/&amp;lt;sub-project-name&amp;gt;-&amp;lt;creation-date&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnvi7pubojh8eq3227lo4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnvi7pubojh8eq3227lo4.png" alt="Image description" width="301" height="582"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Mistakes We Made with Sub-Project Branch
&lt;/h1&gt;

&lt;p&gt;Before writing on this topic, I assumed the use of sub-project branch was self-evident. However, after our team encountered several issues while using sub-project branches, we considered abandoning them. This led to even more problems, making me truly understand the importance of sub-project branch. As expected, the most profound realizations often come through mistakes.&lt;/p&gt;

&lt;p&gt;Below are the reasons we abandoned project branches. However, these reasons actually stemmed from incorrect usage rather than inherent flaws in project branches. Therefore, I will list the problems we encountered, along with the real causes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Project branches made the code messy:&lt;/strong&gt; We had different versions of components across different project branches.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Real Cause:&lt;/strong&gt; Excessive development branches led to code management chaos, especially when many branches remained unsynchronized for long periods, making the overall code structure complex.
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcucatt6voafurzmc1g5w.png" alt="Image description" width="214" height="400"&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Frequent conflicts during merges:&lt;/strong&gt; Every time we merged a project branch, we encountered numerous conflicts that were difficult to resolve. Sometimes, new bugs were introduced while resolving conflicts, such as accidentally deleting correct code.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Real Cause:&lt;/strong&gt; Long periods without synchronization caused excessive conflicts during merging, increasing the effort required to resolve them. The correct approach is to synchronize from the &lt;code&gt;develop&lt;/code&gt;  branch to the &lt;code&gt;sub-project branch&lt;/code&gt; regularly.
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faxj4382ylx9sjq5voykh.png" alt="Image description" width="350" height="350"&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;In reality, these issues were not intrinsic to project branches themselves.&lt;/p&gt;

&lt;h1&gt;
  
  
  Necessity of Sub-Project Branch
&lt;/h1&gt;

&lt;p&gt;Below are the key reasons for using project branches and best practices for managing them properly.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Check for conflicts before merging:&lt;/strong&gt; Fix the conflicts on the PR before merging.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhodyhbp6lkmt0yxylm5u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhodyhbp6lkmt0yxylm5u.png" alt="Image description" width="200" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Evaluate the testing process:&lt;/strong&gt; Notify the testing team before merging and ensure they have enough time to conduct tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftunor9zcrdf4avb6j6t1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftunor9zcrdf4avb6j6t1.png" alt="Image description" width="377" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Significantly reduce the difficulty of reverts:&lt;/strong&gt; If a branch contains only a single change, it minimizes coupling issues with other functionalities.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxijxsy0zeoolihiqvtvt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxijxsy0zeoolihiqvtvt.png" alt="Image description" width="408" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Run automated tests on the project branch:&lt;/strong&gt; Write sanity tests specifically for the branch to ensure feature reliability before merging.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4pfngbtq5bijket7o3o4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4pfngbtq5bijket7o3o4.png" alt="Image description" width="350" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The develop branch is used more widely than you think:&lt;/strong&gt; The &lt;code&gt;develop&lt;/code&gt;  branch is not just for some engineers—others, including different engineering teams, may also rely on it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk98mp1sds69vpjc5qfmw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk98mp1sds69vpjc5qfmw.png" alt="Image description" width="300" height="300"&gt;&lt;/a&gt;&lt;br&gt;
Even testers and product managers might use the &lt;code&gt;develop&lt;/code&gt;  branch.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa4r18vh7awa3v4d2xpfq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa4r18vh7awa3v4d2xpfq.png" alt="Image description" width="350" height="350"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Using project branches can maximize release stability, improve code traceability, enhance testing efficiency, and facilitate better team collaboration.&lt;/p&gt;

&lt;h1&gt;
  
  
  Best Practices for Using Sub-Project Branch
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Daily synchronization:&lt;/strong&gt; Keep merging the &lt;code&gt;develop&lt;/code&gt;  branch changes to Sub-project branch to avoid excessive conflicts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keep sub-project branches short-lived:&lt;/strong&gt; Sub-project branches should not exist for too long (&lt;strong&gt;no more than 3 sprints&lt;/strong&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Squash merges:&lt;/strong&gt; Use &lt;code&gt;squash &amp;amp; merge&lt;/code&gt; when merging code to make all commits in 1 commit. Cause from the perspective of the &lt;code&gt;develop&lt;/code&gt; branch, this feature should have only one or a few commits.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9nzy9addnwm8kk2z0jgl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9nzy9addnwm8kk2z0jgl.png" alt="Image description" width="800" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Keep it clean:&lt;/strong&gt; &lt;strong&gt;Delete the sub-project branch after merging&lt;/strong&gt; to prevent unnecessary future commits.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>The Bicycle-shed effect</title>
      <dc:creator>code plato</dc:creator>
      <pubDate>Wed, 05 Mar 2025 19:50:05 +0000</pubDate>
      <link>https://forem.com/codeplato/the-bicycle-shed-effect-1mln</link>
      <guid>https://forem.com/codeplato/the-bicycle-shed-effect-1mln</guid>
      <description>&lt;h1&gt;
  
  
  The Definition
&lt;/h1&gt;

&lt;p&gt;The Law of triviality as known as the bicycle-shed effect. Quote from  wiki page:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The law of triviality is C. Northcote Parkinson's 1957 argument that people within an organization commonly give disproportionate weight to trivial issues.[1] Parkinson provides the example of a fictional committee whose job was to approve the plans for a nuclear power plant spending the majority of its time on discussions about relatively minor but easy-to-grasp issues, such as what materials to use for the staff bicycle shed, while neglecting the proposed design of the plant itself, which is far more important and a far more difficult and complex task.&lt;/p&gt;

&lt;p&gt;The law has been applied to software development and other activities.[2] The terms bicycle-shed effect, bike-shed effect, and bike-shedding were coined based on Parkinson's example; it was popularized in the Berkeley Software Distribution community by the Danish software developer Poul-Henning Kamp in 1999[3] and, due to that, has since become popular within the field of software development generally. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcc4w0jku749lyclxc2ku.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcc4w0jku749lyclxc2ku.jpg" alt="Image description" width="600" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  What do we use it?
&lt;/h1&gt;

&lt;p&gt;Here are some examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;☎️ Don’t get too hung up on details during a meeting. For example, don’t argue over the details of the prompts that should be displayed for this feature. Don’t even waste time on technical details. Meeting time is for everyone, not just a few people.&lt;/li&gt;
&lt;li&gt;🖱️ When searching for information on the Internet, don't be distracted by interesting little bits of knowledge, or even Google them. Try to be patient with your curiosity.&lt;/li&gt;
&lt;li&gt;📦 When developing a feature or product, don’t waste too much time polishing it before the first release. Fine-tune it after the release.&lt;/li&gt;
&lt;li&gt;🗄️ When using the four-quadrant work method, try not to waste time on urgent but unimportant tasks. Once you have solved a problem, move on to the next one and do not extend the time for that problem.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>[Copilot Extreme Practice Journal] DAY 1: Using Copilot to Generate Unit Tests Without Writing a Single Line of Code</title>
      <dc:creator>code plato</dc:creator>
      <pubDate>Tue, 04 Mar 2025 05:13:52 +0000</pubDate>
      <link>https://forem.com/codeplato/copilot-extreme-practice-journal-day-1-using-copilot-to-generate-unit-tests-without-writing-a-c29</link>
      <guid>https://forem.com/codeplato/copilot-extreme-practice-journal-day-1-using-copilot-to-generate-unit-tests-without-writing-a-c29</guid>
      <description>&lt;p&gt;This series is called "Extreme Practice" because I don't know if this is the best practice. I am currently trying to push &lt;code&gt;ChatGPT&lt;/code&gt; to the extreme, meaning almost all actual coding and writing tasks are delegated to &lt;code&gt;ChatGPT&lt;/code&gt; to see how much efficiency can be improved—or decreased.&lt;/p&gt;

&lt;h1&gt;
  
  
  Terminology Introduction
&lt;/h1&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;Copilot&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Copilot&lt;/code&gt; is a chatbot developed by Microsoft that utilizes &lt;code&gt;ChatGPT&lt;/code&gt;. You can consider it a wrapper around &lt;code&gt;ChatGPT&lt;/code&gt;. It allows you to switch between different models, including Gemini, Claude, and &lt;code&gt;ChatGPT&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;GitHub Copilot&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;GitHub Copilot&lt;/code&gt; is an AI coding assistant that integrates &lt;code&gt;Copilot&lt;/code&gt; into &lt;code&gt;GitHub&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;GitHub Copilot VSCode Plugin&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;GitHub&lt;/code&gt; has developed a &lt;code&gt;Copilot&lt;/code&gt; plugin for &lt;code&gt;GitHub&lt;/code&gt;. This plugin enables programmers to use &lt;code&gt;Copilot&lt;/code&gt; within the IDE by connecting it to &lt;code&gt;GitHub Copilot&lt;/code&gt;. It currently supports &lt;code&gt;VSCode&lt;/code&gt; and IntelliJ.&lt;/p&gt;

&lt;h1&gt;
  
  
  Task Requirements
&lt;/h1&gt;

&lt;p&gt;Generating unit tests for UI components is always a painful task. Testing pure logic functions is straightforward—you just pass in parameters and verify the output. However, testing components requires setting up many variables needed for component loading and passing them to the component in different ways.&lt;/p&gt;

&lt;p&gt;Today is the first day of practice. I attempted to use &lt;code&gt;Copilot&lt;/code&gt; to generate a unit test for the Vue component &lt;code&gt;BenefitsAccordion&lt;/code&gt;. The requirements are as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⏳ Must be completed within 30 minutes.&lt;/li&gt;
&lt;li&gt;✅ The generated unit test must pass.&lt;/li&gt;
&lt;li&gt;🏆 The unit test must follow good coding practices.&lt;/li&gt;
&lt;li&gt;🙅‍♂️ I should not write any code; the task should be completed through conversation only.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Execution Process
&lt;/h1&gt;

&lt;p&gt;I will use ✅ and ❌ and 🤔 to show how it is doing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If it work as my expect. I will give it a ✅&lt;/li&gt;
&lt;li&gt;If it doesn't work as expect. I will give it a ❌&lt;/li&gt;
&lt;li&gt;If I don't know whether it works well or not, I will give it a 🤔&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Initial Generation of Test Cases ✅
&lt;/h2&gt;

&lt;p&gt;I opened the &lt;code&gt;Copilot Edits&lt;/code&gt; panel:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftoejt7vepuctv3d6yntj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftoejt7vepuctv3d6yntj.png" alt="Image description" width="438" height="946"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I selected the file and told it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;/tests generate unit test for this file&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frcbt45t5usxt1ree2xmq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frcbt45t5usxt1ree2xmq.png" alt="Image description" width="800" height="357"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It generated a test file, but it did not recognize that my codebase is a monorepo (&lt;a href="https://medium.com/@magenta2127/monorepo-vs-multi-repo-vs-monolith-7c4a5f476009" rel="noopener noreferrer"&gt;Monorepo vs Multi-repo vs Monolith&lt;/a&gt;). The generated test file should be placed in the corresponding sub-repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Moving the Test Case ❌
&lt;/h2&gt;

&lt;p&gt;I instructed it to move the unit test to &lt;code&gt;apps/&amp;lt;sub-repo&amp;gt;/tests/unit&lt;/code&gt;. So I said:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;@workspace app/xxx has its own tests folder. Move the unit test you just generated to that folder instead of tests/unit.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It agreed and moved the file, but placed it in the root directory &lt;code&gt;/tests/unit&lt;/code&gt;, which is incorrect.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Moving the Test Case Again ✅
&lt;/h2&gt;

&lt;p&gt;Since test cases should be placed according to the structure of the System Under Test (SUT), I said:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't put it in the root of apps/ms/tests/unit. Place it according to the source code file structure.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This time, it got it right:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Move the unit test file for the BenefitsAccordion.vue component to the apps/ms/tests/unit/components/xxx/xxx folder to match the source code file structure. &lt;br&gt;
But there were many errors&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6hk0lb3sdv74llpoov70.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6hk0lb3sdv74llpoov70.png" alt="Image description" width="682" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Attempting to Fix the Test Case ❌
&lt;/h2&gt;

&lt;p&gt;I asked it to fix the errors in the unit test file:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fix the errors in the unit test file BenefitsAccordion.spec.ts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It responded:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fix the errors in the unit test file for the BenefitsAccordion.vue component.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, the errors remained unchanged.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Directly Telling It How to Fix the Errors  ✅
&lt;/h2&gt;

&lt;p&gt;The errors were basic—missing imports for Vitest functions like &lt;code&gt;describe&lt;/code&gt; and &lt;code&gt;beforeEach&lt;/code&gt;. I explicitly pointed this out:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The errors in BenefitsAccordion are because you didn't import describe, beforeEach, etc., from Vitest.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This time, it understood:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fix the errors by importing describe, beforeEach, it, and expect from Vitest.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 6: Specifying Which Methods to Test ✅
&lt;/h2&gt;

&lt;p&gt;It generated some unhelpful test cases, such as &lt;code&gt;it should render ...&lt;/code&gt;, which I consider meaningless. So, I told it explicitly:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;/tests in BenefitsAccordion.spec.ts, only generate unit tests for methods xxx, xxx, xxx, xxx, xxx, xxx. Remove other tests.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It responded correctly:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Generate unit tests only for the methods xxx, xxx, xxx, xxx, xxx, and xxx. Remove other unit tests.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 7: Categorizing Test Methods ✅
&lt;/h2&gt;

&lt;p&gt;A good practice is to group test cases using &lt;code&gt;describe()&lt;/code&gt;. I instructed:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use describe to categorize unit tests by the methods being tested.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It got me&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Organize the unit tests using describe blocks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 8: Testing More Logical Paths ✅
&lt;/h2&gt;

&lt;p&gt;It only tested one path per method, but each method had at least two logical paths. I told it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You only test one path. Test more paths. For example, for xxx, test when xxx is true.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It got me&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Add more test cases to cover different paths of the methods being tested.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 9: Manually Specifying a Missing Path 🤔
&lt;/h2&gt;

&lt;p&gt;One test path was still missing, so I explicitly pointed it out:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For planId, you didn’t test when planId is null.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It responded:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Add a test case for planId when planId is null.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 10: Running the Test Cases ❌
&lt;/h2&gt;

&lt;p&gt;I asked it to run the tests, but it said:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;❌ Sorry, I can't assist with that.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I ran them manually and found one failed test:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4w5fze3z2tz21w9bcxl1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4w5fze3z2tz21w9bcxl1.png" alt="Image description" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 11: Fixing the Test Case ❌
&lt;/h2&gt;

&lt;p&gt;I asked it to fix the failing test:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The unit test "returns empty string when planId is null" failed, correct it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It responded:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;✅ Correct the unit test "returns empty string when planId is null".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, the error persisted.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 12: Attempting to Fix the Test Again ❌
&lt;/h2&gt;

&lt;p&gt;After multiple attempts, it still couldn't fix the test. I had to fix it manually, which violated my goal of not touching the code. Additionally, fixing one test caused five others to fail.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8gexxb0mbq012bizd38l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8gexxb0mbq012bizd38l.png" alt="Image description" width="800" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 13: Ensuring Test Isolation ❌
&lt;/h2&gt;

&lt;p&gt;To prevent tests from affecting each other, I instructed:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Ensure each test runs with a clean component instance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It responded:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Ensure each test runs with a new component instance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, it did not work, and the errors remained.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;The experiment ultimately failed. The tests did not pass, and the situation worsened. I even had to intervene manually. In the end, I deleted the test file. Tomorrow, I will attempt a different approach.&lt;/p&gt;

</description>
      <category>githubcopilot</category>
      <category>chatgpt</category>
      <category>node</category>
      <category>programming</category>
    </item>
    <item>
      <title>Use watch as the last resort</title>
      <dc:creator>code plato</dc:creator>
      <pubDate>Sat, 01 Mar 2025 04:28:13 +0000</pubDate>
      <link>https://forem.com/codeplato/use-watch-as-the-last-resort-5d5d</link>
      <guid>https://forem.com/codeplato/use-watch-as-the-last-resort-5d5d</guid>
      <description>&lt;p&gt;We should avoid using &lt;code&gt;watch&lt;/code&gt; in Vue. I'm not saying it's bad; we should just treat &lt;code&gt;watch&lt;/code&gt; (and &lt;code&gt;watchEffect&lt;/code&gt;) as a last resort. &lt;code&gt;watch&lt;/code&gt; is a very reliable tool. It is the simplest and laziest way to achieve a function. It can almost guarantee 100% triggering, thus covering your needs. But the problem is that sometimes &lt;strong&gt;it covers too much&lt;/strong&gt;. &lt;code&gt;watch&lt;/code&gt; has the following disadvantages:&lt;/p&gt;

&lt;h2&gt;
  
  
  Disadvantages
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Implicit Dependency
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;watch&lt;/code&gt; creates an implicit dependency.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An implicit dependency is a hidden relationship between components in a system where one component relies on another without being explicitly stated.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An explicit dependency occurs when component A directly calls component D, making the relationship clear and easy to trace.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6hm56xh6s18jt6ekjff2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6hm56xh6s18jt6ekjff2.png" alt="Explicit Dependency" width="431" height="271"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Implicit dependency means that component A eventually calls component D in an unobvious way, creating a connection between them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyuzo6t0lnc6gnbjb0qt7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyuzo6t0lnc6gnbjb0qt7.png" alt="Implicit dependency" width="478" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although explicit dependencies seem easier to maintain, a system full of them would lack flexibility. For example, &lt;code&gt;vue-router&lt;/code&gt; is an implicit dependency, and implicit dependencies are essential for building frameworks. However, using them in business code increases maintenance difficulty.&lt;/p&gt;

&lt;p&gt;Here is a great article that explains the disadvantages of implicit dependencies:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.marktinderholt.com/software%20development/2024/12/10/avoid-side-effects.html" rel="noopener noreferrer"&gt;Side Effects in Code: Why They Matter and How to Avoid Them&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although this article does not explicitly mention &lt;code&gt;implicit dependencies&lt;/code&gt;, side effects are essentially how &lt;code&gt;watch&lt;/code&gt; operates in Vue.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Side effects create implicit dependencies between components, increasing the chance of bugs. For instance, in our discussion, I noted: "If a method assumes that a specific member variable will be updated beforehand, you’ve built a trap for the next developer — or even for yourself six months later."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  2. Difficult to Trace the Source of Changes
&lt;/h3&gt;

&lt;p&gt;When an error occurs, it is difficult to determine which piece of code initially triggered the change. If you are debugging in a local environment, it's easy to check the stack trace. But if you are troubleshooting on QA, CM, or PROD and are unaware of a &lt;code&gt;watch&lt;/code&gt;, debugging can become a nightmare.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpbw93zjmxk6xg8zvoq44.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpbw93zjmxk6xg8zvoq44.png" alt="Image description" width="638" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A real-world example: while working on one of the page of our project, we encountered a defect that seemed to originate from the data initialization. However, we didn't realize that the data were initialized twice—once via a &lt;code&gt;watch&lt;/code&gt;. If both initializations worked correctly, the duplicate execution would go unnoticed. But when bugs inevitably appear, identifying the source of the error becomes critical in measuring code quality.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Creates Tight Coupling, Making Refactoring Difficult
&lt;/h3&gt;

&lt;p&gt;Using &lt;code&gt;watch&lt;/code&gt; can tightly couple different parts of the code. Initially, we knew that code A triggered code B through &lt;code&gt;watch&lt;/code&gt;. Over time, additional code was introduced—code C also triggered code B through &lt;code&gt;watch&lt;/code&gt;. Then, a third engineer, seeing the existing pattern, added code D, further expanding the web of dependencies. This results in increased coupling, making the code harder to maintain.&lt;/p&gt;

&lt;p&gt;This article discusses the dangers of tight coupling:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/pragmatic-programmers/code-coupling-reducing-dependency-in-your-code-ae21c4534295" rel="noopener noreferrer"&gt;Code Coupling: Reducing Dependency in Your Code&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You’ve no doubt encountered this effect: you make a change in one corner of the system and it breaks code in a far, far distant corner. You might not even spot such a defect, particularly if you don’t do extensive regression testing with each release. That, of course, means your unfortunate customer will likely be the one who spots the defect.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A &lt;code&gt;watch&lt;/code&gt; is like a spider web—it captures any logic that falls into its scope. If you &lt;code&gt;watch&lt;/code&gt; a global variable, such as &lt;code&gt;savedSession&lt;/code&gt;, its impact could extend across the entire project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk927lzvlacdcaikvg2l1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk927lzvlacdcaikvg2l1.png" alt="Image description" width="516" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A project with excessive &lt;code&gt;watch&lt;/code&gt; usage resembles the image above.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. May Cause Infinite Loops
&lt;/h3&gt;

&lt;p&gt;If &lt;code&gt;watch&lt;/code&gt; A triggers &lt;code&gt;watch&lt;/code&gt; B, and &lt;code&gt;watch&lt;/code&gt; B triggers &lt;code&gt;watch&lt;/code&gt; A, you create an infinite loop.&lt;/p&gt;

&lt;p&gt;A real example: while developing &lt;code&gt;QuotesPage.vue&lt;/code&gt;, I was a big fan of &lt;code&gt;watch&lt;/code&gt; and added five or six of them on a single page. This led to an infinite loop when the page attempted a "relaxation" operation. At the time, there was no good way to suspend a watcher. See this answer from Evan You, the creator of Vue.js:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F77gvs3d9nlwwjpv68c2g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F77gvs3d9nlwwjpv68c2g.png" alt="Image description" width="800" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The only way to suspend a &lt;code&gt;watch&lt;/code&gt; is to include a conditional check. I had to add some variables like &lt;code&gt;watcherEnabled&lt;/code&gt; disable the &lt;code&gt;watch&lt;/code&gt; when necessary. After I stopped using &lt;code&gt;watch&lt;/code&gt;, I was able to say goodbye to this ugly workaround.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Hard to Read and Understand
&lt;/h3&gt;

&lt;p&gt;When reviewing the logic behind a &lt;code&gt;watch&lt;/code&gt;, you often need to navigate between multiple files or distant parts of the same file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgdic6ilxem6gcbdc7m7j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgdic6ilxem6gcbdc7m7j.png" alt="Image description" width="248" height="1114"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You might need to jump across different areas of the project to understand the full logic flow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Better Solutions
&lt;/h2&gt;

&lt;p&gt;There are better alternatives to &lt;code&gt;watch&lt;/code&gt;. Consider the following before using a watcher:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Directly call the method&lt;/strong&gt; if they are in the same component.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;&lt;code&gt;computed&lt;/code&gt;&lt;/strong&gt; properties if you only need to display something.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;custom events&lt;/strong&gt; to trigger methods in a parent component.&lt;/li&gt;
&lt;li&gt;Use lifecycle hooks like &lt;strong&gt;&lt;code&gt;app:rendered&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;page:start&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;definePageMeta&lt;/code&gt;&lt;/strong&gt; in Nuxt, or &lt;strong&gt;&lt;code&gt;onMounted&lt;/code&gt;&lt;/strong&gt; in Vue.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When to Use &lt;code&gt;watch&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Despite its drawbacks, &lt;code&gt;watch&lt;/code&gt; is still useful in some cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When you need to update something, but &lt;strong&gt;custom events&lt;/strong&gt; cannot achieve the desired behavior.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By using &lt;code&gt;watch&lt;/code&gt; cautiously and considering alternatives first, you can write more maintainable and predictable Vue applications.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>typescript</category>
      <category>frontend</category>
      <category>cleancoding</category>
    </item>
    <item>
      <title>How to write elegant code 1: Keep the chain of event calls short</title>
      <dc:creator>code plato</dc:creator>
      <pubDate>Sat, 21 Dec 2024 18:21:12 +0000</pubDate>
      <link>https://forem.com/codeplato/how-to-write-elegant-code-1-keep-the-chain-of-event-calls-short-n8e</link>
      <guid>https://forem.com/codeplato/how-to-write-elegant-code-1-keep-the-chain-of-event-calls-short-n8e</guid>
      <description>&lt;h1&gt;
  
  
  The chain of event calls
&lt;/h1&gt;

&lt;p&gt;There are some scenarios we keep throw up the event to the parent component. Then it handle the event in the component far way from the component which raise the event&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6cadn7wzd19zuywwx1qe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6cadn7wzd19zuywwx1qe.png" alt="Image description" width="211" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This has the following disadvantages&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Switching between different component source code increase the difficulty of developing&lt;/li&gt;
&lt;li&gt;We coupling deeply these components. The architecture is fragile. If there is a component updated might break the event chain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we calculate the distance of event calls chain in this way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;when the event is handled in the same component which fire it, the distance is 0&lt;/li&gt;
&lt;li&gt;if the event if handled in the parent component we plus 1 to the distance. For example, the event is handled by the parent component, the distance is 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the example above, the distance is 3&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffksgn9aeb2uexoz36mdj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffksgn9aeb2uexoz36mdj.png" alt="Image description" width="211" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code is hard to read if the distance is larger than 1. If it’s 3, you will need to switch 4 files to read the event call chain completely.&lt;/p&gt;

&lt;h1&gt;
  
  
  How to fix it
&lt;/h1&gt;

&lt;p&gt;Put the even handler beside the event listener&lt;/p&gt;

&lt;h1&gt;
  
  
  Questions
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Q&lt;/strong&gt;: Will this cause a lot of duplicate code? &lt;strong&gt;A&lt;/strong&gt;: No. we can extract common code to composable or component to reuse the same code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Q&lt;/strong&gt;: Will this create a lot of duplicate components like modals? &lt;strong&gt;A&lt;/strong&gt;: No, make sure the component only be created when it's needed. For example if you are using &lt;code&gt;Vue.js&lt;/code&gt; use &lt;code&gt;v-if&lt;/code&gt; instead of &lt;code&gt;v-show&lt;/code&gt; for modal component to make sure it won’t be rendered when no needed. Also, use private variable instead of global variable to control if the modal is shown to make sure the toggle method will only trigger 1 modal to be open or close.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>3 Things You Should Try to Avoid in Vue.js</title>
      <dc:creator>code plato</dc:creator>
      <pubDate>Sun, 03 Apr 2022 18:07:44 +0000</pubDate>
      <link>https://forem.com/codeplato/3-things-you-should-try-to-avoid-in-vuejs-27b6</link>
      <guid>https://forem.com/codeplato/3-things-you-should-try-to-avoid-in-vuejs-27b6</guid>
      <description>&lt;h1&gt;
  
  
  1. Try not to write the inline script in the template if there are more than 2 lines of code
&lt;/h1&gt;

&lt;p&gt;VSCode &lt;strong&gt;cannot&lt;/strong&gt; check any syntax error of the inline script in the template. So try not to write the inline script in the template. If you write the inline script in the template, it's no different than writing code in Notepad. There are 2 drawbacks:&lt;/p&gt;

&lt;h2&gt;
  
  
  VS Code cannot check the syntax error
&lt;/h2&gt;

&lt;p&gt;If you are using &lt;code&gt;Typescript&lt;/code&gt;, &lt;code&gt;VS Code&lt;/code&gt; can identify the syntax error for you. You might think I'd be able to spot those syntax errors, but actually, we don't. For example, in this code, the inline script only has 2 lines of code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div @click="alert('hello world'); doSomething();" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The even handler is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;doSomething(event: Event) {
   // do something ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is no errors in &lt;code&gt;VS Code&lt;/code&gt;. But when you run this code, you will find that the &lt;code&gt;event&lt;/code&gt; parameter is &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you extract the line move &lt;code&gt;console.log('do something'); doSomething();&lt;/code&gt; into a method &lt;code&gt;sayHelloAndDoSomething()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sayHelloAndDoSomething() {
    console.log('do something');
    this.doSomething();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you will see there is an error&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2g2szo2tvqjnq4vw3vxk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2g2szo2tvqjnq4vw3vxk.png" alt="Image description" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It turns out that &lt;code&gt;doSomething&lt;/code&gt; has a required parameter &lt;code&gt;event&lt;/code&gt;. The worse thing is this inline script seems to be working fine. But the parameter &lt;code&gt;event&lt;/code&gt; of &lt;code&gt;doSomething&lt;/code&gt; arguments is &lt;code&gt;undefined&lt;/code&gt;. We create a defect in this way. This example looks dumb. But there similar cases I've met at work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hard to debug the code in production
&lt;/h2&gt;

&lt;p&gt;The inline script will be compiled in 1 line js code in production. It's not able to debug this code. If you put the code in a method/computed of the component. You can still be able to debug it even it's already minified. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54wkst1z7w663ou4gr0n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54wkst1z7w663ou4gr0n.png" alt="Image description" width="800" height="606"&gt;&lt;/a&gt;&lt;br&gt;
If there is an error, we can still debug this code. It's not difficult to know what is the &lt;code&gt;e&lt;/code&gt; or &lt;code&gt;n&lt;/code&gt; variable based on the source code.&lt;/p&gt;

&lt;p&gt;Imagine how convenient it will be if you can debug the code in production. That really help me for several times. So try to extract inline script code into component method/computed, it will help you eventually.&lt;/p&gt;
&lt;h1&gt;
  
  
  2. Try not to use watcher
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://v2.vuejs.org/v2/guide/computed.html?redirect=true#Computed-vs-Watched-Property" rel="noopener noreferrer"&gt;Vue.js documentation&lt;/a&gt; already suggest us not to use watcher.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx3wvzjwo3stavfyea1gv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx3wvzjwo3stavfyea1gv.png" alt="Image description" width="800" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why? Because watcher is an implicit dependency. Implicit dependency brings the following problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Logic becomes more scattered, and there is no explicit correlation between them. It makes it difficult to understand the code. This is especially difficult when troubleshooting.&lt;/li&gt;
&lt;li&gt;When you update the feature, you won't be able to notice that somewhere a logic has been broken. Just because this logic is triggered by a watcher.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In addition to the problems brought by implicit dependencies, &lt;code&gt;watcher&lt;/code&gt; has other problems of his own:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The watcher won't be triggered when Vue.js rendering the component as &lt;code&gt;watch&lt;/code&gt; is lazy by default unless you use &lt;code&gt;immediate: true&lt;/code&gt;. So if you expect some logic in a watcher will be called when &lt;code&gt;v-if&lt;/code&gt; turn to &lt;code&gt;true&lt;/code&gt; , you may be disappointed. We did meet a defect caused by this.&lt;/li&gt;
&lt;li&gt;The watcher cannot be paused. Sometimes we don't want to disable the watcher, we just want to pause it. Unfortunately, watcher doesn't provide the pause feature. &lt;a href="https://github.com/vuejs/vue/issues/1829#issuecomment-157944069" rel="noopener noreferrer"&gt;this comment&lt;/a&gt; mentioned that the only way to pause the watcher is doing a conditional check. When I have a similar problem at work, I usually add a flag like &lt;code&gt;watcherEnabled&lt;/code&gt; to pause the 
watchers in some scenarios. But this flag also brought me some defects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are 5 better approaches than watcher&lt;/p&gt;
&lt;h2&gt;
  
  
  From child to parent
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Use custom events
&lt;/h3&gt;

&lt;p&gt;If you need to trigger the method of the parent component. You can use &lt;a href="https://v2.vuejs.org/v2/guide/components-custom-events.html?redirect=true" rel="noopener noreferrer"&gt;custom event&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fazragve5odwmm6lh3az8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fazragve5odwmm6lh3az8.png" alt="Image description" width="351" height="231"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  From parent to child
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Move method from child to parent
&lt;/h3&gt;

&lt;p&gt;If the metod you want to trigger is in child method. Instead of using &lt;code&gt;$refs&lt;/code&gt;, it will be better to move the method to the parent component. Then you can directly call it or use custom event&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqy542e10v59wf4dmv9ns.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqy542e10v59wf4dmv9ns.png" alt="Image description" width="351" height="231"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Use ref attribute
&lt;/h3&gt;

&lt;p&gt;If the method you want to trigger cannot be moved to the parent. Or you don't want to take the risk to refactor the task. You can use &lt;a href="https://v2.vuejs.org/v2/guide/components-edge-cases.html#Accessing-Child-Component-Instances-amp-Child-Elements" rel="noopener noreferrer"&gt;ref attribute&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc4kmj9o99pa1a34gjh2h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc4kmj9o99pa1a34gjh2h.png" alt="Image description" width="351" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But remember that using &lt;code&gt;$refs&lt;/code&gt; is not a good practice. $refs has its own problem as well. The component  &lt;code&gt;$refs&lt;/code&gt; is &lt;code&gt;undefined&lt;/code&gt; when the component is rendering. So if you use &lt;code&gt;$refs&lt;/code&gt; it in the template, it might be undefined. If you are using &lt;code&gt;$refs&lt;/code&gt; in the &lt;code&gt;computed&lt;/code&gt;. The &lt;code&gt;computed&lt;/code&gt; might be broken. For example, there is a &lt;code&gt;computed&lt;/code&gt; to get the child component scrollLeft attribute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;get childScollLeft() {
    return this.$refs.child.scrollLeft;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;computed&lt;/code&gt; won't work as you expect. Because when the variable you want to watch in the computed is undefined at the beginning, then this computed will not be reactive. Not even after the &lt;code&gt;$refs&lt;/code&gt; loaded.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use props
&lt;/h3&gt;

&lt;p&gt;If you want to update the child component UI. You can use &lt;a href="https://v2.vuejs.org/v2/guide/components-props.html?redirect=true" rel="noopener noreferrer"&gt;props&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr122jroa2mb6lvq1sb66.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr122jroa2mb6lvq1sb66.png" alt="Image description" width="351" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  From anywhere
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use Vuex
&lt;/h3&gt;

&lt;p&gt;If you want to update the parent/child/sibling component UI. You can use Vuex. The only downside is that this may increase code complexity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0e63hk4fnrlgf1mm5x9b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0e63hk4fnrlgf1mm5x9b.png" alt="Image description" width="526" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Try not to pass functions as props
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://medium.com/js-dojo/passing-functions-as-props-an-anti-pattern-in-vue-js-b542fc0cf5d" rel="noopener noreferrer"&gt;Passing functions as props an anti-pattern in Vue.js&lt;/a&gt;. There is a sentence in the article that is really good.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As the application grows larger in size and other developers come on board, they will look at the child component code and have to figure out which callback function prop it is and where it is from.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's the issue that I meet in my project. Sometimes I need to figure out where are those methods come from.&lt;/p&gt;

&lt;p&gt;I agree with it. But this is not the main reason I don't recommend passing functions as props. The main reason is when I read the code it takes some effort to get familiar with the context of this component. If there is a function was passed as a prop, then I need to be familiar with the context of another component. If that were all it was, it wouldn't be so bad. The worse is I need to switch between different components contexts when reading the method.&lt;/p&gt;

&lt;p&gt;I recommend using &lt;a href="https://v2.vuejs.org/v2/guide/components-custom-events.html?redirect=true" rel="noopener noreferrer"&gt;custom event&lt;/a&gt; instead of passing functions as props.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Jest Best Practice 1: Use eslint-plugin-jest</title>
      <dc:creator>code plato</dc:creator>
      <pubDate>Wed, 28 Apr 2021 03:22:47 +0000</pubDate>
      <link>https://forem.com/codeplato/jest-best-practice-1-use-eslint-plugin-jest-o7e</link>
      <guid>https://forem.com/codeplato/jest-best-practice-1-use-eslint-plugin-jest-o7e</guid>
      <description>&lt;h1&gt;
  
  
  Wasteland in the project
&lt;/h1&gt;

&lt;p&gt;We used to focus on the quality of business code only and ignore the unit test code quality. That makes the unit tests code becomes the wild west of our project. So I gonna share some practice I've used in my project.&lt;/p&gt;

&lt;h1&gt;
  
  
  Start with the code style
&lt;/h1&gt;

&lt;p&gt;First, let's begin with the style of your unit test code. We used to use eslint on our business code. But have you try to use eslint on the jest code? Try &lt;code&gt;eslint-plugin-jest&lt;/code&gt;. Here is the introduction of this package: &lt;a href="https://www.npmjs.com/package/eslint-plugin-jest" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/eslint-plugin-jest&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are the rule set I've used in my project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'jest/expect-expect': 'error',
'jest/lowercase-name': [
    'error',
    {
        ignore: ['describe'],
    },
],
'jest/no-disabled-tests': 'error'
'jest/no-done-callback': 'error',
'jest/no-duplicate-hooks': 'error',
'jest/no-conditional-expect': 'error',
'jest/no-export': 'error',
'jest/no-focused-tests': 'error',
'jest/no-identical-title': 'error',
'jest/no-interpolation-in-snapshots': 'error',
'jest/no-jasmine-globals': 'error',
'jest/no-jest-import': 'error',
'jest/no-large-snapshots': 'error',
'jest/no-mocks-import': 'error',
'jest/no-standalone-expect': 'error',
'jest/no-test-prefixes': 'error',
'jest/valid-describe': 'error',
'jest/valid-expect-in-promise': 'error',
'jest/prefer-to-have-length': 'warn',
'jest/valid-expect': 'error',
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most of them are easy to understand. But I want to introduce some of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  jest/no-done-callback
&lt;/h2&gt;

&lt;p&gt;We might need to change our habits. Don't use &lt;code&gt;done&lt;/code&gt; anymore. Because if the code is not able to reach &lt;code&gt;done&lt;/code&gt; it will easily get an error. Also using callback can be very prone,  as it requires careful understanding of how assertions work in tests, or otherwise tests won't behave as expected.&lt;/p&gt;

&lt;p&gt;There are 2 scenarios we need to change our way to write the test&lt;/p&gt;

&lt;h3&gt;
  
  
  for async operation
&lt;/h3&gt;

&lt;p&gt;For async operation. Turn to use &lt;code&gt;async...await&lt;/code&gt; instead of done. Or you can return the Promise like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return doSomething().then(data =&amp;gt; {...})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  for setTimeout
&lt;/h3&gt;

&lt;p&gt;For &lt;code&gt;setTimeout&lt;/code&gt;. Use &lt;code&gt;jest.useFakeTimers()&lt;/code&gt; at the beginning of the test file. Then use &lt;code&gt;jest.runAllTimers()&lt;/code&gt; to fast-forward until all timers have been executed&lt;/p&gt;

&lt;p&gt;For more information about timer mocker, please refer to &lt;a href="https://jestjs.io/docs/timer-mocks" rel="noopener noreferrer"&gt;https://jestjs.io/docs/timer-mocks&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  jest/no-conditional-expect
&lt;/h2&gt;

&lt;p&gt;Use the &lt;code&gt;expect&lt;/code&gt; in conditional call could lead to the &lt;code&gt;expect&lt;/code&gt; silently being skipped. Put the &lt;code&gt;expect&lt;/code&gt; in &lt;code&gt;catch&lt;/code&gt; is also easy to be skipped.&lt;/p&gt;

&lt;p&gt;The following patterns are warnings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it ('foo', () =&amp;gt; {
    const result = doSomething();
    if (result === 1) {
        expect(1).toBe(1)
    }
})

it ('bar', () =&amp;gt; {
    try {
        await foo();
    } catch (err) {
        expect(err).toMatchObject({ code: 'MODULE_NOT_FOUND' });
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will be better to write these tests in this way&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it ('foo', () =&amp;gt; {
    const result = doSomething();
    expect(result).toBe(1);
    expect(1).toBe(1);
})

it ('throws an error', () =&amp;gt; {
    await expect(foo).rejects.toThrow(Error);
})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  jest/no-identical-title
&lt;/h2&gt;

&lt;p&gt;There is an important rule &lt;code&gt;no-identical-title&lt;/code&gt;. It's for preventing us to name 2 test cases with the same name.&lt;/p&gt;

&lt;p&gt;The following patterns are considered warnings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it('should do bar', () =&amp;gt; {});
it('should do bar', () =&amp;gt; {}); // Has the same title as the previous test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's simple but very useful. I have the experience that I tried to fix the failed unit test. But it was still failed after 30mins troubleshooting. Then I found that I'm not fixing the one that failed. It's especially tricky when there are 2 failed unit tests with the same name.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>jest</category>
      <category>eslint</category>
    </item>
  </channel>
</rss>
