What is a test?

A test is code that throws an error when the actual result of something does not match the expected output.

const sum = (a, b) => a + b;
const subtract = (a, b) => a - b;

let result, expected;

result = sum(3, 7);
expected = 10;
if (result !== expected) {
  throw new Error(`${result} is not equal to ${expected}`);
}

result = subtract(7, 3);
expected = 4;
if (result !== expected) {
  throw new Error(`${result} is not equal to ${expected}`);
}

The actual !== expected part is called an assertion. The above custom tests code could be rewritten as a real test that could be run with Jest.

File: tests/test-introdution/base.test.js

const sum = (a, b) => a + b;
const subtract = (a, b) => a - b;

test("Should sum", () => {
  expect(sum(3, 7)).toEqual(10);
});
test("Should subtract", () => {
  expect(subtract(7, 3)).toEqual(4);
});

the output generated by Jest is the following:

 PASS  tests/test-introdution/base.test.js
  ✓ Should sum (1ms)
  ✓ Should subtract

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        0.08s, estimated 1s

If you'd like to run it on your local machine, follow the instructions of the guide.

Note that, usually, the testing frameworks allow you to write the test using the global test() or it() functions. The tests are usually grouped in describe (or context, again, it depends on the framework) functions

File: tests/test-introdution/describe.test.js

const sum = (a, b) => a + b;
const subtract = (a, b) => a - b;

describe("Math operations", () => {
  test("Should sum", () => {
    expect(sum(3, 7)).toEqual(10);
  });
  test("Should subtract", () => {
    expect(subtract(7, 3)).toEqual(4);
  });
});
 PASS  tests/test-introdution/describe.test.js
  Math operations
    ✓ Should sum (1ms)
    ✓ Should subtract

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        0.072s, estimated 1s

When you need to run only a particular test or skip some of them, you can call the test.only() of test.skip() utilities

File: tests/test-introdution/only.test.js

const sum = (a, b) => a + b;
const subtract = (a, b) => a - b;

describe("Math operations", () => {
  test.only("Should sum", () => {
    expect(sum(3, 7)).toEqual(10);
  });
  test("Should subtract", () => {
    expect(subtract(7, 3)).toEqual(4);
  });
});
 PASS  tests/test-introdution/only.test.js
  Math operations
    ✓ Should sum (2ms)
    ○ skipped Should subtract

Test Suites: 1 passed, 1 total
Tests:       1 skipped, 1 passed, 2 total
Snapshots:   0 total
Time:        0.066s, estimated 1s

File: tests/test-introdution/skip.test.js

const sum = (a, b) => a + b;
const subtract = (a, b) => a - b;

describe("Math operations", () => {
  test.skip("Should sum", () => {
    expect(sum(3, 7)).toEqual(10);
  });
  test("Should subtract", () => {
    expect(subtract(7, 3)).toEqual(4);
  });
});
 PASS  tests/test-introdution/skip.test.js
  Math operations
    ✓ Should subtract (1ms)
    ○ skipped Should sum

Test Suites: 1 passed, 1 total
Tests:       1 skipped, 1 passed, 2 total
Snapshots:   0 total
Time:        0.066s, estimated 1s

You can even run some common operations leveraging the before, after, beforeEach, and afterEach function hooks

File: tests/test-introdution/after-each.test.js

const sum = (a, b) => a + b;
const subtract = (a, b) => a - b;

describe("Math operations", () => {
  let i = 0;
  afterEach(() => {
    i++;
    console.log(`${i} tests run`);
  });
  test("Should sum", () => {
    expect(sum(3, 7)).toEqual(10);
  });
  test("Should subtract", () => {
    expect(subtract(7, 3)).toEqual(4);
  });
});
PASS  tests/test-introdution/after-each.test.js
  Math operations
    ✓ Should sum (1ms)
    ✓ Should subtract (1ms)

  console.log tests/test-introdution/after-each.test.js:8
    1 tests run

  console.log tests/test-introdution/after-each.test.js:8
    2 tests run

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        0.311s, estimated 1s

Author: Stefano Magni

results matching ""

    No results matching ""