The assert
object is an instance of chaijs/assert which is passed to each test as a property on test context.
To make your tests more reliable, you can also plan assertions to be executed for a given test. Let’s consider this example.
test('must throw exception', async ({ assert }) => {
try {
await badOperation()
} catch ({ message }) {
assert.equal(message, 'Some error message')
}
})
The above test passes even if an exception was never thrown and no assertions were run. Which means it is a bad test, which is passed because we structured it badly.
To overcome this situation, you must plan some assertions, to make sure the catch
block is always executed and an assertion has been made.
test('must throw exception', async ({ assert }) => {
assert.plan(1)
try {
await badOperation()
} catch ({ message }) {
assert.equal(message, 'Some error message')
}
})
This time, if badOperation
doesn’t throw an exception, the test still fails since we planned for 1
assertion and 0
were made.