<?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: Suresh Krishnan Nair</title>
    <description>The latest articles on DEV Community by Suresh Krishnan Nair (@sureshkrishnannair).</description>
    <link>https://dev.to/sureshkrishnannair</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%2F1397657%2F4ed33859-c066-4001-8e4f-7c7c631d791d.jpg</url>
      <title>DEV Community: Suresh Krishnan Nair</title>
      <link>https://dev.to/sureshkrishnannair</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sureshkrishnannair"/>
    <language>en</language>
    <item>
      <title>Creating Fixtures from Response Bodies dynamically using Cypress Exec(), a practical approach</title>
      <dc:creator>Suresh Krishnan Nair</dc:creator>
      <pubDate>Thu, 04 Apr 2024 16:49:42 +0000</pubDate>
      <link>https://dev.to/sureshkrishnannair/creating-fixtures-from-response-bodies-dynamically-using-cypress-exec-a-practical-approach-b8l</link>
      <guid>https://dev.to/sureshkrishnannair/creating-fixtures-from-response-bodies-dynamically-using-cypress-exec-a-practical-approach-b8l</guid>
      <description>&lt;p&gt;&lt;code&gt;cy.exec()&lt;/code&gt; is used to execute shell commands directly within your Cypress tests. This feature enables you to interact with the underlying operating system, perform tasks such as working with the file system, running build scripts, seeding your test database, starting processes, killing processes or executing any other command-line operations needed during your tests.&lt;/p&gt;

&lt;p&gt;In this article, we explore how the &lt;code&gt;cy.exec()&lt;/code&gt; command can be utilized to handle test fixtures effectively by creating fixtures from response bodies dynamically, along with examples and best practices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding Test Fixtures:&lt;/strong&gt; Test fixtures are pre-defined data used as inputs for executing tests. These fixtures would include JSON files, database snapshots, or any other data source required for test scenarios.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; Creating Fixtures from API Responses&lt;/p&gt;

