posthog-node
Advanced tools
Comparing version 2.5.0 to 2.5.1
@@ -0,1 +1,4 @@ | ||
# 2.5.1 - 2023-02-16 | ||
1. Make sure shutdown waits for pending promises to resolve. Fixes a problem with using PostHog Node in serverless environments. | ||
# 2.5.0 - 2023-02-15 | ||
@@ -2,0 +5,0 @@ |
@@ -106,2 +106,3 @@ /// <reference types="node" /> | ||
private removeDebugCallback?; | ||
private pendingPromises; | ||
private _optoutOverride; | ||
@@ -108,0 +109,0 @@ protected _events: SimpleEventEmitter; |
@@ -14,2 +14,3 @@ import { PostHogFetchOptions, PostHogFetchResponse, PostHogAutocaptureElement, PostHogDecideResponse, PosthogCoreOptions, PostHogEventProperties, PostHogPersistedProperty, PosthogCaptureOptions, JsonType } from './types'; | ||
private removeDebugCallback?; | ||
private pendingPromises; | ||
private _optoutOverride; | ||
@@ -16,0 +17,0 @@ protected _events: SimpleEventEmitter; |
{ | ||
"name": "posthog-node", | ||
"version": "2.5.0", | ||
"version": "2.5.1", | ||
"description": "PostHog Node.js integration", | ||
@@ -5,0 +5,0 @@ "repository": "PostHog/posthog-node", |
@@ -6,3 +6,3 @@ // import { PostHog } from '../' | ||
import { anyDecideCall, anyLocalEvalCall, apiImplementation } from './feature-flags.spec' | ||
import { waitForPromises } from '../../posthog-core/test/test-utils/test-utils' | ||
import { waitForPromises, wait } from '../../posthog-core/test/test-utils/test-utils' | ||
@@ -156,2 +156,59 @@ jest.mock('../package.json', () => ({ version: '1.2.3' })) | ||
describe('shutdown', () => { | ||
beforeEach(() => { | ||
// a serverless posthog configuration | ||
posthog = new PostHog('TEST_API_KEY', { | ||
host: 'http://example.com', | ||
flushAt: 1, | ||
flushInterval: 0, | ||
}) | ||
mockedFetch.mockImplementation(async () => { | ||
// simulate network delay | ||
await wait(500) | ||
return Promise.resolve({ | ||
status: 200, | ||
text: () => Promise.resolve('ok'), | ||
json: () => | ||
Promise.resolve({ | ||
status: 'ok', | ||
}), | ||
} as any) | ||
}) | ||
}) | ||
afterEach(() => { | ||
posthog.debug(false) | ||
}) | ||
it('should shutdown cleanly', async () => { | ||
const logSpy = jest.spyOn(global.console, 'log') | ||
jest.useRealTimers() | ||
// using debug mode to check console.log output | ||
// which tells us when the flush is complete | ||
posthog.debug(true) | ||
for (let i = 0; i < 10; i++) { | ||
posthog.capture({ event: 'test-event', distinctId: '123' }) | ||
// requests come 100ms apart | ||
await wait(100) | ||
} | ||
// 10 capture calls to debug log | ||
// 6 flush calls to debug log | ||
expect(logSpy).toHaveBeenCalledTimes(16) | ||
expect(10).toEqual(logSpy.mock.calls.filter((call) => call[1].includes('capture')).length) | ||
expect(6).toEqual(logSpy.mock.calls.filter((call) => call[1].includes('flush')).length) | ||
logSpy.mockClear() | ||
await posthog.shutdownAsync() | ||
// remaining 4 flush calls to debug log | ||
// happen during shutdown | ||
expect(4).toEqual(logSpy.mock.calls.filter((call) => call[1].includes('flush')).length) | ||
jest.useFakeTimers() | ||
logSpy.mockRestore() | ||
}) | ||
}) | ||
describe('groupIdentify', () => { | ||
@@ -158,0 +215,0 @@ it('should identify group with unique id', () => { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
778815
10969