<?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: Lucas Azevedo</title>
    <description>The latest articles on DEV Community by Lucas Azevedo (@_oazevedolucas).</description>
    <link>https://dev.to/_oazevedolucas</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4032241%2Fa88943c0-07f5-4c13-b1df-6950808bfb01.png</url>
      <title>DEV Community: Lucas Azevedo</title>
      <link>https://dev.to/_oazevedolucas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/_oazevedolucas"/>
    <language>en</language>
    <item>
      <title>Why generating random CPFs in your tests is wrong (and how I fixed it with a zero-dependency lib)</title>
      <dc:creator>Lucas Azevedo</dc:creator>
      <pubDate>Thu, 16 Jul 2026 13:26:54 +0000</pubDate>
      <link>https://dev.to/_oazevedolucas/why-generating-random-cpfs-in-your-tests-is-wrong-and-how-i-fixed-it-with-a-zero-dependency-lib-36cp</link>
      <guid>https://dev.to/_oazevedolucas/why-generating-random-cpfs-in-your-tests-is-wrong-and-how-i-fixed-it-with-a-zero-dependency-lib-36cp</guid>
      <description>&lt;p&gt;Every time I needed a CPF to seed a test database or fill out a form in a dev environment, I ran into the same problem: generic generators gave me a number that &lt;strong&gt;looked&lt;/strong&gt; like a CPF 11 digits, nicely formatted  that &lt;strong&gt;broke&lt;/strong&gt; the moment it hit any real validation.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For non-Brazilian readers: the &lt;strong&gt;CPF&lt;/strong&gt; is Brazil's individual taxpayer ID, like a national ID number. The &lt;strong&gt;CNPJ&lt;/strong&gt; is its business equivalent. Both are used everywhere in Brazilian software.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a silent problem. Your test passes with data that production would reject. And you find out about the bug far too late.&lt;/p&gt;

&lt;p&gt;I got tired of it and decided to build something better: &lt;strong&gt;&lt;a href="https://www.npmjs.com/package/massa-br" rel="noopener noreferrer"&gt;massa-br&lt;/a&gt;&lt;/strong&gt;  a deterministic generator of &lt;strong&gt;valid&lt;/strong&gt; Brazilian data, with zero dependencies.&lt;/p&gt;

&lt;p&gt;In this article I want to focus less on "install and use" and more on what I learned building it: why "valid by construction" changes everything, and how the CPF check digit actually works.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem: "looks like a CPF" ≠ "is a CPF"
&lt;/h2&gt;

&lt;p&gt;A CPF isn't just 11 random digits. The &lt;strong&gt;last two digits are calculated&lt;/strong&gt; from the first nine. They exist precisely to catch typos.&lt;/p&gt;

&lt;p&gt;Which means: if you roll 11 random digits, the odds of the last two matching the correct calculation are roughly 1 in 100. In practice, &lt;strong&gt;almost every "random" CPF is invalid.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's why a generic generator betrays you: it produces something that passes a &lt;code&gt;\d{11}&lt;/code&gt; regex, but fails a check-digit validation which is exactly what any serious system does.&lt;/p&gt;

&lt;h2&gt;
  
  
  The shift: generate valid by construction
&lt;/h2&gt;

&lt;p&gt;The core idea of massa-br is simple: instead of rolling all 11 digits and hoping, you roll only the &lt;strong&gt;9 base digits&lt;/strong&gt; and &lt;strong&gt;calculate&lt;/strong&gt; the 2 check digits. That way the result is valid always, by construction.&lt;/p&gt;

&lt;p&gt;To make this work, I had to actually understand how the check digit is computed. It's worth sharing, because almost nobody explains it clearly.&lt;/p&gt;

&lt;h3&gt;
  
  
  How the CPF check digit works
&lt;/h3&gt;

