Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Run ts on test files #1358

Merged
merged 7 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"test:react": "cd react; pnpm test",
"test:functional": "jest functional_tests",
"test-watch": "jest --watch src",
"test:typecheck": "cd src/__tests__ && tsc --noEmit --project tsconfig.json",
"typecheck": "tsc --noEmit --project tsconfig.json",
"cypress": "cypress open",
"prepare": "husky install",
"deprecate-old-versions": "node scripts/deprecate-old-versions.mjs"
Expand All @@ -33,6 +35,7 @@
"react/package.json"
],
"dependencies": {
"@types/web": "^0.0.154",
"fflate": "^0.4.8",
"preact": "^10.19.3",
"web-vitals": "^4.0.1"
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/__tests__/autocapture-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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)
})
})
})
Expand Down
8 changes: 0 additions & 8 deletions src/__tests__/autocapture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,6 @@ describe('Autocapture system', () => {
it('should capture copy', () => {
const fakeEvent = makeCopyEvent({
target: elTarget,
clientX: 5,
clientY: 5,
})

setWindowTextSelection('copy this test')
Expand All @@ -417,8 +415,6 @@ describe('Autocapture system', () => {
it('should capture cut', () => {
const fakeEvent = makeCutEvent({
target: elTarget,
clientX: 5,
clientY: 5,
})

setWindowTextSelection('cut this test')
Expand All @@ -435,8 +431,6 @@ describe('Autocapture system', () => {
it('ignores empty selection', () => {
const fakeEvent = makeCopyEvent({
target: elTarget,
clientX: 5,
clientY: 5,
})

setWindowTextSelection('')
Expand All @@ -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!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import {
errorToProperties,
ErrorProperties,
unhandledRejectionToProperties,
} from '../../../extensions/exception-autocapture/error-conversion'

import { isNull } from '../../../utils/type-utils'
import { expect } from '@jest/globals'
import { ErrorProperties } from '../../../types'

// ugh, jest
// can't reference PromiseRejectionEvent to construct it 🤷
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/extensions/replay/sessionrecording-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
splitBuffer,
SEVEN_MEGABYTES,
estimateSize,
circularReferenceReplacer,
} from '../../../extensions/replay/sessionrecording-utils'
import { largeString, threeMBAudioURI, threeMBImageURI } from '../test_data/sessionrecording-utils-test-data'
import { eventWithTime } from '@rrweb/types'
Expand Down Expand Up @@ -333,4 +334,22 @@ describe(`SessionRecording utility functions`, () => {
expect(result).toEqual([buffer])
})
})

describe('circularReferenceReplacer', () => {
it('should handle circular references', () => {
const obj: any = {}
obj.obj = obj
const result = JSON.stringify(obj, circularReferenceReplacer())
expect(result).toEqual('{"obj":"[Circular]"}')
})

it('should handle nested circular references', () => {
const a: any = {}
const b: any = {}
a.b = b
b.a = a
const result = JSON.stringify(a, circularReferenceReplacer())
expect(result).toEqual('{"b":{"a":"[Circular]"}}')
})
})
})
18 changes: 11 additions & 7 deletions src/__tests__/featureflags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { filterActiveFeatureFlags, parseFeatureFlagDecideResponse, PostHogFeatureFlags } from '../posthog-featureflags'
import { PostHogPersistence } from '../posthog-persistence'
import { RequestRouter } from '../utils/request-router'
import { PostHogConfig } from '../types'

jest.useFakeTimers()
jest.spyOn(global, 'setTimeout')
Expand All @@ -15,15 +16,17 @@ describe('featureflags', () => {
token: 'random fake token',
persistence: 'memory',
api_host: 'https://app.posthog.com',
}
} as PostHogConfig

let mockWarn

beforeEach(() => {
instance = {
config,
get_distinct_id: () => 'blah id',
getGroups: () => {},
persistence: new PostHogPersistence(config),
requestRouter: new RequestRouter({ config }),
requestRouter: new RequestRouter({ config } as any),
register: (props) => instance.persistence.register(props),
unregister: (key) => instance.persistence.unregister(key),
get_property: (key) => instance.persistence.props[key],
Expand All @@ -40,8 +43,8 @@ describe('featureflags', () => {

featureFlags = new PostHogFeatureFlags(instance)

jest.spyOn(instance, 'capture').mockReturnValue()
jest.spyOn(window.console, 'warn').mockImplementation()
jest.spyOn(instance, 'capture').mockReturnValue(undefined)
mockWarn = jest.spyOn(window.console, 'warn').mockImplementation()

instance.persistence.register({
$feature_flag_payloads: {
Expand Down Expand Up @@ -76,7 +79,7 @@ describe('featureflags', () => {
})

it('should warn if decide endpoint was not hit and no flags exist', () => {
window.POSTHOG_DEBUG = true
;(window as any).POSTHOG_DEBUG = true
featureFlags.instance.decideEndpointWasHit = false
instance.persistence.unregister('$enabled_feature_flags')
instance.persistence.unregister('$active_feature_flags')
Expand All @@ -88,7 +91,7 @@ describe('featureflags', () => {
'isFeatureEnabled for key "beta-feature" failed. Feature flags didn\'t load in time.'
)

window.console.warn.mockClear()
mockWarn.mockClear()

expect(featureFlags.getFeatureFlag('beta-feature')).toEqual(undefined)
expect(window.console.warn).toHaveBeenCalledWith(
Expand Down Expand Up @@ -249,7 +252,7 @@ describe('featureflags', () => {
)

// Clear the mock to reset call count
window.console.warn.mockClear()
mockWarn.mockClear()

// Test with suppressing warning (new behavior)
featureFlags.override(
Expand Down Expand Up @@ -1031,6 +1034,7 @@ describe('parseFeatureFlagDecideResponse', () => {
// checks that nothing fails when asking for ?v=2 and getting a ?v=1 response
const decideResponse = { featureFlags: ['beta-feature', 'alpha-feature-2'] }

// @ts-expect-error testing backwards compatibility
parseFeatureFlagDecideResponse(decideResponse, persistence)

expect(persistence.register).toHaveBeenLastCalledWith({
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/helpers/script-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const checkScriptsForSrcExists = (src: string): boolean => {
const scripts = document.querySelectorAll('body > script')
let foundScript = false
for (let i = 0; i < scripts.length; i++) {
if (scripts[i].src === src) {
if ((scripts[i] as any).src === src) {
foundScript = true
break
}
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/posthog-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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')
Expand Down
Loading
Loading