From 027dd3d8ffa9f3f2b9692552297a2c6041bddc87 Mon Sep 17 00:00:00 2001 From: Todd Baert Date: Thu, 15 Aug 2024 15:00:51 -0400 Subject: [PATCH] chore: add assertion for hook context contents Signed-off-by: Todd Baert --- packages/server/test/client.spec.ts | 33 +++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/server/test/client.spec.ts b/packages/server/test/client.spec.ts index 368c3bb54..bbb44312c 100644 --- a/packages/server/test/client.spec.ts +++ b/packages/server/test/client.spec.ts @@ -5,6 +5,7 @@ import { EvaluationContext, EvaluationDetails, FlagNotFoundError, + Hook, JsonArray, JsonObject, JsonValue, @@ -18,6 +19,8 @@ import { TransactionContextPropagator, } from '../src'; import { OpenFeatureClient } from '../src/client/internal/open-feature-client'; +import { isDeepStrictEqual } from 'node:util'; +import { HookContext } from '@openfeature/core'; const BOOLEAN_VALUE = true; const STRING_VALUE = 'val'; @@ -740,9 +743,35 @@ describe('OpenFeatureClient', () => { // Set Client Context const client = OpenFeature.getClient('contextual', 'test', clientContext); // Set Hook Context - client.addHooks({ before: () => beforeHookContext }); + const hook = { + before: jest.fn((hookContext: HookContext) => { + // we have to put this assertion here because of limitations in jest with expect.objectContaining and mutability + if (isDeepStrictEqual(hookContext.context, { + ...globalContext, + ...transactionContext, + ...clientContext, + ...invocationContext, + // before hook context should be missing here (and not overridden) + })) { + return beforeHookContext; + } + }), + after: jest.fn((hookContext: HookContext) => { + // we have to put this assertion here because of limitations in jest with expect.objectContaining and mutability + if (isDeepStrictEqual(hookContext.context, { + ...globalContext, + ...transactionContext, + ...clientContext, + ...invocationContext, + ...beforeHookContext, + })) { + return beforeHookContext; + } + }), + } as unknown as Hook; + + await client.getBooleanValue(flagKey, defaultValue, invocationContext, { hooks: [hook] }); - await client.getBooleanValue(flagKey, defaultValue, invocationContext); expect(provider.resolveBooleanEvaluation).toHaveBeenCalledWith( expect.anything(), expect.anything(),