Add missing assert.error method
MODIFY
packages/ses/src/error/assert.js
packages/ses/src/error/assert.js
@@ -147,11 +147,21 @@ const getLogArgs = ({ template, args }) => {
const hiddenMessageLogArgs = new WeakMap();
/**
- * @param {HiddenDetails} hiddenDetails
- * @param {ErrorConstructor} ErrorConstructor
- * @returns {Error}
+ * @type {AssertMakeError}
*/
-const makeDetailedError = (hiddenDetails, ErrorConstructor) => {
+const makeError = (
+ optDetails = details`Assert failed`,
+ ErrorConstructor = Error,
+) => {
+ if (typeof optDetails === 'string') {
+ // If it is a string, use it as the literal part of the template so
+ // it doesn't get quoted.
+ optDetails = details([optDetails]);
+ }
+ const hiddenDetails = hiddenDetailsMap.get(optDetails);
+ if (hiddenDetails === undefined) {
+ throw new Error(`unrecognized details ${optDetails}`);
+ }
const messageString = getMessageString(hiddenDetails);
const error = new ErrorConstructor(messageString);
hiddenMessageLogArgs.set(error, getLogArgs(hiddenDetails));
@@ -163,6 +173,7 @@ const makeDetailedError = (hiddenDetails, ErrorConstructor) => {
// particularly fruitful place to put a breakpoint.
return error;
};
+freeze(makeError);
// /////////////////////////////////////////////////////////////////////////////
@@ -291,16 +302,7 @@ const makeAssert = (optRaise = undefined) => {
optDetails = details`Assert failed`,
ErrorConstructor = Error,
) => {
- if (typeof optDetails === 'string') {
- // If it is a string, use it as the literal part of the template so
- // it doesn't get quoted.
- optDetails = details([optDetails]);
- }
- const hiddenDetails = hiddenDetailsMap.get(optDetails);
- if (hiddenDetails === undefined) {
- throw new Error(`unrecognized details ${optDetails}`);
- }
- const error = makeDetailedError(hiddenDetails, ErrorConstructor);
+ const error = makeError(optDetails, ErrorConstructor);
if (optRaise !== undefined) {
optRaise(error);
}
@@ -359,6 +361,7 @@ const makeAssert = (optRaise = undefined) => {
// Note that "assert === baseAssert"
/** @type {Assert} */
const assert = assign(baseAssert, {
+ error: makeError,
fail,
equal,
typeof: assertTypeof,

MODIFY
packages/ses/src/error/types.js
packages/ses/src/error/types.js
* @returns {asserts flag}
*/
+/**
+ * @callback AssertMakeError
+ *
+ * The `assert.error` method, recording details for the console.
+ *
+ * The optional `optDetails` can be a string.
+ * @param {Details=} optDetails The details of what was asserted
+ * @param {ErrorConstructor=} ErrorConstructor An optional alternate error
+ * constructor to use.
+ * @returns {Error}
+ */
+
/**
* @callback AssertFail
*
*
* @typedef { BaseAssert & {
* typeof: AssertTypeof,
+ * error: AssertMakeError,
* fail: AssertFail,
* equal: AssertEqual,
* string: AssertString,

MODIFY
packages/ses/test/error/assert-log.test.js
packages/ses/test/error/assert-log.test.js
@@ -272,6 +272,64 @@ test('assert typeof', t => {
]);
});
+test('assert error default', t => {
+ const err = assert.error(d`<${'bar'},${q('baz')}>`);
+ t.is(err.message, '<(a string),"baz">');
+ t.is(err.name, 'Error');
+ throwsAndLogs(
+ t,
+ () => {
+ throw err;
+ },
+ /<\(a string\),"baz">/,
+ [['log', 'Caught', Error]],
+ );
+ throwsAndLogs(
+ t,
+ () => {
+ throw err;
+ },
+ /<\(a string\),"baz">/,
+ [
+ ['log', 'Caught', '(Error#1)'],
+ ['groupCollapsed', ''],
+ ['debug', 'Error#1:', '<', 'bar', ',', 'baz', '>'],
+ ['debug', '', 'stack of Error\n'],
+ ['groupEnd'],
+ ],
+ { wrapWithCausal: true },
+ );
+});
+
+test('assert error explicit', t => {
+ const err = assert.error(d`<${'bar'},${q('baz')}>`, URIError);
+ t.is(err.message, '<(a string),"baz">');
+ t.is(err.name, 'URIError');
+ throwsAndLogs(
+ t,
+ () => {
+ throw err;
+ },
+ /<\(a string\),"baz">/,
+ [['log', 'Caught', URIError]],
+ );
+ throwsAndLogs(
+ t,
+ () => {
+ throw err;
+ },
+ /<\(a string\),"baz">/,
+ [
+ ['log', 'Caught', '(URIError#1)'],
+ ['groupCollapsed', ''],
+ ['debug', 'URIError#1:', '<', 'bar', ',', 'baz', '>'],
+ ['debug', '', 'stack of URIError\n'],
+ ['groupEnd'],
+ ],
+ { wrapWithCausal: true },
+ );
+});
+
test('assert q', t => {
throwsAndLogs(
t,

Add missing assert.error method (#554)