Skip to content

Commit

Permalink
Extend test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion committed Oct 2, 2024
1 parent 160e7c7 commit 10603d0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
27 changes: 27 additions & 0 deletions packages/shared/sdk-client/__tests__/HookRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,33 @@ describe('given a hook runner and test hook', () => {
);
});

it('should log "unknown hook" when getMetadata returns an empty name', () => {
const errorHook: Hook = {
getMetadata: jest.fn().mockImplementation(() => ({
name: '',
})),
beforeEvaluation: jest.fn().mockImplementation(() => {
throw new Error('Test error in beforeEvaluation');
}),
afterEvaluation: jest.fn(),
};

const errorHookRunner = new HookRunner(logger, [errorHook]);

errorHookRunner.withEvaluation('test-flag', { kind: 'user', key: 'user-123' }, false, () => ({
value: true,
variationIndex: 1,
reason: { kind: 'OFF' },
}));

// Verify that the error was logged with the correct hook name
expect(logger.error).toHaveBeenCalledWith(
expect.stringContaining(
'An error was encountered in "beforeEvaluation" of the "unknown hook" hook: Error: Test error in beforeEvaluation',
),
);
});

it('should log the correct hook name when an error occurs', () => {
// Modify the testHook to throw an error in beforeEvaluation
testHook.beforeEvaluation = jest.fn().mockImplementation(() => {
Expand Down
12 changes: 6 additions & 6 deletions packages/shared/sdk-client/src/HookRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ function tryExecuteStage<TData>(
}
}

function getHookName(logger: LDLogger, hook?: Hook): string {
function getHookName(logger: LDLogger, hook: Hook): string {
try {
return hook?.getMetadata().name ?? UNKNOWN_HOOK_NAME;
return hook.getMetadata().name || UNKNOWN_HOOK_NAME;
} catch {
logger.error(`Exception thrown getting metadata for hook. Unable to get hook name.`);
return UNKNOWN_HOOK_NAME;
Expand All @@ -58,14 +58,14 @@ function executeAfterEvaluation(
logger: LDLogger,
hooks: Hook[],
hookContext: EvaluationSeriesContext,
updatedData: (EvaluationSeriesData | undefined)[],
updatedData: EvaluationSeriesData[],
result: LDEvaluationDetail,
) {
// This iterates in reverse, versus reversing a shallow copy of the hooks,
// for efficiency.
for (let hookIndex = hooks.length - 1; hookIndex >= 0; hookIndex -= 1) {
const hook = hooks[hookIndex];
const data = updatedData[hookIndex] ?? {};
const data = updatedData[hookIndex];
tryExecuteStage(
logger,
AFTER_EVALUATION_STAGE_NAME,
Expand Down Expand Up @@ -96,14 +96,14 @@ function executeAfterIdentify(
logger: LDLogger,
hooks: Hook[],
hookContext: IdentifySeriesContext,
updatedData: (IdentifySeriesData | undefined)[],
updatedData: IdentifySeriesData[],
result: IdentifyResult,
) {
// This iterates in reverse, versus reversing a shallow copy of the hooks,
// for efficiency.
for (let hookIndex = hooks.length - 1; hookIndex >= 0; hookIndex -= 1) {
const hook = hooks[hookIndex];
const data = updatedData[hookIndex] ?? {};
const data = updatedData[hookIndex];
tryExecuteStage(
logger,
AFTER_EVALUATION_STAGE_NAME,
Expand Down
1 change: 1 addition & 0 deletions packages/shared/sdk-client/src/api/LDOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { LDLogger } from '@launchdarkly/js-sdk-common';

import { Hook } from './integrations/Hooks';

export interface LDOptions {
Expand Down

0 comments on commit 10603d0

Please sign in to comment.