-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathopen-feature.module.spec.ts
More file actions
136 lines (115 loc) · 4.58 KB
/
open-feature.module.spec.ts
File metadata and controls
136 lines (115 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import type { TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import { getOpenFeatureClientToken, OpenFeatureModule, ServerProviderEvents } from '../src';
import type { Client } from '@openfeature/server-sdk';
import { OpenFeature } from '@openfeature/server-sdk';
import { defaultProvider, getOpenFeatureDefaultTestModule } from './fixtures';
describe('OpenFeatureModule', () => {
let moduleRef: TestingModule;
describe('client injection', () => {
beforeAll(async () => {
moduleRef = await Test.createTestingModule({
imports: [getOpenFeatureDefaultTestModule()],
}).compile();
});
afterAll(async () => {
await moduleRef.close();
});
describe('without configured providers', () => {
let moduleWithoutProvidersRef: TestingModule;
beforeAll(async () => {
moduleWithoutProvidersRef = await Test.createTestingModule({
imports: [OpenFeatureModule.forRoot({})],
}).compile();
});
afterAll(async () => {
await moduleWithoutProvidersRef.close();
});
it('should return the SDKs default provider and not throw', async () => {
expect(() => {
moduleWithoutProvidersRef.get<Client>(getOpenFeatureClientToken());
}).not.toThrow();
});
});
it('should return the default provider', async () => {
const client = moduleRef.get<Client>(getOpenFeatureClientToken());
expect(client).toBeDefined();
expect(await client.getStringValue('testStringFlag', '')).toEqual('expected-string-value-default');
});
it('should inject the client with the given scope', async () => {
const client = moduleRef.get<Client>(getOpenFeatureClientToken('domainScopedClient'));
expect(client).toBeDefined();
expect(await client.getStringValue('testStringFlag', '')).toEqual('expected-string-value-scoped');
});
it('should expose nest framework metadata on injected clients', () => {
const defaultClient = moduleRef.get<Client>(getOpenFeatureClientToken());
const scopedClient = moduleRef.get<Client>(getOpenFeatureClientToken('domainScopedClient'));
expect(defaultClient.metadata).toMatchObject({
sdk: 'js-server',
paradigm: 'server',
framework: 'nest',
});
expect(scopedClient.metadata).toMatchObject({
sdk: 'js-server',
paradigm: 'server',
framework: 'nest',
});
});
it('should surface nest metadata in hook contexts', async () => {
const hook = { before: jest.fn() };
const hookModuleRef = await Test.createTestingModule({
imports: [OpenFeatureModule.forRoot({ defaultProvider, hooks: [hook] })],
}).compile();
try {
const client = hookModuleRef.get<Client>(getOpenFeatureClientToken());
await client.getBooleanValue('testBooleanFlag', false);
expect(hook.before).toHaveBeenCalledWith(
expect.objectContaining({
clientMetadata: expect.objectContaining({
sdk: 'js-server',
paradigm: 'server',
framework: 'nest',
}),
}),
undefined,
);
} finally {
await hookModuleRef.close();
OpenFeature.clearHooks();
}
});
});
describe('handlers', () => {
let moduleWithoutProvidersRef: TestingModule;
const handlerSpy = jest.fn();
beforeAll(async () => {
moduleWithoutProvidersRef = await Test.createTestingModule({
imports: [OpenFeatureModule.forRoot({ handlers: [[ServerProviderEvents.Ready, handlerSpy]] })],
}).compile();
});
it('should add event handlers to OpenFeature', async () => {
expect(OpenFeature.getHandlers(ServerProviderEvents.ConfigurationChanged)).toHaveLength(0);
expect(OpenFeature.getHandlers(ServerProviderEvents.Stale)).toHaveLength(0);
expect(OpenFeature.getHandlers(ServerProviderEvents.Error)).toHaveLength(0);
expect(OpenFeature.getHandlers(ServerProviderEvents.Ready)).toHaveLength(1);
});
afterAll(async () => {
await moduleWithoutProvidersRef.close();
});
});
describe('hooks', () => {
let moduleWithoutProvidersRef: TestingModule;
const hook = { before: jest.fn() };
beforeAll(async () => {
moduleWithoutProvidersRef = await Test.createTestingModule({
imports: [OpenFeatureModule.forRoot({ hooks: [hook] })],
}).compile();
});
it('should add hooks to OpenFeature', async () => {
expect(OpenFeature.getHooks()).toEqual([hook]);
});
afterAll(async () => {
await moduleWithoutProvidersRef.close();
});
});
});