Plugins
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.
expect.use(pluginDefinition)
Unexpected plugins are functions or objects that adhere to the following interface:
Optional properties:
- name:
String
- the name of the plugin. - dependencies:
String array
- a list of dependencies.
Required:
- installInto:
function(expect)
- a function that will update the given expect instance.
If you pass a function to use
, it will be used as the installInto
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.
The installInto
function receives an instance of unexpected and uses
the addAssertion
, addStyle
, installTheme
and addType
methods
to extend the instance.
Notice that it is usually a good idea to clone the instance before extending it with plugins.
Example
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
IntegerInterval
instances.
and we get improved output when the assertion fails:
For more inspiration you can look at the source for one of the following plugins: