2020-04-28 19:38:34 +00:00
|
|
|
const { expect } = require('chai')
|
2018-03-07 16:42:39 +00:00
|
|
|
|
2020-04-28 19:38:34 +00:00
|
|
|
const OError = require('../..')
|
2019-07-01 10:34:15 +00:00
|
|
|
|
2020-09-11 09:05:54 +00:00
|
|
|
/**
|
|
|
|
* @param {Error} e
|
|
|
|
* @param {any} expected
|
|
|
|
*/
|
2020-04-17 07:44:50 +00:00
|
|
|
exports.expectError = function OErrorExpectError(e, expected) {
|
2020-05-15 09:32:18 +00:00
|
|
|
expect(
|
|
|
|
e.name,
|
|
|
|
"error should set the name property to the error's name"
|
|
|
|
).to.equal(expected.name)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
e instanceof expected.klass,
|
|
|
|
'error should be an instance of the error type'
|
|
|
|
).to.be.true
|
|
|
|
|
|
|
|
expect(
|
|
|
|
e instanceof Error,
|
|
|
|
'error should be an instance of the built-in Error type'
|
|
|
|
).to.be.true
|
|
|
|
|
|
|
|
expect(
|
|
|
|
require('util').types.isNativeError(e),
|
|
|
|
'error should be recognised by util.types.isNativeError'
|
|
|
|
).to.be.true
|
|
|
|
|
|
|
|
expect(
|
|
|
|
e.toString(),
|
|
|
|
'toString should return the default error message formatting'
|
|
|
|
).to.equal(expected.message)
|
|
|
|
|
2020-09-11 09:05:54 +00:00
|
|
|
expect(e.stack, 'error should have a stack trace').to.not.be.empty
|
|
|
|
|
2020-05-15 09:32:18 +00:00
|
|
|
expect(
|
2020-09-11 09:05:54 +00:00
|
|
|
/** @type {string} */ (e.stack).split('\n')[0],
|
2020-05-15 09:32:18 +00:00
|
|
|
'stack should start with the default error message formatting'
|
|
|
|
).to.match(new RegExp(`^${expected.name}:`))
|
|
|
|
|
|
|
|
expect(
|
2020-09-11 09:05:54 +00:00
|
|
|
/** @type {string} */ (e.stack).split('\n')[1],
|
2020-05-15 09:32:18 +00:00
|
|
|
'first stack frame should be the function where the error was thrown'
|
|
|
|
).to.match(expected.firstFrameRx)
|
2019-07-01 10:34:15 +00:00
|
|
|
}
|
2020-04-28 19:40:20 +00:00
|
|
|
|
2020-09-11 09:05:54 +00:00
|
|
|
/**
|
|
|
|
* @param {Error} error
|
|
|
|
* @param {String[]} expected
|
|
|
|
*/
|
2020-04-28 19:40:20 +00:00
|
|
|
exports.expectFullStackWithoutStackFramesToEqual = function (error, expected) {
|
|
|
|
const fullStack = OError.getFullStack(error)
|
|
|
|
const fullStackWithoutFrames = fullStack
|
|
|
|
.split('\n')
|
2021-12-16 09:04:32 +00:00
|
|
|
.filter(line => !/^\s+at\s/.test(line))
|
2020-05-15 09:32:18 +00:00
|
|
|
expect(
|
|
|
|
fullStackWithoutFrames,
|
|
|
|
'full stack without frames should equal'
|
|
|
|
).to.deep.equal(expected)
|
2020-04-28 19:40:20 +00:00
|
|
|
}
|