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 9e354f8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
26 changes: 15 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,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

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

Expand All @@ -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"]}',
Expand All @@ -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: [] });

Expand All @@ -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);
Expand Down
27 changes: 15 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,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

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

Expand All @@ -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"]}',
Expand All @@ -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: [] });

Expand All @@ -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);
Expand Down
9 changes: 4 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,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 });
Expand Down
9 changes: 4 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,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 });
Expand Down
15 changes: 5 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,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 });
Expand All @@ -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 });
Expand Down

0 comments on commit 9e354f8

Please sign in to comment.