From 9d9ab2640c01471d6e24a6013ff797df6ef75624 Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Tue, 28 Feb 2023 14:28:54 +0000 Subject: [PATCH] support for old versions of node --- ...owErrorMatchingInlineSnapshot.test.ts.snap | 29 +++++++++++++----- ...toThrowErrorMatchingInlineSnapshot.test.ts | 30 ++++++++++++++----- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/e2e/__tests__/__snapshots__/toThrowErrorMatchingInlineSnapshot.test.ts.snap b/e2e/__tests__/__snapshots__/toThrowErrorMatchingInlineSnapshot.test.ts.snap index 890455297aee..7a21b7968e93 100644 --- a/e2e/__tests__/__snapshots__/toThrowErrorMatchingInlineSnapshot.test.ts.snap +++ b/e2e/__tests__/__snapshots__/toThrowErrorMatchingInlineSnapshot.test.ts.snap @@ -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 @@ -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" diff --git a/e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts b/e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts index 8df2b36ab627..88b86838a036 100644 --- a/e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts +++ b/e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts @@ -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(); }); @@ -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(); });