Skip to main content
Version: v3.x.x

Organizing Tests​

There are various motivations for organizing tests better:

  • Different tests usually have their own files.
  • Use isolated scopes to declare the same variables or isolate one test from another in the same file.
  • Grouping multiple tests of the same method.

Let's create two basic methods (sum and sub) to be tested:

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

By separating tests with different responsibilities​

Create a file to test the sum method and another for the sub method.

import { test, assert } from 'poku';
import { sum } from '../../src/calc.mjs';

test('Testing "sum" method', () => {
assert(sum(0, 0), 0, 'should return zero');
assert(sum(0, 1), 1, 'should return one');
assert(sum(1, 1), 2, 'should return two');
});

By categorizing tests with different responsibilities​

Create a unique file to test both the sum and sub methods.

import { test, assert } from 'poku';
import { sum, sub } from '../../src/calc.mjs';

test('Testing "sum" method', () => {
assert(sum(0, 0), 0, 'should return zero');
assert(sum(0, 1), 1, 'should return one');
assert(sum(1, 1), 2, 'should return two');
});

test('Testing "sub" method', () => {
assert(sub(1, 1), 0, 'should return zero');
assert(sub(2, 1), 1, 'should return one');
assert(sub(3, 1), 2, 'should return two');
});

By describing tests with different responsibilities​

Create a unique file to test both the sum and sub methods.

import { describe, it, assert } from 'poku';
import { sum, sub } from '../../src/calc.mjs';

describe('Testing calculation methods', () => {
it('"sum" method', () => {
assert(sum(0, 0), 0, 'should return zero');
assert(sum(0, 1), 1, 'should return one');
assert(sum(1, 1), 2, 'should return two');
});

it('"sub" method', () => {
assert(sub(1, 1), 0, 'should return zero');
assert(sub(2, 1), 1, 'should return one');
assert(sub(3, 1), 2, 'should return two');
});
});
  • When using describe + it with messages, it's common not to include the message in assert.

tip

You are free to choose to use describe + it, or test + describe, or test + it and so on, but note that if you use messages, they will only be properly formatted for:

  • assert
  • test
  • test + assert
  • it
  • it + assert
  • describe
  • describe + assert
  • describe + it
  • describe + it + assert
  • describe + assert + it + assert
  • describe + test
  • describe + test + assert
  • describe + assert + test + assert
Be careful

Avoid coupling test and it when using hooks such as beforeEach (e.g., test + test, it + it, test + it, etc.).


note

If you find any typos, feel free to open a Pull Request correcting them.