@posthog/core 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/error-tracking/error-properties-builder.d.ts +6 -6
- package/dist/error-tracking/error-properties-builder.d.ts.map +1 -1
- package/dist/error-tracking/error-properties-builder.js +7 -13
- package/dist/error-tracking/error-properties-builder.mjs +5 -11
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -20
- package/dist/index.mjs +1 -2
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/error-tracking/error-properties-builder.ts +14 -16
- package/src/index.ts +0 -1
- package/src/types.ts +1 -1
- package/src/__tests__/featureFlagUtils.spec.ts +0 -427
- package/src/__tests__/gzip.spec.ts +0 -69
- package/src/__tests__/posthog.ai.spec.ts +0 -110
- package/src/__tests__/posthog.capture.spec.ts +0 -91
- package/src/__tests__/posthog.core.spec.ts +0 -135
- package/src/__tests__/posthog.debug.spec.ts +0 -36
- package/src/__tests__/posthog.enqueue.spec.ts +0 -93
- package/src/__tests__/posthog.featureflags.spec.ts +0 -1106
- package/src/__tests__/posthog.featureflags.v1.spec.ts +0 -922
- package/src/__tests__/posthog.flush.spec.ts +0 -237
- package/src/__tests__/posthog.gdpr.spec.ts +0 -50
- package/src/__tests__/posthog.groups.spec.ts +0 -96
- package/src/__tests__/posthog.identify.spec.ts +0 -194
- package/src/__tests__/posthog.init.spec.ts +0 -110
- package/src/__tests__/posthog.listeners.spec.ts +0 -51
- package/src/__tests__/posthog.register.spec.ts +0 -47
- package/src/__tests__/posthog.reset.spec.ts +0 -76
- package/src/__tests__/posthog.sessions.spec.ts +0 -63
- package/src/__tests__/posthog.setProperties.spec.ts +0 -102
- package/src/__tests__/posthog.shutdown.spec.ts +0 -88
- package/src/__tests__/utils.spec.ts +0 -36
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { createTestClient, PostHogCoreTestClient, PostHogCoreTestClientMocks } from '@/testing'
|
|
2
|
-
|
|
3
|
-
describe('PostHog Core', () => {
|
|
4
|
-
let posthog: PostHogCoreTestClient
|
|
5
|
-
let mocks: PostHogCoreTestClientMocks
|
|
6
|
-
|
|
7
|
-
jest.useFakeTimers()
|
|
8
|
-
jest.setSystemTime(new Date('2022-01-01'))
|
|
9
|
-
|
|
10
|
-
const errorAPIResponse = Promise.resolve({
|
|
11
|
-
status: 400,
|
|
12
|
-
text: () => Promise.resolve('error'),
|
|
13
|
-
json: () =>
|
|
14
|
-
Promise.resolve({
|
|
15
|
-
status: 'error',
|
|
16
|
-
}),
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
describe('getFlags', () => {
|
|
20
|
-
beforeEach(() => {
|
|
21
|
-
;[posthog, mocks] = createTestClient('TEST_API_KEY', { flushAt: 1 })
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
it('should handle successful v1 response and return normalized response', async () => {
|
|
25
|
-
const mockV1Response = {
|
|
26
|
-
featureFlags: { 'test-flag': true },
|
|
27
|
-
featureFlagPayloads: { 'test-flag': { a: 'payload' } },
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const expectedResponse = {
|
|
31
|
-
...mockV1Response,
|
|
32
|
-
flags: {
|
|
33
|
-
'test-flag': {
|
|
34
|
-
key: 'test-flag',
|
|
35
|
-
enabled: true,
|
|
36
|
-
variant: undefined,
|
|
37
|
-
reason: undefined,
|
|
38
|
-
metadata: {
|
|
39
|
-
id: undefined,
|
|
40
|
-
version: undefined,
|
|
41
|
-
description: undefined,
|
|
42
|
-
payload: '{"a":"payload"}',
|
|
43
|
-
},
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
mocks.fetch.mockImplementation((url) => {
|
|
49
|
-
if (url.includes('/flags/?v=2&config=true')) {
|
|
50
|
-
return Promise.resolve({
|
|
51
|
-
status: 200,
|
|
52
|
-
text: () => Promise.resolve('ok'),
|
|
53
|
-
json: () => Promise.resolve(mockV1Response),
|
|
54
|
-
})
|
|
55
|
-
}
|
|
56
|
-
return errorAPIResponse
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
const response = await posthog.getFlags('test-distinct-id')
|
|
60
|
-
expect(response).toEqual(expectedResponse)
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
it('should handle successful v4 response and return normalized response', async () => {
|
|
64
|
-
const mockV4Response = {
|
|
65
|
-
flags: {
|
|
66
|
-
'test-flag': {
|
|
67
|
-
key: 'test-flag',
|
|
68
|
-
enabled: true,
|
|
69
|
-
variant: 'test-payload',
|
|
70
|
-
reason: {
|
|
71
|
-
code: 'matched_condition',
|
|
72
|
-
description: 'matched condition set 1',
|
|
73
|
-
condition_index: 0,
|
|
74
|
-
},
|
|
75
|
-
metadata: {
|
|
76
|
-
id: 1,
|
|
77
|
-
version: 1,
|
|
78
|
-
description: 'test-flag',
|
|
79
|
-
payload: '{"a":"payload"}',
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const expectedResponse = {
|
|
86
|
-
...mockV4Response,
|
|
87
|
-
featureFlags: { 'test-flag': 'test-payload' },
|
|
88
|
-
featureFlagPayloads: { 'test-flag': { a: 'payload' } },
|
|
89
|
-
}
|
|
90
|
-
mocks.fetch.mockImplementation((url) => {
|
|
91
|
-
if (url.includes('/flags/?v=2&config=true')) {
|
|
92
|
-
return Promise.resolve({
|
|
93
|
-
status: 200,
|
|
94
|
-
text: () => Promise.resolve('ok'),
|
|
95
|
-
json: () => Promise.resolve(mockV4Response),
|
|
96
|
-
})
|
|
97
|
-
}
|
|
98
|
-
return errorAPIResponse
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
const response = await posthog.getFlags('test-distinct-id')
|
|
102
|
-
expect(response).toEqual(expectedResponse)
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
it('should handle error response', async () => {
|
|
106
|
-
mocks.fetch.mockImplementation((url) => {
|
|
107
|
-
if (url.includes('/flags/?v=2&config=true')) {
|
|
108
|
-
return Promise.resolve({
|
|
109
|
-
status: 400,
|
|
110
|
-
text: () => Promise.resolve('error'),
|
|
111
|
-
json: () => Promise.resolve({ error: 'went wrong' }),
|
|
112
|
-
})
|
|
113
|
-
}
|
|
114
|
-
return errorAPIResponse
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
const response = await posthog.getFlags('test-distinct-id')
|
|
118
|
-
expect(response).toBeUndefined()
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
it('should handle network errors', async () => {
|
|
122
|
-
const emitSpy = jest.spyOn(posthog['_events'], 'emit')
|
|
123
|
-
mocks.fetch.mockImplementation((url) => {
|
|
124
|
-
if (url.includes('/flags/?v=2&config=true')) {
|
|
125
|
-
return Promise.reject(new Error('Network error'))
|
|
126
|
-
}
|
|
127
|
-
return errorAPIResponse
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
const response = await posthog.getFlags('test-distinct-id')
|
|
131
|
-
expect(response).toBeUndefined()
|
|
132
|
-
expect(emitSpy).toHaveBeenCalledWith('error', expect.any(Error))
|
|
133
|
-
})
|
|
134
|
-
})
|
|
135
|
-
})
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { createTestClient, PostHogCoreTestClient, PostHogCoreTestClientMocks } from '@/testing'
|
|
2
|
-
|
|
3
|
-
describe('PostHog Core', () => {
|
|
4
|
-
let posthog: PostHogCoreTestClient
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
6
|
-
let mocks: PostHogCoreTestClientMocks
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
;[posthog, mocks] = createTestClient('TEST_API_KEY', {})
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
describe('debug', () => {
|
|
13
|
-
it('should log emitted events when enabled', () => {
|
|
14
|
-
const spy = jest.spyOn(console, 'log')
|
|
15
|
-
|
|
16
|
-
posthog.capture('test-event1')
|
|
17
|
-
expect(spy).toHaveBeenCalledTimes(0)
|
|
18
|
-
|
|
19
|
-
posthog.debug()
|
|
20
|
-
posthog.capture('test-event1')
|
|
21
|
-
expect(spy).toHaveBeenCalledTimes(1)
|
|
22
|
-
expect(spy).toHaveBeenCalledWith(
|
|
23
|
-
'PostHog Debug',
|
|
24
|
-
'capture',
|
|
25
|
-
expect.objectContaining({
|
|
26
|
-
event: 'test-event1',
|
|
27
|
-
})
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
spy.mockReset()
|
|
31
|
-
posthog.debug(false)
|
|
32
|
-
posthog.capture('test-event1')
|
|
33
|
-
expect(spy).toHaveBeenCalledTimes(0)
|
|
34
|
-
})
|
|
35
|
-
})
|
|
36
|
-
})
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { PostHogPersistedProperty } from '@/types'
|
|
2
|
-
import { createTestClient, PostHogCoreTestClient, PostHogCoreTestClientMocks } from '@/testing'
|
|
3
|
-
|
|
4
|
-
describe('PostHog Core', () => {
|
|
5
|
-
let posthog: PostHogCoreTestClient
|
|
6
|
-
let mocks: PostHogCoreTestClientMocks
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
jest.setSystemTime(new Date('2022-01-01'))
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
function createSut(maxQueueSize: number = 1000, flushAt: number = 20): void {
|
|
13
|
-
;[posthog, mocks] = createTestClient('TEST_API_KEY', {
|
|
14
|
-
maxQueueSize: maxQueueSize,
|
|
15
|
-
flushAt: flushAt,
|
|
16
|
-
})
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
describe('enqueue', () => {
|
|
20
|
-
it('should add a message to the queue', () => {
|
|
21
|
-
createSut()
|
|
22
|
-
|
|
23
|
-
posthog.capture('type', {
|
|
24
|
-
foo: 'bar',
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Queue)).toHaveLength(1)
|
|
28
|
-
|
|
29
|
-
const item = posthog.getPersistedProperty<any[]>(PostHogPersistedProperty.Queue)?.pop()
|
|
30
|
-
|
|
31
|
-
expect(item).toMatchObject({
|
|
32
|
-
message: {
|
|
33
|
-
library: 'posthog-core-tests',
|
|
34
|
-
library_version: '2.0.0-alpha',
|
|
35
|
-
type: 'capture',
|
|
36
|
-
properties: {
|
|
37
|
-
foo: 'bar',
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
expect(mocks.fetch).not.toHaveBeenCalled()
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
it('should delete oldest message if queue is full', () => {
|
|
46
|
-
createSut(2, 2)
|
|
47
|
-
|
|
48
|
-
posthog.capture('type1', {
|
|
49
|
-
foo: 'bar',
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
posthog.capture('type2', {
|
|
53
|
-
foo: 'bar',
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
posthog.capture('type3', {
|
|
57
|
-
foo: 'bar',
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Queue)).toHaveLength(2)
|
|
61
|
-
|
|
62
|
-
let item = posthog.getPersistedProperty<any[]>(PostHogPersistedProperty.Queue)?.pop()
|
|
63
|
-
|
|
64
|
-
expect(item).toMatchObject({
|
|
65
|
-
message: {
|
|
66
|
-
library: 'posthog-core-tests',
|
|
67
|
-
library_version: '2.0.0-alpha',
|
|
68
|
-
type: 'capture',
|
|
69
|
-
properties: {
|
|
70
|
-
foo: 'bar',
|
|
71
|
-
},
|
|
72
|
-
event: 'type3',
|
|
73
|
-
},
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
item = posthog.getPersistedProperty<any[]>(PostHogPersistedProperty.Queue)?.pop()
|
|
77
|
-
|
|
78
|
-
expect(item).toMatchObject({
|
|
79
|
-
message: {
|
|
80
|
-
library: 'posthog-core-tests',
|
|
81
|
-
library_version: '2.0.0-alpha',
|
|
82
|
-
type: 'capture',
|
|
83
|
-
properties: {
|
|
84
|
-
foo: 'bar',
|
|
85
|
-
},
|
|
86
|
-
event: 'type2',
|
|
87
|
-
},
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
expect(mocks.fetch).not.toHaveBeenCalled()
|
|
91
|
-
})
|
|
92
|
-
})
|
|
93
|
-
})
|