<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pouya Jabbari Sani on Medium]]></title>
        <description><![CDATA[Stories by Pouya Jabbari Sani on Medium]]></description>
        <link>https://medium.com/@pouyajabbarisani?source=rss-e0f36d508814------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Ioacvx9USzeF40xT2m10cQ.jpeg</url>
            <title>Stories by Pouya Jabbari Sani on Medium</title>
            <link>https://medium.com/@pouyajabbarisani?source=rss-e0f36d508814------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 17:33:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pouyajabbarisani/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to write React Tests using Jest and Enzyme — part 2]]></title>
            <link>https://medium.com/pouya-jabbarisani/how-to-write-react-tests-using-jest-and-enzyme-part-2-661368301944?source=rss-e0f36d508814------2</link>
            <guid isPermaLink="false">https://medium.com/p/661368301944</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[enzyme]]></category>
            <dc:creator><![CDATA[Pouya Jabbari Sani]]></dc:creator>
            <pubDate>Sat, 28 Dec 2019 08:03:37 GMT</pubDate>
            <atom:updated>2019-12-28T08:03:37.774Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bkk7Df364MJL3d5B6K7DMQ.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@lazuran-calin-731955">Lăzuran Călin</a> from <a href="https://www.pexels.com/">pexels.com</a></figcaption></figure><h3>How to write React Tests using Jest and Enzyme — part 2</h3><p>in <a href="https://medium.com/pouya-jabbarisani/how-to-write-react-tests-using-jest-and-enzyme-part-1-13d24438e1fd"><strong>part 1</strong></a>, we learned different types of tests and some of the testing tools. If you haven’t read Part 1, I suggest you read it first:</p><p><a href="https://medium.com/pouya-jabbarisani/how-to-write-react-tests-using-jest-and-enzyme-part-1-13d24438e1fd">How to write React Tests using Jest and Enzyme — part 1</a></p><p>Test React applications using Jest and Enzyme</p><h4>Pre-requirements</h4><p>First of all, we need to install Jest and Enzyme on our project using npm, as we know testing is a development based task and because of this we are using <em>— save-dev</em> flag to add it to our dev-dependencies section in package.json:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*J7A-N4FzxY_M6hr6TVDOwg.png" /></figure><p>Then, we need to make a decision about directory structure of our test files in our project; there is two way to store the test files in our project: first, save them in a separated directory called <strong><em>__tests__</em></strong><em> </em>as normal .js files and second, save every single component’s related test file behind of that with the same name ending with <strong><em>.test.js</em></strong> or <strong><em>.spec.js</em></strong></p><h4>Writing the first test</h4><p>Let’s start with a simple assertion test via Jest:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6e9d45d80244ecb631b0a8cc84bece8/href">https://medium.com/media/c6e9d45d80244ecb631b0a8cc84bece8/href</a></iframe><p>As you might guess, If we run this test, It will pass because 2+2 is equal to 4.</p><h4>Grouping the tests</h4><p>If we want to test a group of related things, we can write related tests in a group using <strong><em>describe </em></strong>as down below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ebb3d6b21dbbbf61ac79990866608993/href">https://medium.com/media/ebb3d6b21dbbbf61ac79990866608993/href</a></iframe><h3><strong>Testing async functions</strong></h3><p>The way of testing async functions is a little bit different than the normal functions. As we know, there are two types of async function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f6b8538eda061235925cd9cbd299700/href">https://medium.com/media/2f6b8538eda061235925cd9cbd299700/href</a></iframe><p>If we use them as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67fd4ad7b61bfc6f2cf1d850b058af82/href">https://medium.com/media/67fd4ad7b61bfc6f2cf1d850b058af82/href</a></iframe><p>It does not work as expected (test will ends successfully without running the expect()).</p><p>So, for async (promise) functions, we need to use expect.<strong><em>assertions</em></strong>(count of expects in the test) to make sure all of them ran, like below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/85b7d306fb8e8000febb72533409a40a/href">https://medium.com/media/85b7d306fb8e8000febb72533409a40a/href</a></iframe><p>But, it will not pass and we need to take one step more to make it complete; we can take this step in two way:</p><p>1- using <strong>done</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d421c313317c11e2fc870a9ed17531b8/href">https://medium.com/media/d421c313317c11e2fc870a9ed17531b8/href</a></iframe><p>2- using <strong>return</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24745a4b5c27c019dc0c143e517a1290/href">https://medium.com/media/24745a4b5c27c019dc0c143e517a1290/href</a></iframe><h3><strong>Mock functions in Jest</strong></h3><p>If we put real <em>fetch</em> to all of our tests, It will take a lot of time to completely run all of our tests, In addition, Many times there are some uncontrollable resources in out test and we have to mock them, so, Jest provides us with a solution called <a href="https://jestjs.io/docs/en/mock-function-api.html"><strong><em>mock functions</em></strong></a><strong><em> </em></strong><em>(also known as “spies”) </em>to mock the fetch event.</p><p>It makes out tests run time much faster, but bear in mind, in some situations, we need to use real fetch based on the importance of that piece of the code. Here is a basic instruction to mocks in Jest:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae5b60f40a36bb5cb4a0e96d0a7adfdc/href">https://medium.com/media/ae5b60f40a36bb5cb4a0e96d0a7adfdc/href</a></iframe><p>There are lots of methods for different kinds of stuff provided for Jest mock function like <em>toHaveBeenCalledWith()</em>,<em> mockReturnValue(), </em>and more which <a href="https://jestjs.io/docs/en/mock-function-api.html">you can see in here</a>.</p><h3><strong>Enzyme</strong></h3><p>Enzyme makes us easily able to test <em>react</em> component’s output. to get started with Enzyme, we need to install <strong><em>enzyme</em></strong> and <strong><em>enzyme-adapter-react-16 </em></strong>npm packages as dev-dependencies in our project, then, we need to make a file with setupTests.js name to configure Enzyme to use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a8f18e3c7e07b50f7471dfe6f024046/href">https://medium.com/media/3a8f18e3c7e07b50f7471dfe6f024046/href</a></iframe><p>Then in the test file of a component (eg: card.test.js):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/385b3b6a149b6e08907463e1285b2110/href">https://medium.com/media/385b3b6a149b6e08907463e1285b2110/href</a></iframe><h4>Snapshot testing in Enzyme</h4><p>To avoid from rendering the component on every code edit, we can take a snapshot to store and compare our code with that snapshot to detect if there are any changes or not, and also, we can press u key in the terminal to take a new snapshot if there are any changes.</p><h3>How to write a test for app component</h3><p>In somewhere like app component, which could become so complex, we need to split its content into another component and then, we actually don’t need to test out app component because it’s made from redux related things and/or router and all of these things are valid and official components which have been tested by their developers. and then we just need to test our inner content component!</p><h3><strong>How to run a special code before then each test</strong></h3><p>Jest provides us with a function called beforeEach() and we can use it to run our custom code before then each test. It also has another function called beforeAll() to run our custom code before then all of our tests. you can see a full list of such functions in <a href="https://jestjs.io/docs/en/setup-teardown"><strong>here</strong></a>.</p><p>for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b7694690f59a83c6eee522a0efe923d/href">https://medium.com/media/6b7694690f59a83c6eee522a0efe923d/href</a></iframe><h3><strong>Accessing the component’s methods in the test file</strong></h3><p>We can use the instance() method to get access to the component’s methods like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd65279c3b41f42cb523332dc5c8b7fc/href">https://medium.com/media/dd65279c3b41f42cb523332dc5c8b7fc/href</a></iframe><h3>Conclusion</h3><p>As you probably realized, testing and writing tests is a massive and wide topic and no one can’t teach it in just one or two posts, but in these posts, I just tried to give you the vision and how to start to become an expert path. Now you know the concepts and different aspects of testing and you can go ahead and continue with reading documentation of these libraries/frameworks.</p><p>🇬🇧💼 By the way, I’m looking for a visa-sponsored job in the UK. If you are looking someone like <a href="http://pouyajabbarisani.com/"><strong>me</strong></a> to hire, send me a message.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=661368301944" width="1" height="1" alt=""><hr><p><a href="https://medium.com/pouya-jabbarisani/how-to-write-react-tests-using-jest-and-enzyme-part-2-661368301944">How to write React Tests using Jest and Enzyme — part 2</a> was originally published in <a href="https://medium.com/pouya-jabbarisani">Pouya Jabbari Sani’s Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to write React Tests using Jest and Enzyme — part 1]]></title>
            <link>https://medium.com/pouya-jabbarisani/how-to-write-react-tests-using-jest-and-enzyme-part-1-13d24438e1fd?source=rss-e0f36d508814------2</link>
            <guid isPermaLink="false">https://medium.com/p/13d24438e1fd</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[tdd]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[test]]></category>
            <category><![CDATA[jest]]></category>
            <dc:creator><![CDATA[Pouya Jabbari Sani]]></dc:creator>
            <pubDate>Mon, 14 Oct 2019 08:26:04 GMT</pubDate>
            <atom:updated>2019-12-28T08:04:48.057Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to write React Tests using Jest and Enzyme — part 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bU9W86g9Tgxmyu_XmmNdEw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@serjosoza">@serjosoza</a> from <a href="https://unsplash.com/photos/x7-YccVlYQQ">unsplash.com</a></figcaption></figure><p>Nowadays developers and tech recruiters are talking about <strong>Test Driven Development </strong>in<strong> </strong>everywhere<strong>;</strong> In the job descriptions, conferences, job interviews and so on. but I think the main importance of understanding how to and why to write tests is in the project maintenance and long-term development.</p><p>Long-term development with a team (every developer writes different parts of code which different ingredients binding and collaborating together at the end) and confidence in deploying a project to production without writing and running code is a nightmare!</p><p>So, anyway, we have to learn writing and using test. and this article is an easy and quick way to learn how to write tests which include fundamentals of writing test and TDD and in the following, we are going to learn how to write tests using <strong><em>Jest</em></strong> and <strong><em>Enzyme</em></strong> for <strong>React</strong> applications. (you can use Jest for any other <strong>JavaSciprt</strong> based application like NodeJs/Angular applications, too.)</p><h3>Different Types of tests</h3><p>We have different types of tests in software development for testing different parts and functionality of our application/software:</p><ul><li><strong>Unit tests: </strong>for<strong> </strong>testing functionality of a <strong>single function or class</strong>. Unit tests should cover all small pure functions of the application.</li><li><strong>Integration tests: </strong>for<strong> </strong>testing relations and <strong>communications</strong> between two parts of our application (eg: is MongoDB working with express correctly?). Integration tests are all about cross communications between different units of code.</li><li><strong>Automation tests: </strong>for<strong> </strong>testing if our application working and behaves as expected in the browser? Automation tests are <strong>UI tests</strong> that are always run on the <strong>browser</strong> or browser-like environment. It includes testing things like clicking, typing, and scrolling.</li></ul><h3><strong>Testing Tools</strong></h3><p>There are various tools and libraries for writing tests in JavaScript for different purposes. some of these libraries used for a specific part of the test and some of them supports more parts.</p><h4>1- Frameworks</h4><p>Includes <a href="https://jasmine.github.io/"><em>Jasmine</em></a>, <a href="https://jestjs.io/"><strong><em>Jest</em></strong></a><strong><em>, </em></strong><a href="https://mochajs.org/"><em>Mocha</em></a><em>, and </em><a href="https://cypress.io/"><em>Cypress</em></a>.</p><p>Frameworks are comprehensive and support various tasks to write and things to do. for example, most of them have its own assertion, test runner and so on.</p><h4>2- Assertion Libraries</h4><p>Includes <em>Jasmine</em>, <strong><em>Jest</em> </strong>and<strong> </strong><a href="https://www.chaijs.com/"><em>Chai</em></a>.</p><p>These libraries help us to reduce the size of code for writing assertions. By using these libraries we no longer need to write complex if/else if statements to check if a given value is <em>equal/bigger/smaller/&amp;…</em> with our expected value or not. Instead, we can simply check them using one line of code like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*SJS_gWEqt6gKd1eIXXvfUg.png" /></figure><h4>3- Spies, Stubs &amp; Mocks</h4><p>Includes <em>Jasmine</em>, <strong><em>Jest </em></strong>and<strong><em> </em></strong><a href="https://sinonjs.org/"><em>Sinon</em></a><em>.</em></p><p><strong>Mocks</strong> are fake methods with pre-programmed behaviour. We use mocks to verify a specified method getting called with what parameters and things like that.</p><p><strong>Stubs</strong>, on the other hand, made for a little bit different task; using stubs, we are replacing a specific method of a component or class with our fake method to simulate the behaviour of that for testing a scenario.</p><p><strong>Spies</strong> wraps a real object and see when, how time, and with what parameters is called.</p><h4>4- Snapshot Testing</h4><p>As I know, there is just one library which supports snapshot testing; <strong>Jest.</strong></p><p>As Jest’s documentation says, snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly.</p><p>It renders a UI component, takes a snapshot from it and save it in a specific directory in our project and then compares the current version with that in every time which we run the test to check if there are any changes or not.</p><p>We must use them just for structures that changing them can cause unexpected problems.</p><h4>5- Test Runners</h4><p>Includes <em>Jasmine</em>, <strong><em>Jest</em></strong>, <em>Mocha</em>, and <a href="https://karma-runner.github.io/latest/index.html"><em>Karma</em></a> (karma is for running tests in browser).</p><p>Every written test need a runner to run. some of the testing libraries have their own runners to run the written tests (like Jasmine and Jest) and some of them need an external library to run the tests.</p><h4>6- Code Coverage</h4><p>Includes <a href="https://istanbul.js.org/"><em>Istanbul</em></a><em> and </em><strong><em>Jest</em></strong> <em>(It uses Istanbul. too!)</em></p><p>It’s important to determine how the percent of our code is being tested and is reliable. for getting wide information about this, we have to use <em>test code coverage</em> tools like Istanbul.</p><p>As you probably noticed <strong>Jest</strong> supports all of these parts and it makes you needless to use other libraries especially if you want to write your tests for the react-powered app. So, I’m going to show you how we can write tests via Jest.</p><p>end of part 1.</p><p><a href="https://medium.com/@pouyajabbarisani/how-to-write-react-tests-using-jest-and-enzyme-part-2-661368301944">How to write React Tests using Jest and Enzyme — part 2</a></p><p>🇬🇧💼 By the way, I’m looking for a visa-sponsored job in the UK. If you are looking someone like <a href="http://pouyajabbarisani.com/"><strong>me</strong></a> to hire, send me a message.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13d24438e1fd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/pouya-jabbarisani/how-to-write-react-tests-using-jest-and-enzyme-part-1-13d24438e1fd">How to write React Tests using Jest and Enzyme — part 1</a> was originally published in <a href="https://medium.com/pouya-jabbarisani">Pouya Jabbari Sani’s Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Make Discount coupon codes in NodeJs & MongoDb]]></title>
            <link>https://medium.com/pouya-jabbarisani/make-a-discount-coupon-code-in-nodejs-mongodb-ac1ac5d7c588?source=rss-e0f36d508814------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac1ac5d7c588</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[discount-code]]></category>
            <category><![CDATA[discount-coupon-codes]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[mongodb]]></category>
            <dc:creator><![CDATA[Pouya Jabbari Sani]]></dc:creator>
            <pubDate>Fri, 15 Jun 2018 13:01:32 GMT</pubDate>
            <atom:updated>2019-10-09T13:46:14.502Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3hJX9vJtjmefTGKDrCIdHg.jpeg" /><figcaption><a href="https://www.pexels.com/photo/working-macbook-computer-keyboard-34577/">by Negative Space @ pexels</a></figcaption></figure><p>Maybe you would like to build a discount system for your online store or platform. In the first look, It seems a little scary and complicated, but it’s so easy in NodeJs/MongoDB based systems!<br>First of all, You need to add a discount model to your model’s folder in your product ready nodeJs project:</p><pre>var mongoose = require(‘mongoose’);</pre><pre>var connection = mongoose.createConnection(“mongodb://localhost/{customName}”);</pre><pre>var DiscountCodesSchema = mongoose.Schema(</pre><pre>{code: { type: String, require: true, unique: true },</pre><pre>isPercent: { type: Boolean, require: true, default: true },</pre><pre>amount: { type: Number, required: true } // if is percent, then number must be ≤ 100, else it’s amount of discount</pre><pre>expireDate: { type: String, require: true, default: ‘’ },</pre><pre>isActive: { type: Boolean, require: true, default: true }</pre><pre>});</pre><pre>DiscountCodesSchema.pre(‘save’, function (next) {</pre><pre>var currentDate = new Date();</pre><pre>this.updated_at = currentDate;</pre><pre>if (!this.created_at) {</pre><pre>this.created_at = currentDate;</pre><pre>}</pre><pre>next();</pre><pre>});</pre><pre>var Discounts = mongoose.model(‘DiscountCodes’, DiscountCodesSchema);</pre><pre>module.exports = Discounts;</pre><p>Then, you need to make a random code generator for making discount coupon codes, For this step, I prefer to make a separate module and save my function in it:</p><pre>function coupongenerator() {</pre><pre>var coupon = “”;</pre><pre>var possible = “abcdefghijklmnopqrstuvwxyz0123456789”;</pre><pre>for (var i = 0; i &lt; {custom code length in here}; i++) {</pre><pre>coupon += possible.charAt(Math.floor(Math.random() * possible.length));</pre><pre>}</pre><pre>return coupon;</pre><pre>}</pre><pre>module.exports = coupongenerator;</pre><p>Then, We can easily use it in our API route. BUT before than saving generated code, you need to check if code is existing, Make a new one:</p><pre>…</pre><pre>let isExistDiscount = false</pre><pre>do {</pre><pre>let myDiscountCode = coupongenerator()</pre><pre>let newDiscountCode = new DiscountCode({</pre><pre>code: myDiscountCode,</pre><pre>isPercent: false,</pre><pre>amount: [{ IRT: 5000 }, { USD: 5 }, { EUR: 5 }],</pre><pre>expireDate: ‘’,</pre><pre>isActive: true</pre><pre>})</pre><pre>newDiscountCode.save(function (err) {</pre><pre>if (err) {</pre><pre>if (err.name === ‘MongoError’ &amp;&amp; err.code === 11000) {</pre><pre><em>// Duplicate code detected</em></pre><pre>isExistDiscount = true;</pre><pre>}</pre><pre>}</pre><pre>res.send({</pre><pre><em>//success message render</em></pre><pre>})</pre><pre>})</pre><pre>}</pre><pre>while (isExistDiscount);</pre><pre>…</pre><p>And finally, you can use the discount coupon code field in anywhere check and process it from MongoDB.</p><p>Good luck!</p><p>By the way, I’m looking for a visa-sponsored job in the UK. If you are looking someone like <a href="http://pouyajabbarisani.com/"><strong>me</strong></a> to hire, send me a message.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac1ac5d7c588" width="1" height="1" alt=""><hr><p><a href="https://medium.com/pouya-jabbarisani/make-a-discount-coupon-code-in-nodejs-mongodb-ac1ac5d7c588">How to Make Discount coupon codes in NodeJs &amp; MongoDb</a> was originally published in <a href="https://medium.com/pouya-jabbarisani">Pouya Jabbari Sani’s Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>