muggle-assert
A simplification of node's assert for use with muggle
Goals
- Predictable and simple behavior
- Simple and readable source code
- Intuitive and small API
- Encourage writing robust and readable tests
- Fully tested
Installation
$ npm install muggle-assert
Usage
Failing assertions throw an error. Passing assertions do nothing.
const assert = assert assert await assert
API
AssertionError
new assert.AssertionError(args)
When an assertion fails, it throws an AssertionError
{ const assertError = message: 'should be a penguin' expected: 'penguin' actual: 'polar bear' operator: 'animal' stackStartFn: outerFunction assert assert assert assert assert // strips stackStartFn and above from the stack trace }
Assert
assert(expression, description)
Assert that an expression is truthy. Throws an AssertionError if expression
is falsy.
The AssertionError has no useful information by default. I recommended always including a description using the word "should" to make the error output readable and useful.
const tux = 'Tux'trycatch assertErrorassertassert
Equal
assert.equal(actual, expected, description = 'should be equal')
Assert that the two values are equivalent.
muggle-deep-equal is used to determine equality. It uses ===
to compare values, and recursively compares the values of objects and arrays. Its behavior is detailed in its readme.
The AssertionError provides actual
and expected
properties that usually give a good idea of why the assertion failed. It's still often a good idea to add a custom description using "should", especially to differentiate multiple assert.equal()
within the same muggle test.
assert assert assert try assert catch assertError assert assert assert assert
Throws
assert.throws(fn, [expectedError], description = 'should throw error')
Assert that a function will throw an error. If fn()
finishes execution without throwing an error, then the assertion fails.
expectedError
, if present, is the error that should be thrown from the function. If an error is thrown but it doesn't equal expectedError
, then the assertion fails.
muggle-deep-equal is used to compare the thrown error to expectedError
. This means the error is compared as expected using toString()
.
assert assert // expectedError is optionalassert try assert catch assertError assert assert assert assert
Rejects
async assert.rejects(promise, [expectedError], description = 'promise should reject')
Assert that a promise is rejected or will reject.
If promise
resolves, then the assertion fails. assert.rejects()
is an async function, so it returns a rejected promise with an AssertionError as the reason instead of throwing on failure.
expectedError
, if present, is the error that the promise should reject with. If the promise rejects with an error that doesn't equal expectedError
, then the assertion fails.
muggle-deep-equal is used to compare the rejection reason to expectedError
. This means errors are compared as expected using toString()
.
await assert await assert // expectedError is optionalawait assert { await throw } // async functions return promisesawait assert try await assert catch assertError assert assert assert assert