Skip to content

Commit 9d9ab26

Browse files
committed
support for old versions of node
1 parent 338be2a commit 9d9ab26

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

e2e/__tests__/__snapshots__/toThrowErrorMatchingInlineSnapshot.test.ts.snap

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@ exports[`updates existing snapshot: updated snapshot 1`] = `
2020
2121
exports[`works fine when function throws error with cause: initial write with cause 1`] = `
2222
"test('works fine when function throws error', () => {
23+
function ErrorWithCause(message, cause) {
24+
const err = new Error(message, {cause});
25+
if (err.cause !== cause) {
26+
// cause does not exist in old versions of node
27+
err.cause = cause;
28+
}
29+
return err;
30+
}
2331
expect(() => {
24-
throw new Error('apple', {
25-
cause: new Error('banana', {
26-
cause: new Error('orange'),
27-
}),
28-
});
32+
throw ErrorWithCause(
33+
'apple',
34+
ErrorWithCause('banana', ErrorWithCause('orange')),
35+
);
2936
}).toThrowErrorMatchingInlineSnapshot(\`
3037
"apple
3138
Cause: banana
@@ -37,10 +44,16 @@ exports[`works fine when function throws error with cause: initial write with ca
3744
3845
exports[`works fine when function throws error with string cause: initial write with cause 1`] = `
3946
"test('works fine when function throws error', () => {
47+
function ErrorWithCause(message, cause) {
48+
const err = new Error(message, {cause});
49+
if (err.cause !== cause) {
50+
// cause does not exist in old versions of node
51+
err.cause = cause;
52+
}
53+
return err;
54+
}
4055
expect(() => {
41-
throw new Error('apple', {
42-
cause: 'here is a cause',
43-
});
56+
throw ErrorWithCause('apple', 'here is a cause');
4457
}).toThrowErrorMatchingInlineSnapshot(\`
4558
"apple
4659
Cause: here is a cause"

e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,20 @@ test('works fine when function throws error with cause', () => {
4747
const filename = 'works-fine-when-function-throws-error-with-cause.test.js';
4848
const template = makeTemplate(`
4949
test('works fine when function throws error', () => {
50+
function ErrorWithCause(message, cause) {
51+
const err = new Error(message, {cause});
52+
if (err.cause !== cause) {
53+
// cause does not exist in old versions of node
54+
err.cause = cause;
55+
}
56+
return err;
57+
}
5058
expect(() => {
51-
throw new Error('apple', {
52-
cause: new Error('banana', {
53-
cause: new Error('orange')
54-
})
55-
});
59+
throw ErrorWithCause('apple',
60+
ErrorWithCause('banana',
61+
ErrorWithCause('orange')
62+
)
63+
);
5664
})
5765
.toThrowErrorMatchingInlineSnapshot();
5866
});
@@ -73,10 +81,16 @@ test('works fine when function throws error with string cause', () => {
7381
'works-fine-when-function-throws-error-with-string-cause.test.js';
7482
const template = makeTemplate(`
7583
test('works fine when function throws error', () => {
84+
function ErrorWithCause(message, cause) {
85+
const err = new Error(message, {cause});
86+
if (err.cause !== cause) {
87+
// cause does not exist in old versions of node
88+
err.cause = cause;
89+
}
90+
return err;
91+
}
7692
expect(() => {
77-
throw new Error('apple', {
78-
cause: 'here is a cause'
79-
});
93+
throw ErrorWithCause('apple', 'here is a cause');
8094
})
8195
.toThrowErrorMatchingInlineSnapshot();
8296
});

0 commit comments

Comments
 (0)