Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Mar 7, 2024
1 parent fdd7e67 commit 51fcb3e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
"prettier": "prettier --write \"./**/*.{ts,tsx}\"",
"prettier:check": "prettier --check \"./**/*.{ts,tsx}\"",
"test": "jest posthog-core posthog-node posthog-web",
"test:all": "jest posthog-core posthog-node posthog-web && yarn test:rn",
"test:node": "jest posthog-node",
"test:core": "jest posthog-core",
"test:web": "jest posthog-core",
"test:rn": "cd posthog-react-native && yarn test",
"test:all": "jest posthog-core posthog-node posthog-web && yarn test:rn",
"build-rollup": "rollup -c",
"build-react-native": "cd posthog-react-native && yarn build"
},
Expand Down
17 changes: 6 additions & 11 deletions posthog-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,9 @@ export abstract class PostHogCoreStateless {

async flush(): Promise<any[]> {
if (!this.flushPromise) {
this.flushPromise = this._flush()
this.flushPromise = this._flush().finally(() => {
this.flushPromise = null
})
this.addPendingPromise(this.flushPromise)
}
return this.flushPromise
Expand Down Expand Up @@ -652,22 +654,15 @@ export abstract class PostHogCoreStateless {
)
}

async shutdownAsync(shutdownTimeoutMs?: number): Promise<void> {
async shutdownAsync(shutdownTimeoutMs: number = 30000): Promise<void> {
await this._initPromise

this.clearFlushTimer()

try {
await Promise.all(
Object.values(this.pendingPromises).map((x) =>
x.catch(() => {
// ignore errors as we are shutting down and can't deal with them anyways.
})
)
)
await Promise.all(Object.values(this.pendingPromises))

const timeout = shutdownTimeoutMs ?? 30000
const startTimeWithDelay = Date.now() + timeout
const startTimeWithDelay = Date.now() + shutdownTimeoutMs

while (true) {
const queue = this.getPersistedProperty<PostHogQueueItem[]>(PostHogPersistedProperty.Queue) || []
Expand Down
1 change: 0 additions & 1 deletion posthog-core/test/posthog.reset.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PostHogPersistedProperty } from '../src'
import { createTestClient, PostHogCoreTestClient, PostHogCoreTestClientMocks } from './test-utils/PostHogCoreTestClient'
import { waitForPromises } from './test-utils/test-utils'

describe('PostHog Core', () => {
let posthog: PostHogCoreTestClient
Expand Down
57 changes: 35 additions & 22 deletions posthog-node/test/posthog-node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ jest.mock('../package.json', () => ({ version: '1.2.3' }))

const mockedFetch = jest.mocked(fetch, true)

const waitForFlushTimer = async (): Promise<void> => {
await waitForPromises()
// To trigger the flush via the timer
jest.runOnlyPendingTimers()
// Then wait for the flush promise
await waitForPromises()
}

const getLastBatchEvents = (): any[] | undefined => {
expect(mockedFetch).toHaveBeenCalledWith('http://example.com/batch/', expect.objectContaining({ method: 'POST' }))

Expand All @@ -25,7 +33,6 @@ describe('PostHog Node.js', () => {
let posthog: PostHog

jest.useFakeTimers()
jest.setTimeout(1000)

beforeEach(() => {
posthog = new PostHog('TEST_API_KEY', {
Expand All @@ -44,6 +51,15 @@ describe('PostHog Node.js', () => {
})

afterEach(async () => {
mockedFetch.mockResolvedValue({
status: 200,
text: () => Promise.resolve('ok'),
json: () =>
Promise.resolve({
status: 'ok',
}),
} as any)

// ensure clean shutdown & no test interdependencies
await posthog.shutdownAsync()
})
Expand All @@ -53,8 +69,7 @@ describe('PostHog Node.js', () => {
expect(mockedFetch).toHaveBeenCalledTimes(0)
posthog.capture({ distinctId: '123', event: 'test-event', properties: { foo: 'bar' }, groups: { org: 123 } })

await waitForPromises()
jest.runOnlyPendingTimers()
await waitForFlushTimer()

const batchEvents = getLastBatchEvents()
expect(batchEvents).toEqual([
Expand All @@ -81,8 +96,7 @@ describe('PostHog Node.js', () => {
expect(mockedFetch).toHaveBeenCalledTimes(0)
posthog.capture({ distinctId: '123', event: 'test-event', properties: { foo: 'bar' }, groups: { org: 123 } })

await waitForPromises()
jest.runOnlyPendingTimers()
await waitForFlushTimer()
expect(getLastBatchEvents()?.[0]).toEqual(
expect.objectContaining({
distinct_id: '123',
Expand All @@ -104,8 +118,7 @@ describe('PostHog Node.js', () => {
groups: { other_group: 'x' },
})

await waitForPromises()
jest.runOnlyPendingTimers()
await waitForFlushTimer()
expect(getLastBatchEvents()?.[0]).toEqual(
expect.objectContaining({
distinct_id: '123',
Expand Down Expand Up @@ -143,7 +156,7 @@ describe('PostHog Node.js', () => {
it('should handle identify mistakenly using $set', async () => {
expect(mockedFetch).toHaveBeenCalledTimes(0)
posthog.identify({ distinctId: '123', properties: { foo: 'bar', $set: { foo: 'other' } } })
jest.runOnlyPendingTimers()
await waitForFlushTimer()
const batchEvents = getLastBatchEvents()
expect(batchEvents).toMatchObject([
{
Expand All @@ -162,7 +175,9 @@ describe('PostHog Node.js', () => {
it('should capture alias events on shared queue', async () => {
expect(mockedFetch).toHaveBeenCalledTimes(0)
posthog.alias({ distinctId: '123', alias: '1234' })
jest.runOnlyPendingTimers()
console.log('action')
await waitForFlushTimer()
console.log('assert')
const batchEvents = getLastBatchEvents()
expect(batchEvents).toMatchObject([
{
Expand All @@ -180,8 +195,7 @@ describe('PostHog Node.js', () => {
it('should allow overriding timestamp', async () => {
expect(mockedFetch).toHaveBeenCalledTimes(0)
posthog.capture({ event: 'custom-time', distinctId: '123', timestamp: new Date('2021-02-03') })
await waitForPromises()
jest.runOnlyPendingTimers()
await waitForFlushTimer()
const batchEvents = getLastBatchEvents()
expect(batchEvents).toMatchObject([
{
Expand All @@ -197,8 +211,7 @@ describe('PostHog Node.js', () => {
expect(mockedFetch).toHaveBeenCalledTimes(0)
const uuid = randomUUID()
posthog.capture({ event: 'custom-time', distinctId: '123', uuid })
await waitForPromises()
jest.runOnlyPendingTimers()
await waitForFlushTimer()
const batchEvents = getLastBatchEvents()
expect(batchEvents).toMatchObject([
{
Expand All @@ -220,8 +233,7 @@ describe('PostHog Node.js', () => {
disableGeoip: false,
})

await waitForPromises()
jest.runOnlyPendingTimers()
await waitForFlushTimer()
const batchEvents = getLastBatchEvents()
expect(batchEvents?.[0].properties).toEqual({
$groups: { org: 123 },
Expand All @@ -239,8 +251,9 @@ describe('PostHog Node.js', () => {
})
client.capture({ distinctId: '123', event: 'test-event', properties: { foo: 'bar' }, groups: { org: 123 } })

await waitForPromises()
jest.runOnlyPendingTimers()
await waitForFlushTimer()

console.log('asserting')

let batchEvents = getLastBatchEvents()
expect(batchEvents?.[0].properties).toEqual({
Expand All @@ -258,8 +271,8 @@ describe('PostHog Node.js', () => {
disableGeoip: true,
})

await waitForPromises()
jest.runOnlyPendingTimers()
await waitForFlushTimer()

batchEvents = getLastBatchEvents()
expect(batchEvents?.[0].properties).toEqual({
$groups: { org: 123 },
Expand All @@ -277,8 +290,9 @@ describe('PostHog Node.js', () => {
disableGeoip: false,
})

await waitForFlushTimer()
await waitForPromises()
jest.runOnlyPendingTimers()

batchEvents = getLastBatchEvents()
expect(batchEvents?.[0].properties).toEqual({
$groups: { org: 123 },
Expand Down Expand Up @@ -729,8 +743,7 @@ describe('PostHog Node.js', () => {
disableGeoip: false,
})

await waitForPromises()
jest.runOnlyPendingTimers()
await waitForFlushTimer()

expect(mockedFetch).toHaveBeenCalledWith(
'http://example.com/decide/?v=3',
Expand Down

0 comments on commit 51fcb3e

Please sign in to comment.