From 9e354f8fd774c2b405009ad4b957c6d1cf538b7f Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Thu, 22 Feb 2024 11:49:01 -0500 Subject: [PATCH] test(marshal,ses): Prefer test.skip over conditional early return Ref #2042 Ref #550 --- packages/marshal/test/test-marshal-capdata.js | 26 ++++++++++-------- .../marshal/test/test-marshal-smallcaps.js | 27 ++++++++++--------- .../test-aggregate-error-console-demo.js | 9 +++---- .../error/test-aggregate-error-console.js | 9 +++---- .../ses/test/error/test-aggregate-error.js | 15 ++++------- 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/packages/marshal/test/test-marshal-capdata.js b/packages/marshal/test/test-marshal-capdata.js index 10712862a3..c4e0cf5081 100644 --- a/packages/marshal/test/test-marshal-capdata.js +++ b/packages/marshal/test/test-marshal-capdata.js @@ -18,6 +18,13 @@ const harden = /** @type {import('ses').Harden & { isFake?: boolean }} */ ( global.harden ); +// Unknown error names decode as generic Errors. +const supportsAggregateError = typeof AggregateError !== 'undefined'; +const decodedAggregateErrorCtor = supportsAggregateError + ? AggregateError + : Error; +const testIfAggregateError = supportsAggregateError ? test : test.skip; + // this only includes the tests that do not use liveSlots /** @@ -154,10 +161,6 @@ test('unserialize errors', t => { }); test('unserialize extended errors', t => { - if (typeof AggregateError === 'undefined') { - t.pass('skip test on platforms prior to AggregateError'); - return; - } const { unserialize } = makeTestMarshal(); const uns = body => unserialize({ body, slots: [] }); @@ -172,10 +175,14 @@ test('unserialize extended errors', t => { const aggErr = uns( '{"@qclass":"error","message":"msg","name":"AggregateError","extraProp":"foo","cause":"bar","errors":["zip","zap"]}', ); - t.is(getPrototypeOf(aggErr), AggregateError.prototype); // direct instance of + t.is(getPrototypeOf(aggErr), decodedAggregateErrorCtor.prototype); // direct instance of t.false('extraProp' in aggErr); t.false('cause' in aggErr); - t.is(aggErr.errors.length, 0); + if (supportsAggregateError) { + t.is(aggErr.errors.length, 0); + } else { + t.false('errors' in aggErr); + } const unkErr = uns( '{"@qclass":"error","message":"msg","name":"UnknownError","extraProp":"foo","cause":"bar","errors":["zip","zap"]}', @@ -186,10 +193,7 @@ test('unserialize extended errors', t => { t.false('errors' in unkErr); }); -const testIfAggregateError = - typeof AggregateError !== 'undefined' ? test : test.skip; - -testIfAggregateError('unserialize errors w recognized extensions', t => { +testIfAggregateError('unserialize recognized error extensions', t => { const { unserialize } = makeTestMarshal(); const uns = body => unserialize({ body, slots: [] }); @@ -206,7 +210,7 @@ testIfAggregateError('unserialize errors w recognized extensions', t => { const aggErr = uns( `{"@qclass":"error","message":"msg","name":"AggregateError","extraProp":"foo","cause":${errEnc},"errors":[${errEnc}]}`, ); - t.is(getPrototypeOf(aggErr), AggregateError.prototype); // direct instance of + t.is(getPrototypeOf(aggErr), decodedAggregateErrorCtor.prototype); // direct instance of t.false('extraProp' in aggErr); t.is(getPrototypeOf(aggErr.cause), URIError.prototype); t.is(getPrototypeOf(aggErr.errors[0]), URIError.prototype); diff --git a/packages/marshal/test/test-marshal-smallcaps.js b/packages/marshal/test/test-marshal-smallcaps.js index 917d7e03d0..c8d9aff03c 100644 --- a/packages/marshal/test/test-marshal-smallcaps.js +++ b/packages/marshal/test/test-marshal-smallcaps.js @@ -19,6 +19,13 @@ const harden = /** @type {import('ses').Harden & { isFake?: boolean }} */ ( global.harden ); +// Unknown error names decode as generic Errors. +const supportsAggregateError = typeof AggregateError !== 'undefined'; +const decodedAggregateErrorCtor = supportsAggregateError + ? AggregateError + : Error; +const testIfAggregateError = supportsAggregateError ? test : test.skip; + // this only includes the tests that do not use liveSlots /** @@ -160,10 +167,6 @@ test('smallcaps unserialize errors', t => { }); test('smallcaps unserialize extended errors', t => { - if (typeof AggregateError === 'undefined') { - t.pass('skip test on platforms prior to AggregateError'); - return; - } const { unserialize } = makeTestMarshal(); const uns = body => unserialize({ body, slots: [] }); @@ -178,10 +181,14 @@ test('smallcaps unserialize extended errors', t => { const aggErr = uns( '#{"#error":"msg","name":"AggregateError","extraProp":"foo","cause":"bar","errors":["zip","zap"]}', ); - t.is(getPrototypeOf(aggErr), AggregateError.prototype); // direct instance of + t.is(getPrototypeOf(aggErr), decodedAggregateErrorCtor.prototype); // direct instance of t.false('extraProp' in aggErr); t.false('cause' in aggErr); - t.is(aggErr.errors.length, 0); + if (supportsAggregateError) { + t.is(aggErr.errors.length, 0); + } else { + t.false('errors' in aggErr); + } const unkErr = uns( '#{"#error":"msg","name":"UnknownError","extraProp":"foo","cause":"bar","errors":["zip","zap"]}', @@ -192,11 +199,7 @@ test('smallcaps unserialize extended errors', t => { t.false('errors' in unkErr); }); -test('smallcaps unserialize errors w recognized extensions', t => { - if (typeof AggregateError === 'undefined') { - t.pass('skip test on platforms prior to AggregateError'); - return; - } +testIfAggregateError('smallcaps unserialize recognized error extensions', t => { const { unserialize } = makeTestMarshal(); const uns = body => unserialize({ body, slots: [] }); @@ -213,7 +216,7 @@ test('smallcaps unserialize errors w recognized extensions', t => { const aggErr = uns( `#{"#error":"msg","name":"AggregateError","extraProp":"foo","cause":${errEnc},"errors":[${errEnc}]}`, ); - t.is(getPrototypeOf(aggErr), AggregateError.prototype); // direct instance of + t.is(getPrototypeOf(aggErr), decodedAggregateErrorCtor.prototype); // direct instance of t.false('extraProp' in aggErr); t.is(getPrototypeOf(refErr.cause), URIError.prototype); t.is(getPrototypeOf(refErr.errors[0]), URIError.prototype); diff --git a/packages/ses/test/error/test-aggregate-error-console-demo.js b/packages/ses/test/error/test-aggregate-error-console-demo.js index ba2da6ae16..f6197f4b61 100644 --- a/packages/ses/test/error/test-aggregate-error-console-demo.js +++ b/packages/ses/test/error/test-aggregate-error-console-demo.js @@ -9,11 +9,10 @@ import '../../index.js'; lockdown(); -test('aggregate error console demo', t => { - if (typeof AggregateError === 'undefined') { - t.pass('skip test on platforms prior to AggregateError'); - return; - } +const testIfAggregateError = + typeof AggregateError !== 'undefined' ? test : test.skip; + +testIfAggregateError('aggregate error console demo', t => { const e3 = Error('e3'); const e2 = Error('e2', { cause: e3 }); const u4 = URIError('u4', { cause: e2 }); diff --git a/packages/ses/test/error/test-aggregate-error-console.js b/packages/ses/test/error/test-aggregate-error-console.js index ba192546be..9ba53b151a 100644 --- a/packages/ses/test/error/test-aggregate-error-console.js +++ b/packages/ses/test/error/test-aggregate-error-console.js @@ -4,11 +4,10 @@ import { throwsAndLogs } from './throws-and-logs.js'; lockdown(); -test('aggregate error console', t => { - if (typeof AggregateError === 'undefined') { - t.pass('skip test on platforms prior to AggregateError'); - return; - } +const testIfAggregateError = + typeof AggregateError !== 'undefined' ? test : test.skip; + +testIfAggregateError('aggregate error console', t => { const e3 = Error('e3'); const e2 = Error('e2', { cause: e3 }); const u4 = URIError('u4', { cause: e2 }); diff --git a/packages/ses/test/error/test-aggregate-error.js b/packages/ses/test/error/test-aggregate-error.js index 5c0b0b2466..bcfec1fb5e 100644 --- a/packages/ses/test/error/test-aggregate-error.js +++ b/packages/ses/test/error/test-aggregate-error.js @@ -5,11 +5,10 @@ const { getOwnPropertyDescriptor } = Object; lockdown(); -test('aggregate error', t => { - if (typeof AggregateError === 'undefined') { - t.pass('skip test on platforms prior to AggregateError'); - return; - } +const testIfAggregateError = + typeof AggregateError !== 'undefined' ? test : test.skip; + +testIfAggregateError('aggregate error', t => { const e1 = Error('e1'); const e2 = Error('e2', { cause: e1 }); const u3 = URIError('u3', { cause: e1 }); @@ -31,11 +30,7 @@ test('aggregate error', t => { }); }); -test('Promise.any aggregate error', async t => { - if (typeof AggregateError === 'undefined') { - t.pass('skip test on platforms prior to AggregateError'); - return; - } +testIfAggregateError('Promise.any aggregate error', async t => { const e1 = Error('e1'); const e2 = Error('e2', { cause: e1 }); const u3 = URIError('u3', { cause: e1 });