Unexpected is built on an extensible core. Every assertion, type and output style provided by the core library is implemented by extending the core. Plugins can make use of the exact same extension methods to provide new and exciting assertion capabilities.
Unexpected plugins are functions or objects that adhere to the following interface:
String- the name of the plugin.
String- the semver version of the plugin (string).
String array- a list of dependencies.
function(expect)- a function that will update the given expect instance.
If you pass a function to
use, it will be used as the
function, and the name of the function will be used as the name of the plugin,
unless the function is anonymous.
The name of the plugin should be the same as the NPM package name.
A plugin can require a list of other plugins to be installed prior to installation of the plugin. If the dependency list is not fulfilled the installation will fail. The idea is that you manage your plugin versions using NPM. If you install a plugin that is already installed, nothing will happen.
installInto function receives an instance of unexpected and uses
to extend the instance.
Notice that it is usually a good idea to clone the instance before extending it with plugins.
Let's say we wanted first class support for a integer intervals and provide as a plugin.
An integer interval is defined the following way:
Now we will define an example plugin that will add support for this type:
After installing the plugin we can use the
to contain assertion on
and we get improved output when the assertion fails:
For more inspiration you can look at the source for existing plugins. See the plugin page for a list.