&lt;p&gt;In this scenario ,we are using &lt;code&gt;exec()&lt;/code&gt; to write to a file to create a fixture from response body&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cy.intercept('POST', '/comments').as('postComment')
cy.get('.add-comment').click()
cy.wait('@postComment').then(({ response }) =&amp;gt; {
//execute shell command 
 cy.exec(
    `echo ${JSON.stringify(response.body)} &amp;gt;cypress/fixtures/comment.json`
  )
  cy.fixture('comment.json').should('deep.eq', response.body)
})
&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;//Reads data from comment.json fixture
cy.fixture('comment.json').then((commentData) =&amp;gt; {
      // Use the commentData in your test
      console.log(commentData);
      // Add assertions or other test logic here
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets breakdown the command &lt;code&gt;cy.exec()&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The backticks `` are used for template literals in JavaScript, allowing embedded expressions.&lt;/li&gt;
&lt;li&gt;The echo command is used to output text.&lt;/li&gt;
&lt;li&gt;${JSON.stringify(response.body)} is an embedded expression that converts the response.body object into a JSON string.&lt;/li&gt;
&lt;li&gt;&amp;gt; is a shell commands, used to redirect the output of the preceding command to a file.&lt;/li&gt;
&lt;li&gt;cypress/fixtures/comment.json specifies the path and filename where the output of the echo command will be written.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This approach helps to verify the intercepted data from the API against the UI, or for other assertions .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage Considerations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensure that the response.body object contains valid JSON data. Otherwise, the JSON.stringify() function may throw an error.&lt;/li&gt;
&lt;li&gt;Keep in mind that using cy.exec() to write fixture files can be useful for capturing dynamic or changing data during test runs, but it may introduce dependencies on the local environment or require additional clean up steps in test suites.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I would like to acknowledge the Cypress documentation for its comprehensive resources and helpful examples that enriched the content of this article.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Understanding Callback Functions in TypeScript: A Beginner’s Guide</title>
      <dc:creator>Suresh Krishnan Nair</dc:creator>
      <pubDate>Sat, 30 Mar 2024 16:53:38 +0000</pubDate>
      <link>https://dev.to/sureshkrishnannair/understanding-callback-functions-in-typescript-a-beginners-guide-1579</link>
      <guid>https://dev.to/sureshkrishnannair/understanding-callback-functions-in-typescript-a-beginners-guide-1579</guid>
      <description>&lt;p&gt;&lt;strong&gt;What are Callback Functions?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Callback functions are functions passed as arguments to other functions and executed at a later time or after a specific event. They allow us to define behavior that should occur once a certain task is completed. Imagine them as “callbacks” to let you know when something has finished.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Exploring the Structure of a Callback Function&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Higher-Order Function&lt;/strong&gt;: The function that takes a callback function as an argument.&lt;br&gt;
&lt;strong&gt;Callback Function&lt;/strong&gt;: The function passed as an argument to the higher-order function.&lt;br&gt;
&lt;strong&gt;Asynchronous Task&lt;/strong&gt;: The task that takes time to complete (e.g., fetching data from a server).&lt;br&gt;
&lt;strong&gt;Completion Event&lt;/strong&gt;: Triggered when the asynchronous task is finished.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Illustrating with a Diagram&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;
   +-----------------------------+
   |   Higher-Order Function    |
   +-----------------------------+
   |                             |
   |    +----------------------+ |
   |    |   Callback Function  | |
   |    +----------------------+ |
   |    |                      | |
   |    |   (Task Completed)   | |
   |    |                      | |
   |    +----------------------+ |
   |                             |
   +-----------------------------+
            |
            |
   +-----------------------------+
   |   Asynchronous Task        |
   +-----------------------------+
            |
            |
   +-----------------------------+
   |   Completion Event         |
   +-----------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example in TypeScript&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;function fetchData(callback: (data: any) =&amp;gt; void) {
  // Simulate fetching data asynchronously
  setTimeout(() =&amp;gt; {
    const data = 'Sample data 1';
    callback(data);
  }, 5000);

  console.log("Initiated "); // Added log message to see when fetchData is initiated

  // Simulate fetching more data asynchronously after a delay
  setTimeout(() =&amp;gt; {
    const data = 'Sample data 2';
    callback(data);
  }, 6000);
}

// Usage of the callback function
fetchData((data) =&amp;gt; {
  console.log('Data received:', data);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&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;Initiated
Data received: Sample data 1
Data received: Sample data 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Let’s break down the provided TypeScript example into its components:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Higher-Order Function&lt;/strong&gt;: &lt;em&gt;fetchData&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fetchData is the higher-order function in this example.&lt;/li&gt;
&lt;li&gt;It takes a callback function as a parameter.&lt;/li&gt;
&lt;li&gt;The callback function represents the behavior that should occur once the data is fetched.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Callback Function Parameter&lt;/strong&gt;: &lt;em&gt;(callback: (data: any) =&amp;gt; void)&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The callback function parameter is defined in the fetchData function signature.&lt;/li&gt;
&lt;li&gt;It accepts a function as an argument, which takes one parameter data of type any and returns void.&lt;/li&gt;
&lt;li&gt;This parameter is a function reference that will be invoked when the data fetching operation completes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Asynchronous Task&lt;/strong&gt;: &lt;em&gt;setTimeout&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inside the fetchData function, there are two setTimeout calls, which simulate asynchronous tasks.&lt;/li&gt;
&lt;li&gt;These asynchronous tasks represent the fetching of data from an external source.&lt;/li&gt;
&lt;li&gt;Each setTimeout call has a delay before executing its callback function, mimicking the time taken to fetch the data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Completion Event&lt;/strong&gt;: &lt;em&gt;Callback Invocation&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The completion event occurs when the setTimeout functions execute their callback functions.&lt;/li&gt;
&lt;li&gt;Inside each setTimeout callback function, the callback parameter (the provided callback function) is invoked with the fetched data as an argument.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Usage of the Callback Function&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The fetchData function is called with a callback function as an argument.&lt;/li&gt;
&lt;li&gt;This callback function defines what should be done with the fetched data.&lt;/li&gt;
&lt;li&gt;In this example, the callback function logs the received data to the console.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Execution Flow&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The fetchData function is called with a callback function as an argument.&lt;/li&gt;
&lt;li&gt;Inside fetchData, the asynchronous tasks (data fetching) are initiated using setTimeout.&lt;/li&gt;
&lt;li&gt;After the specified delay, each setTimeout executes its callback function.&lt;/li&gt;
&lt;li&gt;Inside each callback function, the provided callback function (callback) is invoked with the fetched data.&lt;/li&gt;
&lt;li&gt;The provided callback function handles the received data as specified in its definition.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall, this example illustrates how callback functions can be used to handle asynchronous operations, allowing us to define behavior that occurs once the asynchronous tasks are completed.&lt;/p&gt;

&lt;p&gt;Disclaimer: “This article was created with the help of AI”&lt;/p&gt;

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