Skip to content

Commit 2f10d24

Browse files
committed
feat: Add axe.utils.assert method
1 parent 2bef161 commit 2f10d24

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/core/utils/assert.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* global axe*/
2+
3+
/**
4+
* If the first argument is falsey, throw an error using the second argument as a message.
5+
* @param {boolean} bool
6+
* @param {string} message
7+
*/
8+
axe.utils.assert = function assert (bool, message) {
9+
if (!bool) {
10+
throw new Error(message)
11+
}
12+
}

test/core/utils/assert.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
describe('axe.utils.assert', function () {
2+
it('does nothing when passed a truthy value', function () {
3+
assert.doesNotThrow(function () {
4+
axe.utils.assert(true)
5+
axe.utils.assert('foo')
6+
axe.utils.assert(123)
7+
axe.utils.assert([])
8+
axe.utils.assert({})
9+
})
10+
})
11+
12+
it('throws an error when passed a falsey value', function () {
13+
assert.throws(function () {
14+
axe.utils.assert(false)
15+
})
16+
assert.throws(function () {
17+
axe.utils.assert(0)
18+
})
19+
assert.throws(function () {
20+
axe.utils.assert(null)
21+
})
22+
assert.throws(function () {
23+
axe.utils.assert(undefined)
24+
})
25+
})
26+
27+
it('sets second argument as the error message', function () {
28+
var message = 'Something went wrong'
29+
try {
30+
axe.utils.assert(false, message)
31+
} catch (e) {
32+
assert.instanceOf(e, Error)
33+
assert.equal(e.message, message)
34+
}
35+
})
36+
})

0 commit comments

Comments
 (0)