diff --git a/package.json b/package.json index 56efc1c7e..1a5ec6d25 100644 --- a/package.json +++ b/package.json @@ -13,12 +13,13 @@ "lint": "eslint src && eslint cypress", "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 && pnpm test:functional", + "test": "pnpm typecheck && 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", + "typecheck": "tsc --noEmit --project tsconfig.test.json", "cypress": "cypress open", "prepare": "husky install", "deprecate-old-versions": "node scripts/deprecate-old-versions.mjs" diff --git a/src/__tests__/autocapture-utils.test.ts b/src/__tests__/autocapture-utils.test.ts index cb2e354d6..59c2ffb8e 100644 --- a/src/__tests__/autocapture-utils.test.ts +++ b/src/__tests__/autocapture-utils.test.ts @@ -16,6 +16,7 @@ import { } from '../autocapture-utils' import { document } from '../utils/globals' import { makeMouseEvent } from './autocapture.test' +import { AutocaptureConfig } from '../types' describe(`Autocapture utility functions`, () => { afterEach(() => { @@ -252,7 +253,9 @@ describe(`Autocapture utility functions`, () => { true, ], ])('correctly respects the allow list: %s', (_, clickTarget, autoCaptureConfig, shouldCapture) => { - expect(shouldCaptureDomEvent(clickTarget, makeMouseEvent({}), autoCaptureConfig)).toBe(shouldCapture) + expect( + shouldCaptureDomEvent(clickTarget, makeMouseEvent({}), autoCaptureConfig as AutocaptureConfig) + ).toBe(shouldCapture) }) }) }) diff --git a/src/__tests__/autocapture.test.ts b/src/__tests__/autocapture.test.ts index 9458381f7..ff540f84e 100644 --- a/src/__tests__/autocapture.test.ts +++ b/src/__tests__/autocapture.test.ts @@ -400,8 +400,6 @@ describe('Autocapture system', () => { it('should capture copy', () => { const fakeEvent = makeCopyEvent({ target: elTarget, - clientX: 5, - clientY: 5, }) setWindowTextSelection('copy this test') @@ -417,8 +415,6 @@ describe('Autocapture system', () => { it('should capture cut', () => { const fakeEvent = makeCutEvent({ target: elTarget, - clientX: 5, - clientY: 5, }) setWindowTextSelection('cut this test') @@ -435,8 +431,6 @@ describe('Autocapture system', () => { it('ignores empty selection', () => { const fakeEvent = makeCopyEvent({ target: elTarget, - clientX: 5, - clientY: 5, }) setWindowTextSelection('') @@ -450,8 +444,6 @@ describe('Autocapture system', () => { it('runs selection through the safe text before capture', () => { const fakeEvent = makeCopyEvent({ target: elTarget, - clientX: 5, - clientY: 5, }) // oh no, a social security number! diff --git a/src/__tests__/featureflags.ts b/src/__tests__/featureflags.ts index d97f92db4..1531e2d39 100644 --- a/src/__tests__/featureflags.ts +++ b/src/__tests__/featureflags.ts @@ -8,8 +8,8 @@ jest.useFakeTimers() jest.spyOn(global, 'setTimeout') describe('featureflags', () => { - let instance - let featureFlags + let instance: any + let featureFlags: any const config = { token: 'random fake token', diff --git a/src/__tests__/posthog-core.test.ts b/src/__tests__/posthog-core.test.ts index cdbeba835..f06458bef 100644 --- a/src/__tests__/posthog-core.test.ts +++ b/src/__tests__/posthog-core.test.ts @@ -55,7 +55,7 @@ describe('posthog core', () => { }) // act - const actual = posthog._calculate_event_properties(eventName, eventProperties) + const actual = posthog._calculate_event_properties(eventName, eventProperties, new Date()) // assert expect(actual['event']).toBe('prop') diff --git a/src/__tests__/posthog-core.ts b/src/__tests__/posthog-core.ts index 2ce103037..0fee2c072 100644 --- a/src/__tests__/posthog-core.ts +++ b/src/__tests__/posthog-core.ts @@ -27,8 +27,11 @@ describe('posthog core', () => { _send_request: jest.fn(), } - const posthogWith = (config: Partial, overrides?: Partial) => { + const posthogWith = (config: Partial, overrides?: Partial): PostHog => { const posthog = defaultPostHog().init('testtoken', config, uuidv7()) + if (!posthog) { + throw new Error('PostHog failed to initialize') + } return Object.assign(posthog, overrides || {}) } @@ -47,7 +50,7 @@ describe('posthog core', () => { }) it('adds system time to events', () => { - const captureData = posthogWith(defaultConfig, defaultOverrides).capture(eventName, {}, {}) + const captureData = posthogWith(defaultConfig, defaultOverrides).capture(eventName, {}, {})! expect(captureData).toHaveProperty('timestamp') // timer is fixed at 2020-01-01 @@ -59,7 +62,7 @@ describe('posthog core', () => { eventName, {}, { timestamp: new Date(2020, 0, 2, 12, 34) } - ) + )! expect(captureData).toHaveProperty('timestamp') expect(captureData.timestamp).toEqual(new Date(2020, 0, 2, 12, 34)) expect(captureData.properties['$event_time_override_provided']).toEqual(true) @@ -115,8 +118,8 @@ describe('posthog core', () => { posthog.capture(eventName, {}, {}) - expect(posthog.sessionPersistence.update_campaign_params).toHaveBeenCalled() - expect(posthog.sessionPersistence.update_referrer_info).toHaveBeenCalled() + expect(posthog.sessionPersistence?.update_campaign_params).toHaveBeenCalled() + expect(posthog.sessionPersistence?.update_referrer_info).toHaveBeenCalled() }) it('errors with undefined event name', () => { diff --git a/src/autocapture.ts b/src/autocapture.ts index dbef30693..14880f75b 100644 --- a/src/autocapture.ts +++ b/src/autocapture.ts @@ -57,7 +57,7 @@ export class Autocapture { return config } - private _addDomEventHandlers(): void { + _addDomEventHandlers(): void { if (!this.isBrowserSupported()) { logger.info('Disabling Automatic Event Collection because this browser is not supported') return diff --git a/src/posthog-core.ts b/src/posthog-core.ts index 16f21e721..d77d85896 100644 --- a/src/posthog-core.ts +++ b/src/posthog-core.ts @@ -234,6 +234,7 @@ class DeprecatedWebPerformanceObserver { export class PostHog { __loaded: boolean config: PostHogConfig + lib_version: string rateLimiter: RateLimiter scrollManager: ScrollManager @@ -285,6 +286,7 @@ export class PostHog { this.__request_queue = [] this.__loaded = false this.analyticsDefaultEndpoint = '/e/' + this.lib_version = Config.LIB_VERSION this.featureFlags = new PostHogFeatureFlags(this) this.toolbar = new Toolbar(this) diff --git a/tsconfig.test.json b/tsconfig.test.json new file mode 100644 index 000000000..69cb9894a --- /dev/null +++ b/tsconfig.test.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "strict": false, + "noImplicitAny": false + }, + "include": ["src/**/*.ts*", "src/__tests__/**/*.ts*"], + "exclude": [] +}