DEV Community

Maxim Nosov ✪
Maxim Nosov ✪

Posted on

Testing the tool

Overview

For my SSG tool I used Jest, because it was made for JavaScript language(language my tool is using) and pretty straightforward to use.

Set up

1) To start using jest simply run:

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

2) Create JavaScript file(e.g. sum.js) and add the following code:

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

3) Create testing file(sum.test.js) and add the following code:

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

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

4) Add the following to your package.json file:

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

6) Run

npm test
Enter fullscreen mode Exit fullscreen mode

Testing the tool

It was quite fun to test the tool for unpredictable behavior by passing invalid parameters to the function and checking the output.

Testing requires a lot of code, we need to consider covering most of testing scenarios to make sure that application doesn't crash.

Testing is crucial

I never worked with testing libraries before, but it's very important for any application.

People say, that it could be never guaranteed that your application will be bug free, but we can reduce amount of bugs by having significant amount of test cases.

To make sure we are testing all lines of code, we can use jest coverage. It makes possible to spot untested paths in our code.

Sum up

Testing application requires a lot of time, but it's required to provide high quality software. For future projects I will consider writing test.

Lastly, it's important not to overuse jest: run tests for your tests :)

Thank you for reading!

Top comments (0)