Skip to content

Commit

Permalink
feat: Add axe.utils.assert method
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoFiers committed Aug 30, 2019
1 parent 2bef161 commit 2f10d24
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/core/utils/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* global axe*/

/**
* If the first argument is falsey, throw an error using the second argument as a message.
* @param {boolean} bool
* @param {string} message
*/
axe.utils.assert = function assert (bool, message) {
if (!bool) {
throw new Error(message)
}
}
36 changes: 36 additions & 0 deletions test/core/utils/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
describe('axe.utils.assert', function () {
it('does nothing when passed a truthy value', function () {
assert.doesNotThrow(function () {
axe.utils.assert(true)
axe.utils.assert('foo')
axe.utils.assert(123)
axe.utils.assert([])
axe.utils.assert({})
})
})

it('throws an error when passed a falsey value', function () {
assert.throws(function () {
axe.utils.assert(false)
})
assert.throws(function () {
axe.utils.assert(0)
})
assert.throws(function () {
axe.utils.assert(null)
})
assert.throws(function () {
axe.utils.assert(undefined)
})
})

it('sets second argument as the error message', function () {
var message = 'Something went wrong'
try {
axe.utils.assert(false, message)
} catch (e) {
assert.instanceOf(e, Error)
assert.equal(e.message, message)
}
})
})

0 comments on commit 2f10d24

Please sign in to comment.