Javascript - Jest Test Runner

About

Jest is a test runner from Facebook for client-side JavaScript applications and React applications specifically.

Jest ships with jsdom for DOM manipulation testing.

Installation

Jest and Typescript installation / configuration

Idea

See IDEA - Javascript Test with Jest

Usage

https://facebook.github.io/jest/docs/en/cli.html

Object

let oneObj = {name: 'nico', id: 2};
expect(oneObj).toEqual({name: 'nico'}) // false, should be exactly equal all Obj keys and values  
expect(oneObj).toMatchObject({name: 'nico'}) // true

Mock

Jest - Mock

Assertion

The assertion library is jasmine

ToThrow

test('Crawler Base Url Test', async () => {
        expect(() => {myFunction()}).toThrow(Error);
})

File Structure

Jest will look for test files with any of the following popular naming conventions:

  • Files with .js suffix in __tests__ folders.
  • Files with .test.js suffix.
  • Files with .spec.js suffix.

The .test.js / .spec.js files (or the __tests__ folders) can be located at any depth under the src top level folder.

It's recommended to collocate the test file with the source file (ie put the test files (or __tests__ folders) next to the code they are testing ) because of:

  • shorter relative imports (ie Source)
  • searching the test is easier

Run

Node

test is a global variable injected by Jest. You need to run your tests through jest

  • Test
test('3 equal 3', () => {
    expect(3).toBe(3);
});
  • Run
jest my-test --notify --config=config.json

Browser

experimental

- https://github.com/kvendrik/jest-lite

<script src="https://unpkg.com/[email protected]/dist/core.js"></script>
expect = jestLite.core.expect;

try {
   expect(3).toBe(2)
} catch (e) {
   // If the test is not good an exception is fired.
   console.log(e.message);
}

Documentation / Reference





Discover More
Welcome From Browser
How to develop, publish and use a javascript library ?

A step by step tutorial on how to create and publish a javascript library
How to resolve the SyntaxError: Cannot use import statement outside a module?

This article shows you how to resolve the syntax Error: Cannot use import statement outside a module.
Intellij Javascript Version Es6
IDEA - Javascript Test with Jest

Javascript Test with Jest in IDEA Source File: auth.js Test File: auth.test.js. Go to test file from source file: Ctrl+Shift+T or Navigate > Test The test file: name should have a .test.,...
Javascript Test - Runner

in Javascript. See also: Winner: Jest because: it's fully integrated in my IDE it supports React out of the box it can also test Typescript it forces you to not start a browser to test...
Jest - Mock

in Jest Mocking is just replacing a function by another called a mocking function. In Jest mocking function can replace the code executed but they also record all call made to them creating a command...
Node - NODE_PATH

NODE_PATH is: a process environment variable that contains a search path value (one or more directory with the linux or windows path separator) where the module loader (ie require statement /...
Card Puncher Data Processing
VsCode - Jest

Jest plugin in VsCode. Run on demand a la Idea () Run Config Keybindings example (from Idea) Run in two phases: upon starting, it will...



Share this page:
Follow us:
Task Runner