Skip to content

Commit 964d65b

Browse files
authored
chore: add assertion for hook context contents (#977)
There's no bug here, but this adds some asserts for a tricky bug I recently found in the Java SDK: open-feature/java-sdk#1049 Signed-off-by: Todd Baert <[email protected]>
1 parent 51d4323 commit 964d65b

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

packages/server/test/client.spec.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
EvaluationContext,
66
EvaluationDetails,
77
FlagNotFoundError,
8+
Hook,
89
JsonArray,
910
JsonObject,
1011
JsonValue,
@@ -18,6 +19,8 @@ import {
1819
TransactionContextPropagator,
1920
} from '../src';
2021
import { OpenFeatureClient } from '../src/client/internal/open-feature-client';
22+
import { isDeepStrictEqual } from 'node:util';
23+
import { HookContext } from '@openfeature/core';
2124

2225
const BOOLEAN_VALUE = true;
2326
const STRING_VALUE = 'val';
@@ -740,9 +743,35 @@ describe('OpenFeatureClient', () => {
740743
// Set Client Context
741744
const client = OpenFeature.getClient('contextual', 'test', clientContext);
742745
// Set Hook Context
743-
client.addHooks({ before: () => beforeHookContext });
746+
const hook = {
747+
before: jest.fn((hookContext: HookContext) => {
748+
// we have to put this assertion here because of limitations in jest with expect.objectContaining and mutability
749+
if (isDeepStrictEqual(hookContext.context, {
750+
...globalContext,
751+
...transactionContext,
752+
...clientContext,
753+
...invocationContext,
754+
// before hook context should be missing here (and not overridden)
755+
})) {
756+
return beforeHookContext;
757+
}
758+
}),
759+
after: jest.fn((hookContext: HookContext) => {
760+
// we have to put this assertion here because of limitations in jest with expect.objectContaining and mutability
761+
if (isDeepStrictEqual(hookContext.context, {
762+
...globalContext,
763+
...transactionContext,
764+
...clientContext,
765+
...invocationContext,
766+
...beforeHookContext,
767+
})) {
768+
return beforeHookContext;
769+
}
770+
}),
771+
} as unknown as Hook;
772+
773+
await client.getBooleanValue(flagKey, defaultValue, invocationContext, { hooks: [hook] });
744774

745-
await client.getBooleanValue(flagKey, defaultValue, invocationContext);
746775
expect(provider.resolveBooleanEvaluation).toHaveBeenCalledWith(
747776
expect.anything(),
748777
expect.anything(),

0 commit comments

Comments
 (0)