Skip to content

Commit 01c5cad

Browse files
teogebjuslesan
andauthored
test(sdk): Rename toThrowStreamrError(), add toEqualStreamrClientError() (#2896)
Renamed to `toThrowStreamrClientError()` to align with the error class name. Just a simple search-and-replace. Contains also contents of PR #2897. --------- Co-authored-by: Santeri Juslenius <santeri.juslenius@gmail.com>
1 parent aa406f7 commit 01c5cad

File tree

13 files changed

+78
-36
lines changed

13 files changed

+78
-36
lines changed

packages/sdk/test/end-to-end/Permissions.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ describe('Stream permissions', () => {
235235
await expect(() => client.grantPermissions(stream.id, {
236236
public: true,
237237
permissions: [StreamPermission.PUBLISH, StreamPermission.GRANT]
238-
})).rejects.toThrowStreamrError({
238+
})).rejects.toThrowStreamrClientError({
239239
message: 'Public permission is not supported for permission types: GRANT',
240240
code: 'UNSUPPORTED_OPERATION'
241241
})
@@ -245,7 +245,7 @@ describe('Stream permissions', () => {
245245
await expect(() => client.grantPermissions(stream.id, {
246246
userId: toUserId(randomBytes(50)),
247247
permissions: [StreamPermission.EDIT, StreamPermission.GRANT]
248-
})).rejects.toThrowStreamrError({
248+
})).rejects.toThrowStreamrClientError({
249249
message: 'Non-Ethereum user id is not supported for permission types: EDIT, GRANT',
250250
code: 'UNSUPPORTED_OPERATION'
251251
})

packages/sdk/test/end-to-end/StreamRegistry.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ describe('StreamRegistry', () => {
148148

149149
it('get a non-existing Stream', async () => {
150150
const streamId = `${publicAddress}/StreamRegistry-nonexisting-${Date.now()}`
151-
return expect(() => client.getStream(streamId)).rejects.toThrowStreamrError({
151+
return expect(() => client.getStream(streamId)).rejects.toThrowStreamrClientError({
152152
code: 'STREAM_NOT_FOUND'
153153
})
154154
}, TIMEOUT)

packages/sdk/test/integration/NetworkNodeFacade.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('NetworkNodeFacade', () => {
5555
await client.destroy()
5656
await expect(async () => {
5757
await getNode()
58-
}).rejects.toThrowStreamrError({ code: 'CLIENT_DESTROYED' })
58+
}).rejects.toThrowStreamrClientError({ code: 'CLIENT_DESTROYED' })
5959
})
6060

6161
it('can call destroy multiple times', async () => {
@@ -67,14 +67,14 @@ describe('NetworkNodeFacade', () => {
6767
await client.destroy()
6868
await expect(async () => {
6969
await getNode()
70-
}).rejects.toThrowStreamrError({ code: 'CLIENT_DESTROYED' })
70+
}).rejects.toThrowStreamrClientError({ code: 'CLIENT_DESTROYED' })
7171
})
7272

7373
it('can destroy before start', async () => {
7474
await client.destroy()
7575
await expect(async () => {
7676
await getNode()
77-
}).rejects.toThrowStreamrError({ code: 'CLIENT_DESTROYED' })
77+
}).rejects.toThrowStreamrClientError({ code: 'CLIENT_DESTROYED' })
7878
})
7979

8080
it('can destroy during start', async () => {
@@ -85,7 +85,7 @@ describe('NetworkNodeFacade', () => {
8585
]
8686
await Promise.allSettled(tasks)
8787
await Promise.all(tasks)
88-
}).rejects.toThrowStreamrError({ code: 'CLIENT_DESTROYED' })
88+
}).rejects.toThrowStreamrClientError({ code: 'CLIENT_DESTROYED' })
8989
})
9090
})
9191
})

