diff --git a/packages/sdk/test/test-utils/customMatchers.ts b/packages/sdk/test/test-utils/customMatchers.ts index aeedacaff1..ddd65e7ef4 100644 --- a/packages/sdk/test/test-utils/customMatchers.ts +++ b/packages/sdk/test/test-utils/customMatchers.ts @@ -1,5 +1,5 @@ import { printExpected, printReceived } from 'jest-matcher-utils' -import isFunction from 'lodash/isFunction' +import { isFunction, isObject } from 'lodash' import { StreamrClientError, StreamrClientErrorCode } from './../../src/StreamrClientError' interface PartialStreamrClientError { @@ -18,8 +18,8 @@ declare global { } } -const formErrorMessage = (description: string, expected: string, actual: string): string => { - return `${description}\nExpected: ${printExpected(expected)}\nReceived: ${printReceived(actual)}` +const formErrorMessage = (field: keyof StreamrClientError, expected: string, actual: string): string => { + return `StreamrClientError ${field} values don't match:\nExpected: ${printExpected(expected)}\nReceived: ${printReceived(actual)}` } const toThrowStreamrError = ( @@ -43,13 +43,14 @@ const toThrowStreamrError = ( const messages: string[] = [] if (!(actualError instanceof StreamrClientError)) { - messages.push(formErrorMessage('Class name', 'StreamrClientError', actualError.constructor.name)) + const received = isObject(actualError) ? actualError.constructor.name : actualError + messages.push(`Not an instance of StreamrClientError:\nReceived: ${printReceived(received)}`) } else { if (actualError.code !== expectedError.code) { - messages.push(formErrorMessage('StreamrClientError.code', expectedError.code, actualError.code)) + messages.push(formErrorMessage('code', expectedError.code, actualError.code)) } if ((expectedError.message !== undefined) && (actualError.message !== expectedError.message)) { - messages.push(formErrorMessage('StreamrClientError.message', expectedError.message, actualError.message)) + messages.push(formErrorMessage('message', expectedError.message, actualError.message)) } } if (messages.length > 0) { @@ -60,7 +61,7 @@ const toThrowStreamrError = ( } else { return { pass: true, - message: () => `Expected not to throw ${printReceived('StreamrClientError')}` + message: () => `Expected not to throw StreamrClientError}` } } } diff --git a/packages/sdk/test/unit/customMatchers.test.ts b/packages/sdk/test/unit/customMatchers.test.ts new file mode 100644 index 0000000000..e854ddc2ad --- /dev/null +++ b/packages/sdk/test/unit/customMatchers.test.ts @@ -0,0 +1,59 @@ +import { StreamrClientError } from '../../src/StreamrClientError' + +describe('custom matchers', () => { + + describe('toThrowStreamrError', () => { + + it('happy path', () => { + const error = new StreamrClientError('Foobar', 'UNKNOWN_ERROR') + expect(() => { + throw error + }).toThrowStreamrError({ + code: error.code, + message: error.message + }) + }) + + describe('error message', () => { + + it('field', () => { + const actual = new StreamrClientError('Foobar', 'UNKNOWN_ERROR') + expect(() => { + expect(() => { throw actual }).toThrowStreamrError({ + message: 'Foobar', + code: 'UNSUPPORTED_OPERATION' + }) + }).toThrow('StreamrClientError code values don\'t match') + }) + + it('unexpected class', () => { + // eslint-disable-next-line @typescript-eslint/no-extraneous-class + class TestClass {} + expect(() => { + // eslint-disable-next-line @typescript-eslint/only-throw-error + expect(() => { throw new TestClass() }).toThrowStreamrError({ + message: 'Foobar', + code: 'UNSUPPORTED_OPERATION' + }) + }).toThrow('Not an instance of StreamrClientError:\nReceived: "TestClass"') + }) + + it('unexpected primitive', () => { + expect(() => { + // eslint-disable-next-line @typescript-eslint/only-throw-error + expect(() => { throw 'mock-error' }).toThrowStreamrError({ + message: 'Foobar', + code: 'UNSUPPORTED_OPERATION' + }) + }).toThrow('Not an instance of StreamrClientError:\nReceived: "mock-error') + }) + + it('inverse', () => { + const actual = new StreamrClientError('Foobar', 'UNKNOWN_ERROR') + expect(() => { + expect(() => { throw actual }).not.toThrowStreamrError(actual) + }).toThrow('Expected not to throw StreamrClientError') + }) + }) + }) +})