&lt;p&gt;The first check digit comes out like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Take the 9 base digits.&lt;/li&gt;
&lt;li&gt;Multiply each one by a decreasing weight: the first by 10, the second by 9, and so on down to the ninth by 2.&lt;/li&gt;
&lt;li&gt;Sum everything and take the remainder of the division by 11 (&lt;code&gt;sum % 11&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;If the remainder is less than 2, the digit is 0. Otherwise, the digit is &lt;code&gt;11 - remainder&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The second check digit follows the same logic, but now over the &lt;strong&gt;10 digits&lt;/strong&gt; (the 9 base ones plus the first check digit you just calculated), with the weights starting at 11.&lt;/p&gt;

&lt;p&gt;In code, computing a single digit looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;calculateDigit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;digits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;startingWeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;digits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;digits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;digit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;digit&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;startingWeight&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;remainder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;remainder&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;remainder&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that, generating a valid CPF becomes: roll 9 digits, calculate the first check digit, calculate the second. Never invalid.&lt;/p&gt;

&lt;p&gt;The CNPJ (the business ID) follows the same principle different weights, but the same idea of a modulo-11 check digit. Once you understand the pattern, it repeats.&lt;/p&gt;

&lt;h2&gt;
  
  
  The second pillar: determinism
&lt;/h2&gt;

&lt;p&gt;Generating valid data solves half the problem. The other half is &lt;strong&gt;reproducibility&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If every run of your test uses a different, random CPF, you lose two things: the ability to reproduce a failure, and the stability of snapshots. A test that generates random data is a test that can fail differently tomorrow without you having changed anything.&lt;/p&gt;

&lt;p&gt;That's why massa-br is &lt;strong&gt;seed-based&lt;/strong&gt;. You pass a seed, and the same seed always generates the same data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createGenerator&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;massa-br&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createGenerator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-seed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pessoa&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// The same seed always produces the same person reproducible.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ran the test, it failed, want to investigate? Same seed, same data, same scenario. The bug doesn't get to escape.&lt;/p&gt;

&lt;h2&gt;
  
  
  The third pillar: zero dependencies
&lt;/h2&gt;

&lt;p&gt;I made a point of not pulling in a single dependency. In a small, focused library, every dependency is a surface of risk a broken update, a transitive vulnerability, an abandoned package.&lt;/p&gt;

&lt;p&gt;The result is a tiny tarball and a dependency tree that is literally just your project. You install massa-br and you install nothing else alongside it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use it
&lt;/h2&gt;

&lt;p&gt;Install:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;massa-br
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basic usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createGenerator&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;massa-br&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createGenerator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;test-seed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pessoa&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// valid, coherent Brazilian data, ready to use in your tests&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The library covers the main Brazilian data types CPF, CNPJ, CEP (postal code), and phone numbers all valid by construction.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I learned building this
&lt;/h2&gt;

&lt;p&gt;Three things stuck with me after this project:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding the domain matters more than the language.&lt;/strong&gt; The code to generate a CPF is trivial. What made the library useful was understanding &lt;em&gt;why&lt;/em&gt; the check digit exists and how it's calculated that's domain knowledge, not syntax.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"Passes the test" and "is correct" are different things.&lt;/strong&gt; Data that passes a regex but fails real validation is the kind of trap that only shows up in production. Generating valid by construction kills the entire class of problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Publishing is a milestone in itself.&lt;/strong&gt; Putting your first library on npm, with a decent README and tests, teaches you about versioning, packaging, and documentation things we rarely practice in closed projects.&lt;/p&gt;




&lt;p&gt;&lt;code&gt;massa-br&lt;/code&gt; is on &lt;a href="https://www.npmjs.com/package/massa-br" rel="noopener noreferrer"&gt;npm&lt;/a&gt; and &lt;a href="https://github.com/oazevedolucas" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;. If it's useful to you, a star helps and if you'd like to contribute new generators (card numbers + Luhn and bank slips are on the roadmap), issues and PRs are welcome.&lt;/p&gt;

&lt;p&gt;How do you handle test data today? Let me know in the comments.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>opensource</category>
      <category>showdev</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
