Asserthat Plugin Extension - GaneshSPatil/assertthat GitHub Wiki

Welcome to the Assertthat Plugin API! Plugins, as the name implies, help in extending the functionality of the Assertthat testing framework. Assertthat publishes an extension endpoint for which plugins can be provided. An extension point published by the Assertthat - defines the interface and the lifecycle that governs the respective plugin.

API Endpoint Methods:

assert.addExtension:

addExtension can be used to add a new extension to the assert.that(actual) module.

Example:

const assert = require('asserthat');

assert.addExtension('is');

Above code snippet will add an is extension to assert.that(actual). The is extension can be used as follows:

assert.that(actual).is

assert.forExtension:

forExtension can be used to get handle to the previously added extension.

Example:

const assert = require('asserthat');

assert.addExtension('is');

assert.forExtension('is'); //will return the handled to the previously added `is` extension

Note: The handle to the added extension is necessary to add methods to the extension.


assert.addMethod:

addMethod can be used to add an extension method. addMethod can only be invoked on an extension.

Example:

const assert = require('asserthat');

assert.addExtension('is');
assert.forExtension('is').addMethod('false', (actual) => {
    return () => {
        if(actual !== false) {
            assert.fail('Expected %s to be false.', [actual]);
        }
    }
});

Above code snippet will add a false method on the is extension.

| IMPORTANT: addMethod function accept the actual value as an argument and is expected to return a function which performs assertion.

The false method on is extension can be used as follows:

assert.that(actual).is.false();

assert.fail:

fail method can be used to fail the assertion. fail is a custom assertthat method which will fail the assertion and prettyprint result.

Example:

const assert = require('asserthat');

assert.fail('Expected %s to be equal to %s.', [actual, expected]);

Note: The assert.fail method is exposed to provide consistent assertion failure messages across the tests. Plugin authors are free to use their own assertion failing techniques.


The Extension Nesting:

addExtension method can be used to add a custom extension to the assert.that(actual) module. For better readability of the tests, another child extension can be added to the already added extension.

const assert = require('assert');

assert.addExtension('is');
assert.forExtension('is').addExtension('a');
assert.forExtension('is').forExtension('a').addMethod('number', (actual) => isNumber);
Above code snippet:
  • will add is extension to assert.that(actual) module.
  • will add a extension to existing is extension.
  • will add number method to a extension.

The number method on nested is.a extension can be used as follows:

assert.that(1).is.a.number();