<?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: CarlosAndress</title>
    <description>The latest articles on Forem by CarlosAndress (@carlos-andress).</description>
    <link>https://forem.com/carlos-andress</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%2F664809%2F3bae1242-e5e1-4660-98e5-0aa5804e67fe.jpeg</url>
      <title>Forem: CarlosAndress</title>
      <link>https://forem.com/carlos-andress</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/carlos-andress"/>
    <language>en</language>
    <item>
      <title>How to write tests with Typescript and jest</title>
      <dc:creator>CarlosAndress</dc:creator>
      <pubDate>Fri, 12 Apr 2024 18:24:29 +0000</pubDate>
      <link>https://forem.com/carlos-andress/how-to-write-tests-with-typescript-and-jest-3p6h</link>
      <guid>https://forem.com/carlos-andress/how-to-write-tests-with-typescript-and-jest-3p6h</guid>
      <description>&lt;p&gt;&lt;strong&gt;Step 1: Set Up Your TypeScript Project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a new folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir test
cd test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a new TypeScript project.&lt;/p&gt;

&lt;p&gt;Initialize your project and create a &lt;code&gt;package.json&lt;/code&gt; file if you don't have one already:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pnpm init

pnpm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install TypeScript and Jest as development dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i jest typescript ts-jest ts-node @types/jest @types/node -D

pnpm add jest typescript ts-jest ts-node @types/jest @types/node -D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Configure TypeScript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;tsconfig.json&lt;/code&gt; file to configure TypeScript. &lt;br&gt;
You can use the &lt;code&gt;tsc --init&lt;/code&gt; command or create it manually.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "target": "ES6",
    "module": "CommonJS",
    "outDir": "./dist",
    "rootDir": "./src"
  }
},
  "include": [
    "src"
  ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a &lt;code&gt;jest.config.ts&lt;/code&gt; file to configure Jest&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pnpm create jest 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import type {Config} from 'jest';

  const config: Config = {
  collectCoverage: true,
  coverageProvider: "v8",
  preset: 'ts-jest',
  roots: [
     "&amp;lt;rootDir&amp;gt;"
   ],
  testEnvironment: "node",
  verbose: true,
  transform: {}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a &lt;code&gt;src&lt;/code&gt; directory for your TypeScript source files and a &lt;code&gt;tests&lt;/code&gt; directory for your test files.&lt;/p&gt;

&lt;p&gt;Creates a file insider the folder &lt;code&gt;src&lt;/code&gt; you migh named &lt;code&gt;operation.ts&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;export function Sum(a: number, b: number): number {
  return a + b;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;import the file we created in the test location into a new file named &lt;code&gt;operation.test.ts&lt;/code&gt; or &lt;code&gt;operation.spec.ts&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;import { Sum} from "../src/operation";

describe("Math functions", () =&amp;gt; {
  test("should add two numbers correctly", () =&amp;gt; {
    expect(add(1, 2)).toEqual(3);
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Run Your Tests&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add a script to your &lt;code&gt;package.json&lt;/code&gt; to run the tests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "test": "jest"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run your tests using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run test

pnpm run test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>typescript</category>
      <category>programming</category>
      <category>node</category>
      <category>testing</category>
    </item>
    <item>
      <title>Parameter 'element' implicitly has an 'any' type.</title>
      <dc:creator>CarlosAndress</dc:creator>
      <pubDate>Fri, 08 Mar 2024 07:23:01 +0000</pubDate>
      <link>https://forem.com/carlos-andress/parameter-element-implicitly-has-an-any-type-281a</link>
      <guid>https://forem.com/carlos-andress/parameter-element-implicitly-has-an-any-type-281a</guid>
      <description>&lt;p&gt;To solve the error we must only type the best way this particular error happened to me with useRef creating a slider here I leave an example of code.&lt;/p&gt;

&lt;p&gt;This is the part of the code that was causing the error and I solved it this way I summarized it so that it was not so long.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Slider = () =&amp;gt; {
  const elementRef = useRef&amp;lt;HTMLInputElement&amp;gt;(null);

  const sliderRight=(element:HTMLElement )=&amp;gt;{
    element.scrollLeft += screenWidth - 110
  }
  const sliderLeft=(element: HTMLElement)=&amp;gt;{
    element.scrollLeft -= screenWidth -110
  }


  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;HiChevronLeft className="hidden md:block text-white text-[30px] absolute
        mx-8 mt-[150px] cursor-pointer" onClick={() =&amp;gt; sliderLeft(elementRef.current as HTMLElement)}/&amp;gt;

        &amp;lt;HiChevronRight className="hidden md:block text-white text-[30px] absolute
        mx-8 mt-[150px] cursor-pointer right-0" onClick={() =&amp;gt; sliderRight(elementRef.current as HTMLElement)}/&amp;gt;

      &amp;lt;/div&amp;gt;
  )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>typescript</category>
      <category>programming</category>
      <category>webdev</category>
      <category>react</category>
    </item>
  </channel>
</rss>
