Skip to content

Commit

Permalink
chore: move posthog test instance helper (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra authored Feb 6, 2024
1 parent 560c63a commit cd08c97
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/library-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
node-version: '18'
cache: 'pnpm'
- run: pnpm install
- run: pnpm jest functional_tests/
- run: pnpm run test:functional

lint:
name: Lint
Expand Down
2 changes: 1 addition & 1 deletion functional_tests/feature-flags.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { v4 } from 'uuid'
import { createPosthogInstance } from './posthog-instance'
import { createPosthogInstance } from '../src/__tests__/helpers/posthog-instance'
import { waitFor } from '@testing-library/dom'
import { getRequests, resetRequests } from './mock-server'

Expand Down
2 changes: 1 addition & 1 deletion functional_tests/identify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'regenerator-runtime/runtime'
import { waitFor } from '@testing-library/dom'
import { v4 } from 'uuid'
import { getRequests } from './mock-server'
import { createPosthogInstance } from './posthog-instance'
import { createPosthogInstance } from '../src/__tests__/helpers/posthog-instance'
import { logger } from '../src/utils/logger'
jest.mock('../src/utils/logger')
test('identify sends a identify event', async () => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"lint": "eslint src",
"prettier": "prettier --write src/ functional_tests/",
"prepublishOnly": "pnpm lint && pnpm test && pnpm build && pnpm test:react",
"test": "pnpm test:unit && pnpm test:custom-eslint-rules",
"test": "pnpm test:unit && pnpm test:custom-eslint-rules && pnpm test:functional",
"test:unit": "jest src",
"test:custom-eslint-rules": "jest eslint-rules",
"test:react": "cd react; pnpm test",
"test:functional": "jest functional_tests",
"test-watch": "jest --watch src",
"cypress": "cypress open",
"prepare": "husky install"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
// The library depends on having the module initialized before it can be used.

import { v4 } from 'uuid'
import { PostHog, init_as_module } from '../src/posthog-core'
import { PostHog, init_as_module } from '../../posthog-core'
import 'regenerator-runtime/runtime'
import { PostHogConfig } from '../src/types'
import { PostHogConfig } from '../../types'

// It sets a global variable that is set and used to initialize subsequent libaries.
beforeAll(() => init_as_module())

export const createPosthogInstance = async (token: string = v4(), config: Partial<PostHogConfig> = {}) => {
export const createPosthogInstance = async (
token: string = v4(),
config: Partial<PostHogConfig> = {}
): Promise<PostHog> => {
// We need to create a new instance of the library for each test, to ensure
// that they are isolated from each other. The way the library is currently
// written, we first create an instance, then call init on it which then
// creates another instance.
const posthog = new PostHog()

// eslint-disable-next-line compat/compat
return await new Promise<PostHog>((resolve) =>
posthog.init(
// Use a random UUID for the token, such that we don't have to worry
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/identify.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { v4 } from 'uuid'
import { createPosthogInstance } from '../../functional_tests/posthog-instance'
import { createPosthogInstance } from './helpers/posthog-instance'
import { logger } from '../utils/logger'
jest.mock('../utils/logger')

Expand Down

0 comments on commit cd08c97

Please sign in to comment.