Skip to content

Commit

Permalink
test(marshal,ses): Prefer test.skip over conditional early return
Browse files Browse the repository at this point in the history
Ref #2042
Ref #550
  • Loading branch information
gibson042 committed Feb 22, 2024
1 parent 18fb8c0 commit a98519a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 43 deletions.
27 changes: 16 additions & 11 deletions packages/marshal/test/test-marshal-capdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ const harden = /** @type {import('ses').Harden & { isFake?: boolean }} */ (
global.harden
);

// Unknown error names decode as generic Errors.
// TODO: Remove after dropping support for pre-AggregateError implementations.
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

/**
Expand Down Expand Up @@ -154,10 +162,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: [] });

Expand All @@ -172,10 +176,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"]}',
Expand All @@ -186,10 +194,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: [] });

Expand All @@ -206,7 +211,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);
Expand Down
28 changes: 16 additions & 12 deletions packages/marshal/test/test-marshal-smallcaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ const harden = /** @type {import('ses').Harden & { isFake?: boolean }} */ (
global.harden
);

// Unknown error names decode as generic Errors.
// TODO: Remove after dropping support for pre-AggregateError implementations.
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

/**
Expand Down Expand Up @@ -160,10 +168,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: [] });

Expand All @@ -178,10 +182,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"]}',
Expand All @@ -192,11 +200,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: [] });

Expand All @@ -213,7 +217,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);
Expand Down
10 changes: 5 additions & 5 deletions packages/ses/test/error/test-aggregate-error-console-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import '../../index.js';

lockdown();

test('aggregate error console demo', t => {
if (typeof AggregateError === 'undefined') {
t.pass('skip test on platforms prior to AggregateError');
return;
}
// TODO: Remove after dropping support for pre-AggregateError implementations.
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 });
Expand Down
10 changes: 5 additions & 5 deletions packages/ses/test/error/test-aggregate-error-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ 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;
}
// TODO: Remove after dropping support for pre-AggregateError implementations.
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 });
Expand Down
16 changes: 6 additions & 10 deletions packages/ses/test/error/test-aggregate-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const { getOwnPropertyDescriptor } = Object;

lockdown();

test('aggregate error', t => {
if (typeof AggregateError === 'undefined') {
t.pass('skip test on platforms prior to AggregateError');
return;
}
// TODO: Remove after dropping support for pre-AggregateError implementations.
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 });
Expand All @@ -31,11 +31,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 });
Expand Down

0 comments on commit a98519a

Please sign in to comment.