packages/sdk/test/integration/Resends2.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('Resends2', () => {
7171
}, {
7272
last: 5
7373
})
74-
}).rejects.toThrowStreamrError(new StreamrClientError(`no storage assigned: ${stream.id}`, 'NO_STORAGE_NODES'))
74+
}).rejects.toThrowStreamrClientError(new StreamrClientError(`no storage assigned: ${stream.id}`, 'NO_STORAGE_NODES'))
7575
})
7676

7777
it('throws error if bad partition', async () => {

packages/sdk/test/integration/client-destroy.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('client destroy', () => {
4343
await client.destroy()
4444
await expect(async () => {
4545
await client.subscribe(stream.id)
46-
}).rejects.toThrowStreamrError({ code: 'CLIENT_DESTROYED' })
46+
}).rejects.toThrowStreamrClientError({ code: 'CLIENT_DESTROYED' })
4747
})
4848

4949
it('unable to publish after destroy called', async () => {

packages/sdk/test/integration/waitForStorage.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,6 @@ describe('waitForStorage', () => {
9696
messageMatchFn: () => {
9797
return true
9898
}
99-
})).rejects.toThrowStreamrError(new StreamrClientError(`no storage assigned: ${stream.id}`, 'NO_STORAGE_NODES'))
99+
})).rejects.toThrowStreamrClientError(new StreamrClientError(`no storage assigned: ${stream.id}`, 'NO_STORAGE_NODES'))
100100
})
101101
})

packages/sdk/test/test-utils/customMatchers.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ declare global {
1313
// eslint-disable-next-line @typescript-eslint/no-namespace
1414
namespace jest {
1515
interface Matchers<R> {
16-
toThrowStreamrError(expectedError: PartialStreamrClientError): R
16+
toThrowStreamrClientError(expectedError: PartialStreamrClientError): R
17+
toEqualStreamrClientError(expectedError: PartialStreamrClientError): R
1718
}
1819
}
1920
}
@@ -22,7 +23,7 @@ const formErrorMessage = (field: keyof StreamrClientError, expected: string, act
2223
return `StreamrClientError ${field} values don't match:\nExpected: ${printExpected(expected)}\nReceived: ${printReceived(actual)}`
2324
}
2425

