assertErr
This commit is contained in:
parent
708d44f121
commit
cbf48ff3b1
1 changed files with 18 additions and 0 deletions
18
src/index.js
18
src/index.js
|
@ -34,6 +34,24 @@ export const assertEq = (a, b, message = `assertion error: ${a} !== ${b}`) => {
|
||||||
throw new AssertionError(message)
|
throw new AssertionError(message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const test = (re, str = '') => re instanceof RegExp ? re.test(str) : new RegExp(re).test(str)
|
||||||
|
|
||||||
|
export const assertErr = (f, err) => {
|
||||||
|
try {
|
||||||
|
f()
|
||||||
|
} catch (e) {
|
||||||
|
if (typeof err === 'string' || err instanceof RegExp) {
|
||||||
|
assert(test(err, e.message), `${err} did not match ${e.message}`)
|
||||||
|
} else if (typeof err === 'function') {
|
||||||
|
assert(e.constructor === err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const expected = err ? `"${err}" ` : ""
|
||||||
|
assert(false, `expecting error ${expected}but function did not throw`)
|
||||||
|
}
|
||||||
|
|
||||||
export const assertCallback = () => {
|
export const assertCallback = () => {
|
||||||
let result = false
|
let result = false
|
||||||
|
|
Loading…
Add table
Reference in a new issue