Skip to content

Commit b350aaa

Browse files
authored
test(marshal,ses): Prefer test.skip over conditional early return (merge #2099)
2 parents 3f9b909 + f33719d commit b350aaa

5 files changed

+48
-43
lines changed

packages/marshal/test/marshal-capdata.test.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ const harden = /** @type {import('ses').Harden & { isFake?: boolean }} */ (
1717
global.harden
1818
);
1919

20+
// Unknown error names decode as generic Errors.
21+
// TODO: Remove after dropping support for pre-AggregateError implementations.
22+
const supportsAggregateError = typeof AggregateError !== 'undefined';
23+
const decodedAggregateErrorCtor = supportsAggregateError
24+
? AggregateError
25+
: Error;
26+
const testIfAggregateError = supportsAggregateError ? test : test.skip;
27+
2028
// this only includes the tests that do not use liveSlots
2129

2230
/**
@@ -153,10 +161,6 @@ test('unserialize errors', t => {
153161
});
154162

155163
test('unserialize extended errors', t => {
156-
if (typeof AggregateError === 'undefined') {
157-
t.pass('skip test on platforms prior to AggregateError');
158-
return;
159-
}
160164
const { unserialize } = makeTestMarshal();
161165
const uns = body => unserialize({ body, slots: [] });
162166

@@ -171,10 +175,14 @@ test('unserialize extended errors', t => {
171175
const aggErr = uns(
172176
'{"@qclass":"error","message":"msg","name":"AggregateError","extraProp":"foo","cause":"bar","errors":["zip","zap"]}',
173177
);
174-
t.is(getPrototypeOf(aggErr), AggregateError.prototype); // direct instance of
178+
t.is(getPrototypeOf(aggErr), decodedAggregateErrorCtor.prototype); // direct instance of
175179
t.false('extraProp' in aggErr);
176180
t.false('cause' in aggErr);
177-
t.is(aggErr.errors.length, 0);
181+
if (supportsAggregateError) {
182+
t.is(aggErr.errors.length, 0);
183+
} else {
184+
t.false('errors' in aggErr);
185+
}
178186

179187
const unkErr = uns(
180188
'{"@qclass":"error","message":"msg","name":"UnknownError","extraProp":"foo","cause":"bar","errors":["zip","zap"]}',
@@ -185,10 +193,7 @@ test('unserialize extended errors', t => {
185193
t.false('errors' in unkErr);
186194
});
187195

188-
const testIfAggregateError =
189-
typeof AggregateError !== 'undefined' ? test : test.skip;
190-
191-
testIfAggregateError('unserialize errors w recognized extensions', t => {
196+
testIfAggregateError('unserialize recognized error extensions', t => {
192197
const { unserialize } = makeTestMarshal();
193198
const uns = body => unserialize({ body, slots: [] });
194199

@@ -205,7 +210,7 @@ testIfAggregateError('unserialize errors w recognized extensions', t => {
205210
const aggErr = uns(
206211
`{"@qclass":"error","message":"msg","name":"AggregateError","extraProp":"foo","cause":${errEnc},"errors":[${errEnc}]}`,
207212
);
208-
t.is(getPrototypeOf(aggErr), AggregateError.prototype); // direct instance of
213+
t.is(getPrototypeOf(aggErr), decodedAggregateErrorCtor.prototype); // direct instance of
209214
t.false('extraProp' in aggErr);
210215
t.is(getPrototypeOf(aggErr.cause), URIError.prototype);
211216
t.is(getPrototypeOf(aggErr.errors[0]), URIError.prototype);

packages/marshal/test/marshal-smallcaps.test.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ const harden = /** @type {import('ses').Harden & { isFake?: boolean }} */ (
1818
global.harden
1919
);
2020

21+
// Unknown error names decode as generic Errors.
22+
// TODO: Remove after dropping support for pre-AggregateError implementations.
23+
const supportsAggregateError = typeof AggregateError !== 'undefined';
24+
const decodedAggregateErrorCtor = supportsAggregateError
25+
? AggregateError
26+
: Error;
27+
const testIfAggregateError = supportsAggregateError ? test : test.skip;
28+
2129
// this only includes the tests that do not use liveSlots
2230

2331
/**
@@ -159,10 +167,6 @@ test('smallcaps unserialize errors', t => {
159167
});
160168

161169
test('smallcaps unserialize extended errors', t => {
162-
if (typeof AggregateError === 'undefined') {
163-
t.pass('skip test on platforms prior to AggregateError');
164-
return;
165-
}
166170
const { unserialize } = makeTestMarshal();
167171
const uns = body => unserialize({ body, slots: [] });
168172

@@ -177,10 +181,14 @@ test('smallcaps unserialize extended errors', t => {
177181
const aggErr = uns(
178182
'#{"#error":"msg","name":"AggregateError","extraProp":"foo","cause":"bar","errors":["zip","zap"]}',
179183
);
180-
t.is(getPrototypeOf(aggErr), AggregateError.prototype); // direct instance of
184+
t.is(getPrototypeOf(aggErr), decodedAggregateErrorCtor.prototype); // direct instance of
181185
t.false('extraProp' in aggErr);
182186
t.false('cause' in aggErr);
183-
t.is(aggErr.errors.length, 0);
187+
if (supportsAggregateError) {
188+
t.is(aggErr.errors.length, 0);
189+
} else {
190+
t.false('errors' in aggErr);
191+
}
184192

185193
const unkErr = uns(
186194
'#{"#error":"msg","name":"UnknownError","extraProp":"foo","cause":"bar","errors":["zip","zap"]}',
@@ -191,11 +199,7 @@ test('smallcaps unserialize extended errors', t => {
191199
t.false('errors' in unkErr);
192200
});
193201

194-
test('smallcaps unserialize errors w recognized extensions', t => {
195-
if (typeof AggregateError === 'undefined') {
196-
t.pass('skip test on platforms prior to AggregateError');
197-
return;
198-
}
202+
testIfAggregateError('smallcaps unserialize recognized error extensions', t => {
199203
const { unserialize } = makeTestMarshal();
200204
const uns = body => unserialize({ body, slots: [] });
201205

@@ -212,7 +216,7 @@ test('smallcaps unserialize errors w recognized extensions', t => {
212216
const aggErr = uns(
213217
`#{"#error":"msg","name":"AggregateError","extraProp":"foo","cause":${errEnc},"errors":[${errEnc}]}`,
214218
);
215-
t.is(getPrototypeOf(aggErr), AggregateError.prototype); // direct instance of
219+
t.is(getPrototypeOf(aggErr), decodedAggregateErrorCtor.prototype); // direct instance of
216220
t.false('extraProp' in aggErr);
217221
t.is(getPrototypeOf(refErr.cause), URIError.prototype);
218222
t.is(getPrototypeOf(refErr.errors[0]), URIError.prototype);

packages/ses/test/error/aggregate-error-console-demo.test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import '../../index.js';
99

1010
lockdown();
1111

12-
test('aggregate error console demo', t => {
13-
if (typeof AggregateError === 'undefined') {
14-
t.pass('skip test on platforms prior to AggregateError');
15-
return;
16-
}
12+
// TODO: Remove after dropping support for pre-AggregateError implementations.
13+
const testIfAggregateError =
14+
typeof AggregateError !== 'undefined' ? test : test.skip;
15+
16+
testIfAggregateError('aggregate error console demo', t => {
1717
const e3 = Error('e3');
1818
const e2 = Error('e2', { cause: e3 });
1919
const u4 = URIError('u4', { cause: e2 });

packages/ses/test/error/aggregate-error-console.test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { throwsAndLogs } from './_throws-and-logs.js';
44

55
lockdown();
66

7-
test('aggregate error console', t => {
8-
if (typeof AggregateError === 'undefined') {
9-
t.pass('skip test on platforms prior to AggregateError');
10-
return;
11-
}
7+
// TODO: Remove after dropping support for pre-AggregateError implementations.
8+
const testIfAggregateError =
9+
typeof AggregateError !== 'undefined' ? test : test.skip;
10+
11+
testIfAggregateError('aggregate error console', t => {
1212
const e3 = Error('e3');
1313
const e2 = Error('e2', { cause: e3 });
1414
const u4 = URIError('u4', { cause: e2 });

packages/ses/test/error/aggregate-error.test.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ const { getOwnPropertyDescriptor } = Object;
55

66
lockdown();
77

8-
test('aggregate error', t => {
9-
if (typeof AggregateError === 'undefined') {
10-
t.pass('skip test on platforms prior to AggregateError');
11-
return;
12-
}
8+
// TODO: Remove after dropping support for pre-AggregateError implementations.
9+
const testIfAggregateError =
10+
typeof AggregateError !== 'undefined' ? test : test.skip;
11+
12+
testIfAggregateError('aggregate error', t => {
1313
const e1 = Error('e1');
1414
const e2 = Error('e2', { cause: e1 });
1515
const u3 = URIError('u3', { cause: e1 });
@@ -31,12 +31,8 @@ test('aggregate error', t => {
3131
});
3232
});
3333

34-
test('Promise.any aggregate error', async t => {
34+
testIfAggregateError('Promise.any aggregate error', async t => {
3535
await null;
36-
if (typeof AggregateError === 'undefined') {
37-
t.pass('skip test on platforms prior to AggregateError');
38-
return;
39-
}
4036
const e1 = Error('e1');
4137
const e2 = Error('e2', { cause: e1 });
4238
const u3 = URIError('u3', { cause: e1 });

0 commit comments

Comments
 (0)