Skip to content

Commit

Permalink
support for old versions of node
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Feb 28, 2023
1 parent 338be2a commit 9d9ab26
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ exports[`updates existing snapshot: updated snapshot 1`] = `
exports[`works fine when function throws error with cause: initial write with cause 1`] = `
"test('works fine when function throws error', () => {
function ErrorWithCause(message, cause) {
const err = new Error(message, {cause});
if (err.cause !== cause) {
// cause does not exist in old versions of node
err.cause = cause;
}
return err;
}
expect(() => {
throw new Error('apple', {
cause: new Error('banana', {
cause: new Error('orange'),
}),
});
throw ErrorWithCause(
'apple',
ErrorWithCause('banana', ErrorWithCause('orange')),
);
}).toThrowErrorMatchingInlineSnapshot(\`
"apple
Cause: banana
Expand All @@ -37,10 +44,16 @@ exports[`works fine when function throws error with cause: initial write with ca
exports[`works fine when function throws error with string cause: initial write with cause 1`] = `
"test('works fine when function throws error', () => {
function ErrorWithCause(message, cause) {
const err = new Error(message, {cause});
if (err.cause !== cause) {
// cause does not exist in old versions of node
err.cause = cause;
}
return err;
}
expect(() => {
throw new Error('apple', {
cause: 'here is a cause',
});
throw ErrorWithCause('apple', 'here is a cause');
}).toThrowErrorMatchingInlineSnapshot(\`
"apple
Cause: here is a cause"
Expand Down
30 changes: 22 additions & 8 deletions e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,20 @@ test('works fine when function throws error with cause', () => {
const filename = 'works-fine-when-function-throws-error-with-cause.test.js';
const template = makeTemplate(`
test('works fine when function throws error', () => {
function ErrorWithCause(message, cause) {
const err = new Error(message, {cause});
if (err.cause !== cause) {
// cause does not exist in old versions of node
err.cause = cause;
}
return err;
}
expect(() => {
throw new Error('apple', {
cause: new Error('banana', {
cause: new Error('orange')
})
});
throw ErrorWithCause('apple',
ErrorWithCause('banana',
ErrorWithCause('orange')
)
);
})
.toThrowErrorMatchingInlineSnapshot();
});
Expand All @@ -73,10 +81,16 @@ test('works fine when function throws error with string cause', () => {
'works-fine-when-function-throws-error-with-string-cause.test.js';
const template = makeTemplate(`
test('works fine when function throws error', () => {
function ErrorWithCause(message, cause) {
const err = new Error(message, {cause});
if (err.cause !== cause) {
// cause does not exist in old versions of node
err.cause = cause;
}
return err;
}
expect(() => {
throw new Error('apple', {
cause: 'here is a cause'
});
throw ErrorWithCause('apple', 'here is a cause');
})
.toThrowErrorMatchingInlineSnapshot();
});
Expand Down

0 comments on commit 9d9ab26

Please sign in to comment.