25-
const toThrowStreamrError = (
26+
const toThrowStreamrClientError = (
2627
actual: unknown, // should be (() => StreamrClientError) | StreamrClientError
2728
expectedError: PartialStreamrClientError
2829
): jest.CustomMatcherResult => {
@@ -40,32 +41,52 @@ const toThrowStreamrError = (
4041
} else {
4142
actualError = actual
4243
}
44+
const assertionErrors = createAssertionErrors(actualError, expectedError)
45+
return toCustomMatcherResult(assertionErrors, 'Expected not to throw StreamrClientError')
46+
}
47+
48+
const toEqualStreamrClientError = (
49+
actual: unknown, // should be StreamrClientError
50+
expectedError: PartialStreamrClientError
51+
): jest.CustomMatcherResult => {
52+
const assertionErrors = createAssertionErrors(actual, expectedError)
53+
return toCustomMatcherResult(assertionErrors, 'StreamrClientErrors are equal')
54+
}
4355

44-
const messages: string[] = []
56+
const createAssertionErrors = (
57+
actualError: unknown,
58+
expectedError: PartialStreamrClientError
59+
): string[] => {
60+
const assertionErrors: string[] = []
4561
if (!(actualError instanceof StreamrClientError)) {
4662
const received = isObject(actualError) ? actualError.constructor.name : actualError
47-
messages.push(`Not an instance of StreamrClientError:\nReceived: ${printReceived(received)}`)
63+
assertionErrors.push(`Not an instance of StreamrClientError:\nReceived: ${printReceived(received)}`)
4864
} else {
4965
if (actualError.code !== expectedError.code) {
50-
messages.push(formErrorMessage('code', expectedError.code, actualError.code))
66+
assertionErrors.push(formErrorMessage('code', expectedError.code, actualError.code))
5167
}
5268
if ((expectedError.message !== undefined) && (actualError.message !== expectedError.message)) {
53-
messages.push(formErrorMessage('message', expectedError.message, actualError.message))
69+
assertionErrors.push(formErrorMessage('message', expectedError.message, actualError.message))
5470
}
5571
}
56-
if (messages.length > 0) {
72+
return assertionErrors
73+
}
74+
75+
const toCustomMatcherResult = (assertionErrors: string[], inversionErrorMessage: string) => {
76+
if (assertionErrors.length > 0) {
5777
return {
5878
pass: false,
59-
message: () => messages.join('\n\n')
79+
message: () => assertionErrors.join('\n\n')
6080
}
6181
} else {
6282
return {
6383
pass: true,
64-
message: () => `Expected not to throw StreamrClientError}`
84+
message: () => inversionErrorMessage
6585
}
6686
}
6787
}
6888

6989
expect.extend({
70-
toThrowStreamrError
90+
toThrowStreamrClientError,
91+
toEqualStreamrClientError
7192
})

packages/sdk/test/unit/Publisher.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('Publisher', () => {
2727
const streamId = await streamIdBuilder.toStreamID('/test')
2828
await expect(async () => {
2929
await publisher.publish(streamId, {})
30-
}).rejects.toThrowStreamrError({
30+
}).rejects.toThrowStreamrClientError({
3131
code: 'MISSING_PERMISSION',
3232
// eslint-disable-next-line max-len
3333
message: `Failed to publish to stream ${streamId}. Cause: You don't have permission to publish to this stream. Using address: ${await authentication.getUserId()}`

packages/sdk/test/unit/PushBuffer.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,19 @@ describe.skip('PushBuffer', () => {
174174
it('errors on bad buffer size', async () => {
175175
expect(() => {
176176
new PushBuffer(0)
177-
}).toThrowStreamrError({ code: 'INVALID_ARGUMENT' })
177+
}).toThrowStreamrClientError({ code: 'INVALID_ARGUMENT' })
178178
expect(() => {
179179
new PushBuffer(-1)
180-
}).toThrowStreamrError({ code: 'INVALID_ARGUMENT' })
180+
}).toThrowStreamrClientError({ code: 'INVALID_ARGUMENT' })
181181
expect(() => {
182182
new PushBuffer(Number.MAX_SAFE_INTEGER + 10)
183-
}).toThrowStreamrError({ code: 'INVALID_ARGUMENT' })
183+
}).toThrowStreamrClientError({ code: 'INVALID_ARGUMENT' })
184184
expect(() => {
185185
new PushBuffer(1.5)
186-
}).toThrowStreamrError({ code: 'INVALID_ARGUMENT' })
186+
}).toThrowStreamrClientError({ code: 'INVALID_ARGUMENT' })
187187
expect(() => {
188188
new PushBuffer(0.5)
189-
}).toThrowStreamrError({ code: 'INVALID_ARGUMENT' })
189+
}).toThrowStreamrClientError({ code: 'INVALID_ARGUMENT' })
190190
})
191191

192192
it('can push inside pull', async () => {

packages/sdk/test/unit/Resends.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('Resends', () => {
3333
await expect(async () => {
3434
const messages = await resends.resend(StreamPartIDUtils.parse('stream#0'), { last: 1, raw: true }, async () => [randomEthereumAddress()])
3535
await collect(messages)
36-
}).rejects.toThrowStreamrError({
36+
}).rejects.toThrowStreamrClientError({
3737
message: `Storage node fetch failed: Mock error, httpStatus=400, url=${requestUrl}`,
3838
code: 'STORAGE_NODE_ERROR'
3939
})
@@ -45,7 +45,7 @@ describe('Resends', () => {
4545
await expect(async () => {
4646
const messages = await resends.resend(StreamPartIDUtils.parse('stream#0'), { last: 1, raw: true }, async () => [randomEthereumAddress()])
4747
await collect(messages)
48-
}).rejects.toThrowStreamrError({
48+
}).rejects.toThrowStreamrClientError({
4949
message: isRunningInElectron()
5050
? 'Failed to fetch'
5151
// eslint-disable-next-line max-len

0 commit comments

Comments
 (0)