-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathidentify.test.ts
More file actions
143 lines (120 loc) · 6.22 KB
/
identify.test.ts
File metadata and controls
143 lines (120 loc) · 6.22 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
137
138
139
140
141
142
143
import { mockLogger } from './helpers/mock-logger'
import { createPosthogInstance } from './helpers/posthog-instance'
import { uuidv7 } from '../uuidv7'
describe('identify', () => {
// Note that there are other tests for identify in posthog-core.identify.js
// These are in the old style of tests, if you are feeling helpful you could
// convert them to the new style in this file.
it('should persist the distinct_id', async () => {
// arrange
const token = uuidv7()
const posthog = await createPosthogInstance(token, { before_send: (cr) => cr })
const distinctId = '123'
// act
posthog.identify(distinctId)
// assert
expect(posthog.persistence!.properties()['$user_id']).toEqual(distinctId)
expect(mockLogger.error).toBeCalledTimes(0)
expect(mockLogger.warn).toBeCalledTimes(0)
})
it('should convert a numeric distinct_id to a string', async () => {
// arrange
const token = uuidv7()
const posthog = await createPosthogInstance(token, { before_send: (cr) => cr })
const distinctIdNum = 123
const distinctIdString = '123'
// act
posthog.identify(distinctIdNum as any)
// assert
expect(posthog.persistence!.properties()['$user_id']).toEqual(distinctIdString)
expect(mockLogger.error).toBeCalledTimes(0)
expect(mockLogger.warn).toBeCalledWith(
'The first argument to posthog.identify was a number, but it should be a string. It has been converted to a string.'
)
})
describe('invalid distinct_id', () => {
it.each([
['undefined', undefined, 'Unique user id has not been set in posthog.identify'],
['null', null, 'Unique user id has not been set in posthog.identify'],
['empty string', '', 'Unique user id has not been set in posthog.identify'],
['false', false, 'Unique user id has not been set in posthog.identify'],
])('should reject %s and log a critical error', async (_label, invalidId, expectedMessage) => {
const token = uuidv7()
const beforeSendMock = jest.fn().mockImplementation((e) => e)
const posthog = await createPosthogInstance(token, { before_send: beforeSendMock })
posthog.identify(invalidId as any)
expect(beforeSendMock).not.toHaveBeenCalled()
expect(mockLogger.critical).toHaveBeenCalledWith(expectedMessage)
})
it.each([
['the string "undefined"', 'undefined'],
['the string "null"', 'null'],
])('should accept %s as a distinct_id without strict_identify', async (_label, coercedId) => {
const token = uuidv7()
const beforeSendMock = jest.fn().mockImplementation((e) => e)
const posthog = await createPosthogInstance(token, { before_send: beforeSendMock })
posthog.identify(coercedId)
expect(beforeSendMock).toHaveBeenCalled()
expect(mockLogger.critical).not.toHaveBeenCalled()
})
it.each([
['undefined', undefined, 'Unique user id has not been set in posthog.identify'],
['null', null, 'Unique user id has not been set in posthog.identify'],
['empty string', '', 'Unique user id has not been set in posthog.identify'],
['false', false, 'Unique user id has not been set in posthog.identify'],
[
'the string "undefined"',
'undefined',
'The string "undefined" was set in posthog.identify which indicates an error. This ID should be unique to the user and not a hardcoded string.',
],
[
'the string "null"',
'null',
'The string "null" was set in posthog.identify which indicates an error. This ID should be unique to the user and not a hardcoded string.',
],
])('should reject %s and throw when strict_identify is true', async (_label, invalidId, expectedMessage) => {
const token = uuidv7()
const beforeSendMock = jest.fn().mockImplementation((e) => e)
const posthog = await createPosthogInstance(token, {
before_send: beforeSendMock,
strict_identify: true,
})
expect(() => posthog.identify(invalidId as any)).toThrow('[PostHog.js] ' + expectedMessage)
expect(beforeSendMock).not.toHaveBeenCalled()
})
it('should default strict_identify to true with the 2026-04-01 defaults', async () => {
const token = uuidv7()
const posthog = await createPosthogInstance(token, {
before_send: (cr) => cr,
defaults: '2026-04-01',
internal_or_test_user_hostname: null,
})
expect(posthog.config.strict_identify).toBe(true)
expect(() => posthog.identify('undefined')).toThrow('[PostHog.js]')
})
it('should default strict_identify to false without the 2026-04-01 defaults', async () => {
const token = uuidv7()
const posthog = await createPosthogInstance(token, { before_send: (cr) => cr })
expect(posthog.config.strict_identify).toBe(false)
})
})
it('should send $is_identified = true with the identify event and following events', async () => {
// arrange
const token = uuidv7()
const beforeSendMock = jest.fn().mockImplementation((e) => e)
const posthog = await createPosthogInstance(token, { before_send: beforeSendMock })
const distinctId = '123'
// act
posthog.capture('custom event before identify')
posthog.identify(distinctId)
posthog.capture('custom event after identify')
// assert
const eventBeforeIdentify = beforeSendMock.mock.calls[0]
expect(eventBeforeIdentify[0].properties.$is_identified).toEqual(false)
const identifyCall = beforeSendMock.mock.calls[1]
expect(identifyCall[0].event).toEqual('$identify')
expect(identifyCall[0].properties.$is_identified).toEqual(true)
const eventAfterIdentify = beforeSendMock.mock.calls[2]
expect(eventAfterIdentify[0].properties.$is_identified).toEqual(true)
})
})