<?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: sagarMehta1907</title>
    <description>The latest articles on DEV Community by sagarMehta1907 (@sagarmehta1907).</description>
    <link>https://dev.to/sagarmehta1907</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%2F544369%2Ff700a8fd-7f0a-43e2-a2ae-32449aa06321.png</url>
      <title>DEV Community: sagarMehta1907</title>
      <link>https://dev.to/sagarmehta1907</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sagarmehta1907"/>
    <language>en</language>
    <item>
      <title>How to resolve async timeout in jest test case for async callback?</title>
      <dc:creator>sagarMehta1907</dc:creator>
      <pubDate>Tue, 09 Mar 2021 07:30:16 +0000</pubDate>
      <link>https://dev.to/sagarmehta1907/how-to-resolve-async-timeout-in-jest-test-case-for-async-callback-29ga</link>
      <guid>https://dev.to/sagarmehta1907/how-to-resolve-async-timeout-in-jest-test-case-for-async-callback-29ga</guid>
      <description>&lt;p&gt;How to resolve async timeout in jest test case for function?&lt;/p&gt;

&lt;p&gt;I worked with node js and jest test. I am facing issue while execute test case for one function "getValidData()" that function sometimes getting late to provide response. So jest test case was failed and provide error related to below async timeout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Error : Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Please refer below for "getValidData()" function and test case for the same.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actual Function: In data.js file&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.getValidData = function(){
return new Promise(function(resolve, reject){
    const payloadData = {
        'user_id': userid,
    };

    const res = await fetch('https://&amp;lt;token-url&amp;gt;',{
        method: 'POST',
        headers: {
            'Accept': 'application/json'
        },
        body: new URLSearchParams(payloadData )
    });

    if(res.status != 200){
        reject(res.message);
    }
    let responseData = await res.json();
    resolve(responseData);

});
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Test Case for Above function:In data.test.js file&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const dataTest = require("../data");
it("Test case for function getValidData", async()=&amp;gt;{
   await dataTest.getValidData().then(res =&amp;gt; {
       expect(res).not.tobeNull();
   }).catch(err =&amp;gt; {

   });
}, 30000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How can i resolve this issue related to timeout with test case for above "getValidData()" function? Please let me know your thoughts and guidance on how to pass and execute without failed the above test case for "getValidData()" function.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to cover code coverage for IF/ELSE condition with the node using the JEST unit test</title>
      <dc:creator>sagarMehta1907</dc:creator>
      <pubDate>Mon, 21 Dec 2020 06:24:28 +0000</pubDate>
      <link>https://dev.to/sagarmehta1907/how-to-cover-code-coverage-for-if-else-condition-with-the-node-using-the-jest-unit-test-34gh</link>
      <guid>https://dev.to/sagarmehta1907/how-to-cover-code-coverage-for-if-else-condition-with-the-node-using-the-jest-unit-test-34gh</guid>
      <description>&lt;p&gt;We are trying to cover code coverage for the node js function but for the if-else condition, we can't get code coverage, so any idea about that how to cover code coverage for the if-else condition.&lt;/p&gt;

&lt;p&gt;Please refer screenshot for sonar code coverage. &lt;/p&gt;

&lt;h3&gt;
  
  
  exports.findMyCompanies = function(user) {return new Promise(function(resolve, reject) {
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let query = `SELECT id,name,description,TYPE,website,org_type,linkedin_profile,logo_url,entity_type,(SELECT  count(*) from pages_access as pa where pa.shared_with_org_id = ? and pa.org_id = org.id) as privileges_count FROM organization org WHERE active=true  and org.id in (SELECT  distinct company_org_id org_id from company_advisor where advisor_org_id=?)`;

dbClient.getConnection().then(function(connection) {
  connection.beginTransaction(function(error_connection, resultData) {
    if (error_connection) {
      dbClient.END_TRANSACTION(connection, "ROLLBACK");
      reject(new Error(error_connection));
      return;
    }

    let orgGetQuery = "select * from `organization` where id=?";
    connection.query(orgGetQuery, [user.org_id], function(err, results) {
      if (err) {
        dbClient.END_TRANSACTION(connection, "ROLLBACK");
        reject(new Error(err));
        return;
      }

      if (
        results.length &amp;gt; 0 &amp;amp;&amp;amp;
        (results[0].type == SYSTEM_ADMIN_ORG ||
          results[0].type == "capitalprovider")
      ) {
        query = `
            SELECT id,
                 name,
                 description,
                 TYPE,
                 website,
                 org_type,
                 linkedin_profile,
                 logo_url,
                 entity_type
            FROM organization org where type='company' and active=true `;
      } else if (results.length &amp;lt; 1) {
        dbClient.END_TRANSACTION(connection, "ROLLBACK");
        reject(new Error("Unknown organization of user"));
        return;
      }

      connection.query(query, [user.org_id, user.org_id], function(
        error_getting_shared_companies,
        results_getting_shared_companies
      ) {
        if (error_getting_shared_companies) {
          dbClient.END_TRANSACTION(connection, "ROLLBACK");
          reject(new Error(error_getting_shared_companies));
          return;
        }

        dbClient.END_TRANSACTION(connection, "COMMIT");
        resolve(results_getting_shared_companies);
      });
    });
  });
});});};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Jest Unit test case for node js function&lt;/p&gt;

&lt;p&gt;it("The function find my company", async () =&amp;gt; {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user = { org_id: "92e1f7fe-93fc-4e51-a86e-8c4962dcef49" };
await advisor
  .findMyCompanies(user)
  .then(companyData =&amp;gt; {
    expect(companyData).not.toBeNull();
  })
  .catch(err =&amp;gt; {
    console.log("Error:- ", err);
  });  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;});&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
