diff --git a/packages/sdk/test/test-utils/customMatchers.ts b/packages/sdk/test/test-utils/customMatchers.ts index e69195d5a4..307b38e5be 100644 --- a/packages/sdk/test/test-utils/customMatchers.ts +++ b/packages/sdk/test/test-utils/customMatchers.ts @@ -14,6 +14,7 @@ declare global { namespace jest { interface Matchers { toThrowStreamrClientError(expectedError: PartialStreamrClientError): R + toEqualStreamrClientError(expectedError: PartialStreamrClientError): R } } } @@ -40,32 +41,52 @@ const toThrowStreamrClientError = ( } else { actualError = actual } + const assertionErrors = createAssertionErrors(actualError, expectedError) + return toCustomMatcherResult(assertionErrors, 'Expected not to throw StreamrClientError') +} + +const toEqualStreamrClientError = ( + actual: unknown, // should be StreamrClientError + expectedError: PartialStreamrClientError +): jest.CustomMatcherResult => { + const assertionErrors = createAssertionErrors(actual, expectedError) + return toCustomMatcherResult(assertionErrors, 'StreamrClientErrors are equal') +} - const messages: string[] = [] +const createAssertionErrors = ( + actualError: unknown, + expectedError: PartialStreamrClientError +): string[] => { + const assertionErrors: string[] = [] if (!(actualError instanceof StreamrClientError)) { const received = isObject(actualError) ? actualError.constructor.name : actualError - messages.push(`Not an instance of StreamrClientError:\nReceived: ${printReceived(received)}`) + assertionErrors.push(`Not an instance of StreamrClientError:\nReceived: ${printReceived(received)}`) } else { if (actualError.code !== expectedError.code) { - messages.push(formErrorMessage('code', expectedError.code, actualError.code)) + assertionErrors.push(formErrorMessage('code', expectedError.code, actualError.code)) } if ((expectedError.message !== undefined) && (actualError.message !== expectedError.message)) { - messages.push(formErrorMessage('message', expectedError.message, actualError.message)) + assertionErrors.push(formErrorMessage('message', expectedError.message, actualError.message)) } } - if (messages.length > 0) { + return assertionErrors +} + +const toCustomMatcherResult = (assertionErrors: string[], inversionErrorMessage: string) => { + if (assertionErrors.length > 0) { return { pass: false, - message: () => messages.join('\n\n') + message: () => assertionErrors.join('\n\n') } } else { return { pass: true, - message: () => `Expected not to throw StreamrClientError}` + message: () => inversionErrorMessage } } } expect.extend({ - toThrowStreamrClientError + toThrowStreamrClientError, + toEqualStreamrClientError }) diff --git a/packages/sdk/test/unit/customMatchers.test.ts b/packages/sdk/test/unit/customMatchers.test.ts index cc6987f188..a46845a518 100644 --- a/packages/sdk/test/unit/customMatchers.test.ts +++ b/packages/sdk/test/unit/customMatchers.test.ts @@ -56,4 +56,25 @@ describe('custom matchers', () => { }) }) }) + + describe('toEqualStreamrClientError', () => { + + it('happy path', () => { + const error = new StreamrClientError('Foobar', 'UNKNOWN_ERROR') + expect(error).toEqualStreamrClientError({ + code: error.code, + message: error.message + }) + }) + + describe('error message', () => { + + it('inverse', () => { + const actual = new StreamrClientError('Foobar', 'UNKNOWN_ERROR') + expect(() => { + expect(actual).not.toEqualStreamrClientError(actual) + }).toThrow('treamrClientErrors are equal') + }) + }) + }) })