Assertionsβ
βοΈ What are Assertions?β
In testing, assertions are used to ensure that a result is really what we expect.
Each tester may have a different way of doing this, but the end is usually the same:
- If our check (assertion) is not exactly as expected, the test will trigger an error on that assertion.
In Practiceβ
Understanding conditions and assertionsβ
In JavaScript, we can compare using ===
, right?
If the condition is valid, it continues, else it will try another condition if it exists. For example:
const actual = 'apple';
const expected = 'banana';
if (actual === expected) {
console.log('The fruits are the same');
} else {
console.log('The fruits are different');
}
console.log(
'It will be executed after the bellow conditions, independent of the result'
);
- βοΈ With assertions, imagine that if the condition is not valid, the script would be aborted immediately with an error.
Let's create a assertion-error.test.mjs
file and try the same, but as a test assertion:
import { assert } from 'poku';
const actual = 'apple';
const expected = 'banana';
assert.strictEqual(actual, expected, 'Expects for the same fruits');
// After assert error, the script will exit the process with an error β
console.log('Nobody notices me π');
And now, the assertion-success.test.mjs
file:
import { assert } from 'poku';
const actual = 'apple';
const expected = 'apple';
assert.strictEqual(actual, expected, 'Expects for the same fruits');
// After assert success, the script will continue normally β
console.log("I'm here π");
Our tests have been created:
- One will cause an error and the other will be completed successfully.
To run all the tests at once, just run the command npx poku --debug
in the terminal and see the magic happen:
npx poku --debug
Why
--debug
?For beginners, it is always recommended to use this option to see all terminal output. Without this option, no
console.log
will be displayed in the terminal, for example.
Conclusionβ
In a real test, we wouldn't use obvious/dummy values, we would test real methods and results that may vary in our project, be it frontend or backend.
When all the tests pass, it means that our suite has successfully passed all the tests created π
Extraβ
Here's a complete step-by-step guide to learning unit testing in practice with Poku, in Portuguese (BR):
π§π· Tutorial: Introduzindo Testes UnitΓ‘rios para Devs Iniciantes (JS)
If you find any typos, feel free to open a Pull Request correcting them.