From d11ea159d66d73e99b1f51f15792372ca1a1b1cb Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Tue, 15 Jun 2021 11:48:23 -0400 Subject: [PATCH] New internal testing utility: withErrorSpy(it, "should...", ...) --- src/__tests__/__snapshots__/exports.ts.snap | 1 + src/utilities/testing/index.ts | 1 + src/utilities/testing/itAsync.ts | 17 +++++++++-------- src/utilities/testing/withErrorSpy.ts | 21 +++++++++++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 src/utilities/testing/withErrorSpy.ts diff --git a/src/__tests__/__snapshots__/exports.ts.snap b/src/__tests__/__snapshots__/exports.ts.snap index 82a7e0a1010..bf8dd514224 100644 --- a/src/__tests__/__snapshots__/exports.ts.snap +++ b/src/__tests__/__snapshots__/exports.ts.snap @@ -314,6 +314,7 @@ Array [ "mockSingleLink", "stripSymbols", "subscribeAndCount", + "withErrorSpy", ] `; diff --git a/src/utilities/testing/index.ts b/src/utilities/testing/index.ts index 45c2f3198f1..668557783e9 100644 --- a/src/utilities/testing/index.ts +++ b/src/utilities/testing/index.ts @@ -13,3 +13,4 @@ export { createMockClient } from './mocking/mockClient'; export { stripSymbols } from './stripSymbols'; export { default as subscribeAndCount } from './subscribeAndCount'; export { itAsync } from './itAsync'; +export { withErrorSpy } from './withErrorSpy'; diff --git a/src/utilities/testing/itAsync.ts b/src/utilities/testing/itAsync.ts index 17c8406321e..9d54b947671 100644 --- a/src/utilities/testing/itAsync.ts +++ b/src/utilities/testing/itAsync.ts @@ -14,12 +14,13 @@ function wrap(key?: "only" | "skip" | "todo") { } const wrappedIt = wrap(); -export function itAsync(...args: Parameters) { - return wrappedIt.apply(this, args); -} -export namespace itAsync { - export const only = wrap("only"); - export const skip = wrap("skip"); - export const todo = wrap("todo"); -} +export const itAsync = Object.assign(function ( + ...args: Parameters +) { + return wrappedIt.apply(this, args); +}, { + only: wrap("only"), + skip: wrap("skip"), + todo: wrap("todo"), +}); diff --git a/src/utilities/testing/withErrorSpy.ts b/src/utilities/testing/withErrorSpy.ts new file mode 100644 index 00000000000..2c39e8c1f91 --- /dev/null +++ b/src/utilities/testing/withErrorSpy.ts @@ -0,0 +1,21 @@ +export function withErrorSpy< + TArgs extends any[], + TResult, +>( + it: (...args: TArgs) => TResult, + ...args: TArgs +) { + const fn = args[1]; + args[1] = function () { + const args = arguments; + const errorSpy = jest.spyOn(console, 'error'); + errorSpy.mockImplementation(() => {}); + return new Promise(resolve => { + resolve(fn?.apply(this, args)); + }).finally(() => { + expect(errorSpy).toMatchSnapshot(); + errorSpy.mockReset(); + }); + }; + return it(...args); +}