posthog-node 2.5.0 → 2.5.1

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.
@@ -11,6 +11,7 @@ export declare abstract class PostHogCoreStateless {
11
11
  private requestTimeout;
12
12
  private captureMode;
13
13
  private removeDebugCallback?;
14
+ private pendingPromises;
14
15
  private _optoutOverride;
15
16
  protected _events: SimpleEventEmitter;
16
17
  protected _flushTimer?: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-node",
3
- "version": "2.5.0",
3
+ "version": "2.5.1",
4
4
  "description": "PostHog Node.js integration",
5
5
  "repository": "PostHog/posthog-node",
6
6
  "scripts": {
@@ -3,7 +3,7 @@ import { PostHog as PostHog } from '../src/posthog-node'
3
3
  jest.mock('../src/fetch')
4
4
  import { fetch } from '../src/fetch'
5
5
  import { anyDecideCall, anyLocalEvalCall, apiImplementation } from './feature-flags.spec'
6
- import { waitForPromises } from '../../posthog-core/test/test-utils/test-utils'
6
+ import { waitForPromises, wait } from '../../posthog-core/test/test-utils/test-utils'
7
7
 
8
8
  jest.mock('../package.json', () => ({ version: '1.2.3' }))
9
9
 
@@ -152,6 +152,63 @@ describe('PostHog Node.js', () => {
152
152
  })
153
153
  })
154
154
 
155
+ describe('shutdown', () => {
156
+ beforeEach(() => {
157
+ // a serverless posthog configuration
158
+ posthog = new PostHog('TEST_API_KEY', {
159
+ host: 'http://example.com',
160
+ flushAt: 1,
161
+ flushInterval: 0,
162
+ })
163
+
164
+ mockedFetch.mockImplementation(async () => {
165
+ // simulate network delay
166
+ await wait(500)
167
+
168
+ return Promise.resolve({
169
+ status: 200,
170
+ text: () => Promise.resolve('ok'),
171
+ json: () =>
172
+ Promise.resolve({
173
+ status: 'ok',
174
+ }),
175
+ } as any)
176
+ })
177
+ })
178
+
179
+ afterEach(() => {
180
+ posthog.debug(false)
181
+ })
182
+
183
+ it('should shutdown cleanly', async () => {
184
+ const logSpy = jest.spyOn(global.console, 'log')
185
+ jest.useRealTimers()
186
+ // using debug mode to check console.log output
187
+ // which tells us when the flush is complete
188
+ posthog.debug(true)
189
+ for (let i = 0; i < 10; i++) {
190
+ posthog.capture({ event: 'test-event', distinctId: '123' })
191
+ // requests come 100ms apart
192
+ await wait(100)
193
+ }
194
+
195
+ // 10 capture calls to debug log
196
+ // 6 flush calls to debug log
197
+ expect(logSpy).toHaveBeenCalledTimes(16)
198
+ expect(10).toEqual(logSpy.mock.calls.filter((call) => call[1].includes('capture')).length)
199
+ expect(6).toEqual(logSpy.mock.calls.filter((call) => call[1].includes('flush')).length)
200
+
201
+ logSpy.mockClear()
202
+
203
+ await posthog.shutdownAsync()
204
+ // remaining 4 flush calls to debug log
205
+ // happen during shutdown
206
+ expect(4).toEqual(logSpy.mock.calls.filter((call) => call[1].includes('flush')).length)
207
+ jest.useFakeTimers()
208
+ logSpy.mockRestore()
209
+ })
210
+ })
211
+
155
212
  describe('groupIdentify', () => {
156
213
  it('should identify group with unique id', () => {
157
214
  posthog.groupIdentify({ groupType: 'posthog', groupKey: 'team-1', properties: { analytics: true } })