Skip to content

Commit f29d97d

Browse files
committed
fix: support react native SDK
fix: support react native SDK fix: test fail on node:e2e test: remove duplicate/ unnecessary localStorage.clear() Update events.spec.ts Update events.spec.ts test: inject correct entry point for node environment Update package.json Update package.json fix: lint fail chore: required id-generator for react native environment bump to 2.2.13 fix: missing default user agent in react native environment Update package.json
1 parent 742c349 commit f29d97d

19 files changed

+303
-50
lines changed

e2e/BKTClient.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
destroyBKTClient,
44
getBKTClient,
55
initializeBKTClient,
6-
} from '../src/main.browser'
6+
} from './module'
77
import { BKTConfig, defineBKTConfig } from '../src/BKTConfig'
88
import { BKTUser, defineBKTUser } from '../src/BKTUser'
99
import { FEATURE_ID_STRING, USER_ID } from './constants'
@@ -12,14 +12,14 @@ import { BKTClient, BKTClientImpl } from '../src/BKTClient'
1212
import { DefaultComponent } from '../src/internal/di/Component'
1313
import { EvaluationStorageImpl } from '../src/internal/evaluation/EvaluationStorage'
1414
import { evaluation1 } from '../test/mocks/evaluations'
15+
import { fetchLike } from './environment'
1516

