DEV Community

Gulnur Baimukhambetova
Gulnur Baimukhambetova

Posted on

3

JavaScript testing with Jest

Hello everyone!

Today, I added testing to my SSGulnur project.

Jest πŸƒ

I chose Jest, as it is one of the most used testing frameworks for JavaScript and most of my tool's logic is written in JS. It was developed by Facebook and is open-source. Jest also has CLI option for checking coverage which is very great, it shows which lines of code have been tested and not and prints a beautiful report.

Set-up πŸ™Œ

Working with Jest is quite easy even for beginners (thanks to their well-written documentation).

First, I had to install it in my project. Can be done by running:

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

Then, I added the following scripts to my package.json to be able to run then through npm:

 "scripts": {
    "test": "jest --",
    "test:watch": "jest --watch --",
    "coverage": "jest --collect-coverage --",
Enter fullscreen mode Exit fullscreen mode

I also updated my CONTRIBUTING.md, so the future contributors find instructions for testing.

The Jest Framework is used for testing in this project.

You can use the following commands:

'npm test' for running test
'npm test example-test' for running only the tests that were specified with a pattern or filename (e.g. npm test createHTML)
'npm run test:watch' for running tests related to changed files based on git (uncommitted files)
'npm run coverage' for checking the coverage of the tests
Enter fullscreen mode Exit fullscreen mode

First unit tests πŸ₯‡

Unit tests allow to check a small pieces of logic independently. I had to restructure my code by breaking it into even smaller pieces so that I can work with more focused functions.

I added test folder to hold all my sample test files and tests. I used the snapshots to check if the output of the functions is as expected, because I work with big HTML files which would be hard to program manually.

Here is my test file:

const { createHTML } = require('../../src/helper');

describe('createHTML', () => {
  test('.md files have to properly transform into HTML', () => {
    expect(createHTML('test/sample.md')).toMatchSnapshot();
  });

  test('.txt files have to properly transform into HTML', () => {
    expect(createHTML('test/sample.txt')).toMatchSnapshot();
  });

  test('Style should change if given with .md file', () => {
    expect(
      createHTML('test/sample.txt', 'https://cdn.jsdelivr.net/npm/water.css@2/out/water.css')
    ).toMatchSnapshot();
  });

  test('Style should change if given with .txt file', () => {
    expect(
      createHTML('test/sample.txt', 'https://cdn.jsdelivr.net/npm/water.css@2/out/water.css')
    ).toMatchSnapshot();
  });
});
Enter fullscreen mode Exit fullscreen mode

Conclusion πŸ’‘

I am a newbie in testing and before it would get me very frustrated, as I used to think that it was a lot of unrewarding manual work. However, now, I understand its importance and learn about it more and more. Apparently, it is also not that hard to start. 😊

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay