This is the test code:
chai.assert.throws(() => {
host1.add()
}, TriedAddingDuplicateError)
As you can see, error have been thrown, Mocha got it, but the test is marked as failed anyway:
AssertionError: expected [Function] to throw 'TriedAddingDuplicateError' but
'TriedAddingDuplicateError' was thrown
chai.expect(() => host1.add()).to.throw(TriedAddingDuplicateError)
, but the result is the sameDo you know how to fix it?
I was missing to instanciate the error class in the function:
host.js before
add () {
let database = db.read()
const hostToAdd = Host.findByHost(this.host)
if (Validation.IsEmptyObject(hostToAdd)) {
database.hosts.push(this)
db.write(database)
return this
}
throw TriedAddingDuplicateError
}
host.js now
add () {
let database = db.read()
const hostToAdd = Host.findByHost(this.host)
if (Validation.IsEmptyObject(hostToAdd)) {
database.hosts.push(this)
db.write(database)
return this
}
throw new TriedAddingDuplicateError()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.