<?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: akshaiim</title>
    <description>The latest articles on DEV Community by akshaiim (@akshaiim).</description>
    <link>https://dev.to/akshaiim</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%2F838090%2F2a9ace28-67fa-43e6-bfcd-8697431c313d.jpeg</url>
      <title>DEV Community: akshaiim</title>
      <link>https://dev.to/akshaiim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akshaiim"/>
    <language>en</language>
    <item>
      <title>Implement testing in node-express apis using jest and supertest</title>
      <dc:creator>akshaiim</dc:creator>
      <pubDate>Mon, 28 Mar 2022 17:23:38 +0000</pubDate>
      <link>https://dev.to/akshaiim/implement-testing-in-node-express-apis-using-jest-and-supertest-jal</link>
      <guid>https://dev.to/akshaiim/implement-testing-in-node-express-apis-using-jest-and-supertest-jal</guid>
      <description>&lt;p&gt;Testing is important to verify the behavior of the smallest parts of code in your application. It helps improve the quality of your code and reduces the amount of time and money you spend on bug fixing. Moreover, unit testing helps you find bugs early on in the development life cycle.  In Node.js there are many frameworks available for running unit tests. Some of them are:&lt;br&gt;
Mocha, Jest, Jasmine etc. &lt;/p&gt;

&lt;p&gt;we'll be looking at unit testing via jest and supertest below:&lt;/p&gt;

&lt;p&gt;Jest is a popular testing framework. It is developed and maintained by Facebook. One of the key features of jest is it is well documented, and it supports parallel test running i.e. each test will run in their own processes to maximize performance. It also includes several features like test watching, coverage, and snapshots.&lt;/p&gt;

&lt;p&gt;You can install it using the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install --save-dev jest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;By default, Jest expects to find all the test files in a folder called “tests” in your root folder.&lt;/p&gt;

&lt;p&gt;Example of a test using jest:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sum = require('./sum');

test('adds 1 + 2 to equal 3', () =&amp;gt; {
expect(sum(1, 2)).toBe(3);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Following are some assert types supported by jest:&lt;/p&gt;

&lt;p&gt;toBe&lt;br&gt;
eg: expect(2+2).toBe(4), &lt;br&gt;
toEqual&lt;br&gt;
eg:- expect(data).toEqual({one: 1, two: 2}) &lt;br&gt;
not.toBe&lt;br&gt;
eg:- expect(2+2).not.toBe(5)&lt;br&gt;
 toBeNull, toBeUndefined, toBegreaterThan, toBeLessThan, toContain, toMatch etc.&lt;/p&gt;

&lt;p&gt;following command can be used to run individual file&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jest &amp;lt;test file name&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;script to run all test files in test folder to be added in package.json&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"test": "jest"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;npm test can be used to run the tests&lt;/p&gt;

&lt;p&gt;Api endpoint testing can be done with the help of jest and another library called supertest.&lt;/p&gt;

&lt;p&gt;install supertest using &lt;br&gt;
&lt;code&gt;npm install --save-dev supertest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A sample api test can be implemented as shown below in test folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const request = require('supertest')
const app = require('./app')
const baseUrl = 'http://localhost:8000'

describe('sends users', (app) =&amp;gt; {
  it('should return users', async () =&amp;gt; {
    const res = await request(baseUrl)
      .get('/getUsers');
    expect(res.statusCode).toEqual(200);
    expect(res.body).toHaveProperty('users');
  });
});

describe('Post Endpoints', () =&amp;gt; {
  it('should create a new post', async () =&amp;gt; {
    const res = await request(baseUrl)
      .post('/posts')
      .send({
        userId: 1,
        title: 'test',
      })
    expect(res.statusCode).toEqual(200)
    expect(res.body).toHaveProperty('post')
  })
})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the tests using npm test and Test results can be seen in terminal console as seen below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RRBdk-gq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bf6ji7ws58iyp6e64bfs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RRBdk-gq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bf6ji7ws58iyp6e64bfs.png" alt="Image description" width="880" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For More Info, Visit:&lt;br&gt;
&lt;a href="https://jestjs.io/"&gt;https://jestjs.io/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/supertest"&gt;https://www.npmjs.com/package/supertest&lt;/a&gt;&lt;/p&gt;

</description>
      <category>testing</category>
      <category>jest</category>
      <category>supertest</category>
      <category>api</category>
    </item>
  </channel>
</rss>
