assert: Add new feature throwsAndCatch, returns the original thrown error
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded

This commit is contained in:
Jonatan Nilsson 2023-08-28 06:25:22 +00:00
parent 1b697090aa
commit bd478e7753
4 changed files with 41 additions and 1 deletions

View file

@ -458,6 +458,15 @@ let err = await assert.isRejected(() => { throw new Error('hello') }) // ok
assert.strictEqual(err.message, 'hello')
```
### assert.throwsAndCatch(fn[, message])
Tests to make sure the function throws an exception. The important feature is this returns the original error that was thrown.
```javascript
let err = assert.throwsAndCatch(() => { throw new Error('hello world') }) // ok
assert.strictEqual(err.message, 'hello world')
```
# Sinon-like spy() stub()
Using sinon-inspired mechanics for spying on calls as well as being able

View file

@ -48,6 +48,15 @@ assert.notMatch = (value, test, message) => {
fail(m);
}
assert.throwsAndCatch = (fn, message) => {
let err = null
assert.throws(fn, function(error) {
err = error
return true
}, message)
return err
}
assert.isFulfilled = (promise, message) => {
return Promise.resolve(true)
.then(() => promise)

View file

@ -1,6 +1,6 @@
{
"name": "eltro",
"version": "1.3.3",
"version": "1.3.4",
"description": "Eltro is a tiny no-dependancy test framework for node",
"main": "index.mjs",
"scripts": {

View file

@ -29,6 +29,28 @@ t.describe('#notOk()', function() {
})
})
t.describe('#throwAndCatch()', function() {
t.test('should work and return the original error', function() {
const assertError = new Error('Speed')
let err = assert.throwsAndCatch(() => { throw assertError })
assert.strictEqual(err, assertError)
})
t.test('should otherwise throw if no error', function() {
const assertMessage = 'Hello world'
let error = null
try {
assert.throwsAndCatch(() => { }, assertMessage)
} catch (err) {
error = err
}
assert.ok(error)
assert.match(error.message, new RegExp(assertMessage))
})
})
t.describe('#isFulfilled()', function() {
t.test('should exist', function() {
assertExtended.ok(assertExtended.isFulfilled)