Jest - NextensArelB/SwaggerGenerationTool GitHub Wiki

[[TOC]]

Overview

Jest JavaScript testing framework.

Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works with projects using: Babel, TypeScript, Node, React, Angular, Vue and more!

Pro:

  • Less configuration
  • Speed
  • Shares the same API as Jasmine which ships with Angular by default.
  • Relevant market share
  • Typescript support

Con:

  • Initial setup can be a little tricky

Why we chose jest?

It is a fast and widely supported testing framework for JavaScript.

Setup

You can find a setup guide for angular here

Speed comparison

Two vanilla Angular v13 projects are created. Project Angular-Example has no modifications. Project Jest-Example has replaced karma + jasmine with jest using the setup guide for angular

Using the PowerShell command Measure-Command we can measure the duration of each test framework and observe that, given the same project with the same tests, jest is 50% faster.

Angular-Example 11,24s

C:\.local\angular-example [master]> Measure-Command { ng test --no-watch }
- Generating browser application bundles (phase: setup)...
✔ Browser application bundle generation complete.

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 11
Milliseconds      : 243
Ticks             : 112435943
TotalDays         : 0,000130134193287037
TotalHours        : 0,00312322063888889
TotalMinutes      : 0,187393238333333
TotalSeconds      : 11,2435943
TotalMilliseconds : 11243,5943

Jest-Example 5,19s

C:\.local\jest-example> Measure-Command { npm run test }
PASS src/app/app.component.spec.ts
  AppComponent
    √ should create the app (174 ms)
    √ should have as title 'jest-example' (38 ms)
    √ should render title (34 ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        2.02 s, estimated 5 s
Ran all test suites.

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 5
Milliseconds      : 197
Ticks             : 51979614
TotalDays         : 6,01615902777778E-05
TotalHours        : 0,00144387816666667
TotalMinutes      : 0,08663269
TotalSeconds      : 5,1979614
TotalMilliseconds : 5197,9614

Marketshare

Openbase

A lookup on openbase reveals that JEST is overwhelmingly more used than competitors image.png

Stackoverflow

It also has healthy engagement on stackoverflow. With over 18,000 questions. Recent questions as little as 30min ago and questions asked as little as 3hrs ago already having answers at the time of making this. image.png

Debugging in VS-Code using extension support

Animation.gif

#Recommended folder sturcture for your Jest files It is recommended to use the following folder structure for you Jest test files:

src/__tests__/file.test.js

The tests in folder tests will automaticlly executed by Jest.

This folder structure lets you have multiple tests directories so tests are still near original files without cluttering the same directories.

⚠️ **GitHub.com Fallback** ⚠️