Custom Assertions - dylanparry/ceylon GitHub Wiki
Custom assertions can be written using assert
. You can import this into your code as follows:
// TypeScript or ES6
import { assert } from 'ceylon';
// CommonJS
const assert = require('ceylon').assert;
Once you have imported it, you can write a custom assertion as follows:
assert({
assertion: boolean,
message: string,
actual: T,
expected: T,
});
In the above code, assertion
is anything that will result in a boolean
value. For example, you could use 1 + 1 === 2
to test a simple addition.
The message
is the error message that should be thrown in case the test in the assertion
fails.
The actual
property is the item that you are testing, and the expected
property is what you expect the item to be. If these properties are supplied, the error thrown by assert
will also show a diff between the two items enabling you to see how the two items differ from each other.
Here is an example:
const actual = 2 + 2;
const expected = 5;
assert({
assertion: actual === expected,
message 'Expected 2 plus 2 to equal 5',
actual: actual,
expected: expected,
});
Which if tested using Mocha, would output something like:
AssertionError: Expected 2 plus 2 to equal 5 + expected - actual -4 +5