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:
- ./src/calc.mjs
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
summethod and another for thesubmethod.
- ./test/unit/sum.test.mjs
- ./test/unit/sub.test.mjs
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');
});
import { test, assert } from 'poku';
import { sub } from '../../src/calc.mjs';
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 categorizing tests with different responsibilitiesβ
Create a unique file to test both the
sumandsubmethods.
- ./test/unit/calc.test.mjs
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
sumandsubmethods.
- ./test/unit/calc.test.mjs
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+itwith messages, it's common not to include the message inassert.
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:
asserttesttest+assertitit+assertdescribedescribe+assertdescribe+itdescribe+it+assertdescribe+assert+it+assertdescribe+testdescribe+test+assertdescribe+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.