DEV Community

anshul137
anshul137

Posted on

DPS909 Blog :LAB 8 Automated Testing

Overview

One very important part of software development is test automation since it creates an automatic process for one or multiple types of tests to run without the need for manual intervention.
Testing helps a large community of developers (or even a single developer) keep a piece of software evolving in the right direction. Without tests, it's easy to break existing code, introduce unexpected bugs, or ignore important edge cases.
On my application ag-ssg I have used tools for automating my tests when working on a JavaScript/TypeScript codebase is JEST.

Jest is one of Facebook's open-source projects that is both under very active development and is also being used to ship code to everybody on Facebook.com.

Learned from this lab

  • general concepts and terminology of software testing
  • unit testing and integration testing

Set Up a Testing Framework

To get Started with JEST.

npm install --save-dev jest
Enter fullscreen mode Exit fullscreen mode

Let's get started by writing a test for a hypothetical function that adds two numbers. First, create a sum.js file:

function sum(a, b) {
  return a + b;
}
module.exports = sum;
Enter fullscreen mode Exit fullscreen mode

Then, create a file named sum.test.js. This will contain our actual test:

const sum = require('./sum');

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});
Enter fullscreen mode Exit fullscreen mode

Add the following section to your package.json:

{
  "scripts": {
    "test": "jest"
  }
}
Enter fullscreen mode Exit fullscreen mode

Finally, run yarn test or npm test and Jest will print this message if your code has passed all the implemented test

Image description

This is the first time when i have used the JEST to test my code. I have learned alot that how we cannot use the import statement when we are creating the .test.js file it will give us the syntax error we manually need to configure JEST so that it can work with the importstatement in the code.

Why to use Jest

List feature of jest

  • Simple and fast, ideal for prototyping.
  • It allows the generation of code coverage reports simply by adding the argument —-coverage to the command line.
  • Error reporting during test execution is rich and valuable.
  • Documentation and the community around the tool are really good.
Thanks
Enter fullscreen mode Exit fullscreen mode

Top comments (0)