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
15 changes: 8 additions & 7 deletions packages/sdk/test/test-utils/customMatchers.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 = (
Expand All @@ -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) {
Expand All @@ -60,7 +61,7 @@ const toThrowStreamrError = (
} else {
return {
pass: true,
message: () => `Expected not to throw ${printReceived('StreamrClientError')}`
message: () => `Expected not to throw StreamrClientError}`
}
}
}
Expand Down
59 changes: 59 additions & 0 deletions packages/sdk/test/unit/customMatchers.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
})
})
Loading