Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions packages/sdk/test/test-utils/customMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare global {
namespace jest {
interface Matchers<R> {
toThrowStreamrClientError(expectedError: PartialStreamrClientError): R
toEqualStreamrClientError(expectedError: PartialStreamrClientError): R
}
}
}
Expand All @@ -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
})
21 changes: 21 additions & 0 deletions packages/sdk/test/unit/customMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
})
})
Loading