kdocs
GitHub
Lang - General
Lang - General
  • Code Versioning
    • Git
      • Configurations
      • Conventional Commit
      • Workflows
    • GitHub
      • Git Actions
    • GitOps
    • SemVer
  • Tests
    • Jest
Powered by GitBook
On this page
  • Installing
  • Running
  • By specific filename
  • With coverage
  • On watch mode
  • Test Structure
  • Creating tests
  • Run multiple values
  • Run specific test
  • Skip tests
  • Concurrent tests
  1. Tests

Jest

Tool for running tests over Javascript/Typecript

PreviousSemVer

Last updated 4 months ago

Installing

npm install --save-dev jest @types/jest ts-jest

Because we are running with ts-jest create the jest.config.js file, which will inform Jest how to handle .ts files correctly.

npx ts-jest config:init

Test files for Jest are named <file>.test.ts.

By specific filename

Specific filename AND path

Run only the tests that were specified with their exact paths.

npx jest --runTestsByPath [path]/[filename]

Specific filename

Find and run the tests that cover a space separated list of source files.

It will regex only by the specified name, so if multiple files have the same name it will execute all of them.

npx jest --findRelatedTests [filename]

With coverage

npx jest --coverage

On watch mode

Watch files for changes and rerun tests.

To rerun only changed files

# Runs `jest -o` by default
npx jest --watch

To rerun all files

npx jest --watchAll

Creating tests

Create a test by using the test() funtion or its alias it().

Will be created a top-level scope.

test('', () => {});

// Or

it('', () => {});

Using describe()

Create a block that groups together several related tests.

This isn't required - you can write the test blocks directly at the top level. But this can be handy if you prefer your tests to be organized into groups.

You can also nest describe blocks if you have a hierarchy of tests.

A describe block also defines a new scope for variables and other functions like beforeAll(), beforeEach(), afterAll() and afterEach().

describe('', () => {
    it('', () => {});
    it('', () => {});
    
    describe('', () => {
        it('', () => {});
    });
});

You define multiple values for a specific test instead of creating multiple test functions with different values.

The .each modifier offers few different ways to define a table of the test cases.

const data = [
    { name: 'a', path: 'path/to/a', count: 1, write: true },
    { name: 'b', path: 'path/to/b', count: 3 },
];

test.each(data)('', ({name, path, count, write}) => {});
test.each([
    { a: 1, b: 1, expected: 2 },
    { a: 1, b: 2, expected: 3 },
    { a: 2, b: 1, expected: 3 },
])('.add($a, $b)', ({a, b, expected}) => {});

Run specific test

To run only specific test or block of tests use:

test.only('', () => {});
it.only('', () => {});

describe.only('', () => {});

Skip tests

To skip tests or block of tests, just append skip modifier right after test | it | describe.

test.skip('', () => {});
it.skip('', () => {});

describe.skip('', () => {});

Use the .concurrent modifier to run test concurrently.

You can't use this modifier in describe() blocks.

Is experimental, as for version 29.

For TypeScript install Jest with preprocessor with:

Use the configuration option to prevent Jest from executing more than the specified amount of tests at the same time.

ts-jest
Running
Test Structure
Run multiple values
To adjust the test description with the data it is using
Concurrent tests
maxConcurrency
Testing Angular Faster with Jest - XfiveXfive
Use it with Angular
Getting Started · Jest
Documentation
Logo
Logo