Skip to main content
Version: v2.x.x

Assertions​

β˜‘οΈ What are Assertions?​

In testing, assertions are used to ensure that a result is really what we expect.

tip

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)


note

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