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: https://github.com/open-feature/java-sdk/pull/1049 Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
This commit is contained in:
parent
51d43230d9
commit
964d65b775
|
|
@ -5,6 +5,7 @@ import {
|
||||||
EvaluationContext,
|
EvaluationContext,
|
||||||
EvaluationDetails,
|
EvaluationDetails,
|
||||||
FlagNotFoundError,
|
FlagNotFoundError,
|
||||||
|
Hook,
|
||||||
JsonArray,
|
JsonArray,
|
||||||
JsonObject,
|
JsonObject,
|
||||||
JsonValue,
|
JsonValue,
|
||||||
|
|
@ -18,6 +19,8 @@ import {
|
||||||
TransactionContextPropagator,
|
TransactionContextPropagator,
|
||||||
} from '../src';
|
} from '../src';
|
||||||
import { OpenFeatureClient } from '../src/client/internal/open-feature-client';
|
import { OpenFeatureClient } from '../src/client/internal/open-feature-client';
|
||||||
|
import { isDeepStrictEqual } from 'node:util';
|
||||||
|
import { HookContext } from '@openfeature/core';
|
||||||
|
|
||||||
const BOOLEAN_VALUE = true;
|
const BOOLEAN_VALUE = true;
|
||||||
const STRING_VALUE = 'val';
|
const STRING_VALUE = 'val';
|
||||||
|
|
@ -740,9 +743,35 @@ describe('OpenFeatureClient', () => {
|
||||||
// Set Client Context
|
// Set Client Context
|
||||||
const client = OpenFeature.getClient('contextual', 'test', clientContext);
|
const client = OpenFeature.getClient('contextual', 'test', clientContext);
|
||||||
// Set Hook Context
|
// 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(provider.resolveBooleanEvaluation).toHaveBeenCalledWith(
|
||||||
expect.anything(),
|
expect.anything(),
|
||||||
expect.anything(),
|
expect.anything(),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue