The package expect-promise
is a plugin to expect which adds words used
to manage promises.
You can install expect-promise
using LuaRocks with the command:
luarocks install expect-promise
The plugin is tested against the promise-lua, but it may be usable
with any module providing promises, as far as it respects the A+ specification
(no matter how the then
function is actually called), and the promise object has a modifiable metatable.
In order to use the plugin, you must declare it somewhere in your tests. A good place for this is a file
always read before executing the tests. For that, simply require the module, providing the expect
object as
parameter.
By default, the plugin expects your promise to have a next
method behaving like the then
method of the
promise A+ specification. If this is not the case, you must provide the name of the function to the
configuration option expect.parameters.promise.next
.
local expect = require('expect')
require('expect-promise')(expect)
expect.parameters.promise = {
next = 'thenCall'
}
Resolves the promise, replaces the target object with the result, and continues the assertion chain.
expect(Promise.resolve(42)).to.eventually.be.a('number').that.equals(42)
Asserts that the target promise is fulfilled.
expect(Promise.resolve(42)).to.be.fulfilled()
Asserts that the target promise is rejected.
expect(Promise.reject()).to.be.rejected()
Asserts that the target promise is rejected with the given error err
. If this argument is of type string,
the test is made using LUA function find
and a second boolean argument can be provided in order to consider
the err
as a plain string instead of a LUA pattern.
expect(Promise.reject('nooo')).to.be.rejectedWith('n.*o$')
expect(Promise.reject('nooo')).to.be.rejectedWith('no', true)