Skip to content

Commit 7d2d5c1

Browse files
handle error assertions more gracefully
Problem: Sometimes we try and assert a specific error, but up the call stack that error may get wrapped in a `ToolkitError` and now exists in the `cause`. Then when we test we need to know where exactly in the `cause` chain our expected error is. This is annoying. Solution: Create a function which recursively goes through the cause chain searching for the expected error. Signed-off-by: nkomonen-amazon <[email protected]>
1 parent 65e651e commit 7d2d5c1

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

packages/core/src/test/credentials/auth.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import fs from '../../shared/fs/fs'
99
import { ToolkitError, isUserCancelledError } from '../../shared/errors'
1010
import { assertTreeItem } from '../shared/treeview/testUtil'
1111
import { getTestWindow } from '../shared/vscode/window'
12-
import { assertTelemetry, captureEventOnce } from '../testUtil'
12+
import { assertError as assertHasError, assertTelemetry, captureEventOnce } from '../testUtil'
1313
import { createBuilderIdProfile, createSsoProfile, createTestAuth } from './testUtil'
1414
import { toCollection } from '../../shared/utilities/asyncCollection'
1515
import globals from '../../shared/extensionGlobals'
@@ -316,7 +316,7 @@ describe('Auth', function () {
316316
auth.getTestTokenProvider(conn)?.getToken.rejects(err1)
317317
const err2 = await runExpiredConnectionFlow(conn, /no/i).catch((e) => e)
318318
assert.ok(err2 instanceof ToolkitError)
319-
assert.strictEqual(err2.cause, err1)
319+
assertHasError(err2.cause, err1)
320320
})
321321

322322
it('bubbles up networking issues instead of invalidating the connection', async function () {
@@ -325,7 +325,7 @@ describe('Auth', function () {
325325
auth.getTestTokenProvider(conn)?.getToken.rejects(expected)
326326
const actual = await conn.getToken().catch((e) => e)
327327
assert.ok(actual instanceof ToolkitError)
328-
assert.strictEqual(actual.cause, expected)
328+
assertHasError(actual, expected)
329329
assert.strictEqual(auth.getConnectionState(conn), 'valid')
330330
})
331331

@@ -339,7 +339,7 @@ describe('Auth', function () {
339339
auth.getTestTokenProvider(conn)?.getToken.rejects(networkError)
340340
const actual = await auth.refreshConnectionState(conn).catch((e) => e)
341341
assert.ok(actual instanceof ToolkitError)
342-
assert.deepStrictEqual(actual, expectedError)
342+
assertHasError(actual, expectedError)
343343
assert.strictEqual(auth.getConnectionState(conn), 'valid')
344344
})
345345

packages/core/src/test/testUtil.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { mkdirSync, existsSync } from 'fs' // eslint-disable-line no-restricted-
1919
import { randomBytes } from 'crypto'
2020
import request from '../shared/request'
2121
import { stub } from 'sinon'
22+
import { ToolkitError } from '../shared'
2223

2324
const testTempDirs: string[] = []
2425

@@ -623,3 +624,19 @@ export function tryRegister(command: DeclaredCommand<() => Promise<any>>) {
623624
export function getFetchStubWithResponse(response: Partial<Response>) {
624625
return stub(request, 'fetch').returns({ response: new Promise((res, _) => res(response)) } as any)
625626
}
627+
628+
/**
629+
* Asserts that the errors match, or at least exists somewhere in the `cause` chain
630+
*/
631+
export function assertError(actual: unknown, expected: unknown) {
632+
try {
633+
return assert.deepStrictEqual(actual, expected)
634+
} catch (e) {
635+
if (!(actual instanceof ToolkitError)) {
636+
throw e
637+
}
638+
}
639+
assert(actual instanceof ToolkitError) // make linter happy
640+
// recursively search causal chaing for matching error
641+
assertError(actual.cause, expected)
642+
}

0 commit comments

Comments
 (0)