1617
suite('e2e/BKTClientTest', () => {
1718
let config: BKTConfig
1819
let user: BKTUser
1920

2021
afterEach(() => {
2122
destroyBKTClient()
22-
localStorage.clear()
2323
})
2424

2525
suite('get string variation using user attribute when initializing', () => {
@@ -29,7 +29,7 @@ suite('e2e/BKTClientTest', () => {
2929
apiKey: import.meta.env.VITE_BKT_API_KEY,
3030
featureTag: 'javascript',
3131
appVersion: '1.2.3',
32-
fetch: window.fetch,
32+
fetch: fetchLike,
3333
})
3434

3535
user = defineBKTUser({
@@ -54,7 +54,7 @@ suite('e2e/BKTClientTest', () => {
5454
apiKey: import.meta.env.VITE_BKT_API_KEY,
5555
featureTag: 'javascript',
5656
appVersion: '1.2.3',
57-
fetch: window.fetch,
57+
fetch: fetchLike,
5858
})
5959

6060
user = defineBKTUser({
@@ -99,7 +99,7 @@ suite('e2e/BKTClientTest', () => {
9999
apiKey: import.meta.env.VITE_BKT_API_KEY,
100100
featureTag: 'javascript',
101101
appVersion: '1.2.3',
102-
fetch: window.fetch,
102+
fetch: fetchLike,
103103
})
104104

105105
user = defineBKTUser({
@@ -201,7 +201,7 @@ suite('e2e/BKTClientTest', () => {
201201
apiEndpoint: import.meta.env.VITE_BKT_API_ENDPOINT,
202202
apiKey: import.meta.env.VITE_BKT_API_KEY,
203203
appVersion: '1.2.3',
204-
fetch: window.fetch,
204+
fetch: fetchLike,
205205
})
206206

207207
user = defineBKTUser({

e2e/environment.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { FetchLike } from '../src/internal/remote/fetch'
2+
3+
let fetchLike: FetchLike
4+
let isNodeEnvironment = false
5+
6+
function setFetchProvider(fetch: FetchLike) {
7+
fetchLike = fetch
8+
}
9+
10+
function setIsNodeEnvironment(isNode: boolean) {
11+
isNodeEnvironment = isNode
12+
}
13+
14+
export {
15+
setFetchProvider,
16+
setIsNodeEnvironment,
17+
fetchLike,
18+
isNodeEnvironment,
19+
}

e2e/evaluations.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
destroyBKTClient,
44
getBKTClient,
55
initializeBKTClient,
6-
} from '../src/main.browser'
6+
} from './module'
77
import { BKTConfig, defineBKTConfig } from '../src/BKTConfig'
88
import { BKTUser, defineBKTUser } from '../src/BKTUser'
99
import {
@@ -15,6 +15,7 @@ import {
1515
USER_ID,
1616
} from './constants'
1717
import './assertions'
18+
import { fetchLike } from './environment'
1819

1920
suite('e2e/evaluations', () => {
2021
let config: BKTConfig
@@ -26,7 +27,7 @@ suite('e2e/evaluations', () => {
2627
apiKey: import.meta.env.VITE_BKT_API_KEY,
2728
featureTag: 'javascript',
2829
appVersion: '1.2.3',
29-
fetch: window.fetch,
30+
fetch: fetchLike,
3031
})
3132

3233
user = defineBKTUser({
@@ -38,7 +39,6 @@ suite('e2e/evaluations', () => {
3839

3940
afterEach(() => {
4041
destroyBKTClient()
41-
localStorage.clear()
4242
})
4343

4444
suite('stringVariation', () => {

e2e/events.spec.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import { suite, test, expect, beforeEach, afterEach, assert } from 'vitest'
22
import {
33
BKTClient,
44
BKTClientImpl,
5-
destroyBKTClient,
6-
getBKTClient,
75
} from '../src/BKTClient'
8-
import { initializeBKTClient } from '../src/main.browser'
6+
import { initializeBKTClient, getBKTClient, destroyBKTClient } from './module'
97
import { BKTConfig, defineBKTConfig } from '../src/BKTConfig'
108
import { BKTUser, defineBKTUser } from '../src/BKTUser'
119
import { DefaultComponent } from '../src/internal/di/Component'
@@ -25,6 +23,7 @@ import { ForbiddenException, TimeoutException } from '../src/BKTExceptions'
2523
import { ApiId, MetricsEventType } from '../src/internal/model/MetricsEventData'
2624
import { SDK_VERSION } from '../src/internal/version'
2725
import { SourceId } from '../src/internal/model/SourceId'
26+
import { fetchLike, isNodeEnvironment } from './environment'
2827

2928
function getDefaultComponent(client: BKTClient): DefaultComponent {
3029
return (client as BKTClientImpl).component as DefaultComponent
@@ -40,7 +39,7 @@ suite('e2e/events', () => {
4039
apiKey: import.meta.env.VITE_BKT_API_KEY,
4140
featureTag: 'javascript',
4241
appVersion: '1.2.3',
43-
fetch: window.fetch,
42+
fetch: fetchLike,
4443
// DO NOT remove this line
4544
// Because the tests are asynchronous and share the same local storage,
4645
// It might fail randomly, having more or fewer events in the storage when checking the test.
@@ -57,7 +56,6 @@ suite('e2e/events', () => {
5756

5857
afterEach(() => {
5958
destroyBKTClient()
60-
localStorage.clear()
6159
})
6260

6361
test('goal event', async () => {
@@ -185,14 +183,13 @@ suite('e2e/events', () => {
185183

186184
test('Using a random string in the api key setting should throw Forbidden', async () => {
187185
destroyBKTClient()
188-
localStorage.clear()
189186

190187
config = defineBKTConfig({
191188
apiEndpoint: import.meta.env.VITE_BKT_API_ENDPOINT,
192189
apiKey: 'some-random-string',
193190
featureTag: 'javascript',
194191
appVersion: '1.2.3',
195-
fetch: window.fetch,
192+
fetch: fetchLike,
196193
})
197194

198195
user = defineBKTUser({
@@ -237,7 +234,7 @@ suite('e2e/events', () => {
237234
apiKey: import.meta.env.VITE_BKT_API_KEY,
238235
featureTag: 'javascript',
239236
appVersion: '1.2.3',
240-
fetch: window.fetch,
237+
fetch: fetchLike,
241238
})
242239

243240
await initializeBKTClient(config, user)
@@ -247,39 +244,45 @@ suite('e2e/events', () => {
247244
const component2 = getDefaultComponent(client)
248245

249246
const events3 = component2.dataModule.eventStorage().getAll()
250-
// 2 events - latency and response size
251-
expect(events3).toHaveLength(2)
252-
// ForbiddenError should not exist
253-
expect(
254-
events.some((e) => {
255-
return (
247+
248+
if (isNodeEnvironment) {
249+
// on the node environment, no events should be stored after destroying the client
250+
// because it's using in-memory storage
251+
expect(events3).toHaveLength(0)
252+
} else {
253+
// on the browser environment, we should have 2 events - latency and response size
254+
expect(events3).toHaveLength(2)
255+
// ForbiddenError should not exist
256+
expect(
257+
events3.some((e) => {
258+
return (
256259
e.type === EventType.METRICS &&
257260
e.event.event['@type'] === MetricsEventType.ForbiddenError &&
258261
e.event.event.apiId === ApiId.GET_EVALUATIONS &&
259262
e.event.sdkVersion === SDK_VERSION &&
260263
e.event.sourceId === SourceId.JAVASCRIPT
261-
)
262-
}),
263-
).toBe(false)
264+
)
265+
}),
266+
).toBe(false)
264267

265-
await client2.flush()
268+
await client2.flush()
266269

267-
const events4 = component2.dataModule.eventStorage().getAll()
270+
const events4 = component2.dataModule.eventStorage().getAll()
268271

269-
// error from /register_events does not get stored
270-
expect(events4).toHaveLength(0)
272+
// error from /register_events does not get stored
273+
expect(events4).toHaveLength(0)
274+
}
271275
})
272276

273277
test('Using a random string in the featureTag setting should not affect api request', async () => {
274278
destroyBKTClient()
275-
localStorage.clear()
276279

277280
config = defineBKTConfig({
278281
apiEndpoint: import.meta.env.VITE_BKT_API_ENDPOINT,
279282
apiKey: import.meta.env.VITE_BKT_API_KEY,
280283
featureTag: 'some-random-feature-tag',
281284
appVersion: '1.2.3',
282-
fetch: window.fetch,
285+
fetch: fetchLike,
283286
})
284287

285288
user = defineBKTUser({
@@ -291,16 +294,14 @@ suite('e2e/events', () => {
291294

292295
test('Timeout', async () => {
293296
// setting a very low value for the timeout
294-
295297
destroyBKTClient()
296-
localStorage.clear()
297298

298299
config = defineBKTConfig({
299300
apiEndpoint: import.meta.env.VITE_BKT_API_ENDPOINT,
300301
apiKey: import.meta.env.VITE_BKT_API_KEY,
301302
featureTag: 'javascript',
302303
appVersion: '1.2.3',
303-
fetch: window.fetch,
304+
fetch: fetchLike,
304305
})
305306

306307
user = defineBKTUser({

e2e/module.browser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export {
2+
destroyBKTClient,
3+
getBKTClient,
4+
initializeBKTClient,
5+
} from '../src/main.browser'

e2e/module.node.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export {
2+
destroyBKTClient,
3+
getBKTClient,
4+
initializeBKTClient,
5+
} from '../src/main'

e2e/module.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export {
2+
destroyBKTClient,
3+
getBKTClient,
4+
initializeBKTClient,
5+
} from '../src/main'

e2e/setup.browser.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
import { afterEach } from 'vitest'
3+
import { setFetchProvider } from './environment'
4+
5+
setFetchProvider(window.fetch)
6+
7+
afterEach(() => {
8+
localStorage.clear()
9+
})

e2e/setup.node.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { setFetchProvider, setIsNodeEnvironment } from './environment'
2+
3+
setFetchProvider(fetch)
4+
5+
setIsNodeEnvironment(true)

e2e/wrapperSdkSourceId.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { EventType } from '../src/internal/model/Event'
2424
import { TimeoutException } from '../src/BKTExceptions'
2525
import { ApiId, MetricsEventType } from '../src/internal/model/MetricsEventData'
2626
import { SourceId } from '../src/internal/model/SourceId'
27+
import { fetchLike } from './environment'
2728

2829
function getDefaultComponent(client: BKTClient): DefaultComponent {
2930
return (client as BKTClientImpl).component as DefaultComponent
@@ -39,7 +40,7 @@ suite('e2e/wrapper-sdk-source-id-and-version', () => {
3940
apiKey: import.meta.env.VITE_BKT_API_KEY,
4041
featureTag: 'javascript',
4142
appVersion: '1.2.3',
42-
fetch: window.fetch,
43+
fetch: fetchLike,
4344
// DO NOT remove this line
4445
// Because the tests are asynchronous and share the same local storage,
4546
// It might fail randomly, having more or fewer events in the storage when checking the test.
@@ -58,7 +59,6 @@ suite('e2e/wrapper-sdk-source-id-and-version', () => {
5859

5960
afterEach(() => {
6061
destroyBKTClient()
61-
localStorage.clear()
6262
})
6363

6464
test('goal event', async () => {
@@ -124,14 +124,13 @@ suite('e2e/wrapper-sdk-source-id-and-version', () => {
124124
// setting a very low value for the timeout
125125

126126
destroyBKTClient()
127-
localStorage.clear()
128127

129128
config = defineBKTConfig({
130129
apiEndpoint: import.meta.env.VITE_BKT_API_ENDPOINT,
131130
apiKey: import.meta.env.VITE_BKT_API_KEY,
132131
featureTag: 'javascript',
133132
appVersion: '1.2.3',
134-
fetch: window.fetch,
133+
fetch: fetchLike,
135134
wrapperSdkSourceId: SourceId.OPEN_FEATURE_JAVASCRIPT,
136135
wrapperSdkVersion: '2.2.3',
137136
})

0 commit comments

Comments
 (0)