Skip to content

Commit

Permalink
fix: improve ES6 bundling (#1542)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra authored Nov 20, 2024
1 parent 56d1d55 commit 4f1a2bc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 38 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/es-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:

jobs:
ssr:
name: Cypress
name: Build and check ES5/ES6 support
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -21,3 +21,6 @@ jobs:

- name: Run es-check to check if our ie11 bundle is ES5 compatible
run: npx es-check@7.2.1 es5 dist/array.full.es5.js

- name: Run es-check to check if our main bundle is ES6 compatible
run: npx es-check@7.2.1 es6 dist/array.full.js
19 changes: 18 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,24 @@ const plugins = (es5) => [
[
'@babel/preset-env',
{
targets: es5 ? 'defaults, IE 11' : '>0.5%, last 2 versions, Firefox ESR, not dead',
targets: es5
? [
'> 0.5%, last 2 versions, Firefox ESR, not dead',
'chrome > 62',
'firefox > 59',
'ios_saf >= 6.1',
'opera > 50',
'safari > 12',
'IE 11',
]
: [
'> 0.5%, last 2 versions, Firefox ESR, not dead',
'chrome > 62',
'firefox > 59',
'ios_saf >= 10.3',
'opera > 50',
'safari > 12',
],
},
],
],
Expand Down
66 changes: 30 additions & 36 deletions src/__tests__/posthog-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,30 @@ import { defaultPostHog } from './helpers/posthog-instance'
import type { PostHogConfig } from '../types'
import { uuidv7 } from '../uuidv7'

const mockReferrerGetter = jest.fn()
const mockURLGetter = jest.fn()
jest.mock('../utils/globals', () => {
const orig = jest.requireActual('../utils/globals')
return {
...orig,
document: {
...orig.document,
createElement: (...args: any[]) => orig.document.createElement(...args),
get referrer() {
return mockReferrerGetter?.()
},
get URL() {
return mockURLGetter?.()
},
},
get location() {
const url = mockURLGetter?.()
return {
href: url,
toString: () => url,
}
},
}
})

describe('posthog core', () => {
const mockURL = jest.fn()
const mockReferrer = jest.fn()

beforeAll(() => {
// Mock getters using Object.defineProperty
Object.defineProperty(document, 'URL', {
get: mockURL,
})
Object.defineProperty(document, 'referrer', {
get: mockReferrer,
})
Object.defineProperty(window, 'location', {
get: () => ({
href: mockURL(),
toString: () => mockURL(),
}),
configurable: true,
})
})

beforeEach(() => {
mockReferrerGetter.mockReturnValue('https://referrer.com')
mockURLGetter.mockReturnValue('https://example.com')
mockReferrer.mockReturnValue('https://referrer.com')
mockURL.mockReturnValue('https://example.com')
console.error = jest.fn()
})

Expand Down Expand Up @@ -111,7 +105,7 @@ describe('posthog core', () => {
it("should send referrer info with the event's properties", () => {
// arrange
const token = uuidv7()
mockReferrerGetter.mockReturnValue('https://referrer.example.com/some/path')
mockReferrer.mockReturnValue('https://referrer.example.com/some/path')
const { posthog, beforeSendMock } = setup({
token,
persistence_name: token,
Expand All @@ -132,14 +126,14 @@ describe('posthog core', () => {
it('should not update the referrer within the same session', () => {
// arrange
const token = uuidv7()
mockReferrerGetter.mockReturnValue('https://referrer1.example.com/some/path')
mockReferrer.mockReturnValue('https://referrer1.example.com/some/path')
const { posthog: posthog1 } = setup({
token,
persistence_name: token,
person_profiles: 'always',
})
posthog1.capture(eventName, eventProperties)
mockReferrerGetter.mockReturnValue('https://referrer2.example.com/some/path')
mockReferrer.mockReturnValue('https://referrer2.example.com/some/path')
const { posthog: posthog2, beforeSendMock } = setup({
token,
persistence_name: token,
Expand All @@ -163,14 +157,14 @@ describe('posthog core', () => {
it('should use the new referrer in a new session', () => {
// arrange
const token = uuidv7()
mockReferrerGetter.mockReturnValue('https://referrer1.example.com/some/path')
mockReferrer.mockReturnValue('https://referrer1.example.com/some/path')
const { posthog: posthog1 } = setup({
token,
persistence_name: token,
person_profiles: 'always',
})
posthog1.capture(eventName, eventProperties)
mockReferrerGetter.mockReturnValue('https://referrer2.example.com/some/path')
mockReferrer.mockReturnValue('https://referrer2.example.com/some/path')
const { posthog: posthog2, beforeSendMock: beforeSendMock2 } = setup({
token,
persistence_name: token,
Expand All @@ -194,7 +188,7 @@ describe('posthog core', () => {
it('should use $direct when there is no referrer', () => {
// arrange
const token = uuidv7()
mockReferrerGetter.mockReturnValue('')
mockReferrer.mockReturnValue('')
const { posthog, beforeSendMock } = setup({
token,
persistence_name: token,
Expand All @@ -217,7 +211,7 @@ describe('posthog core', () => {
it('should not send campaign params as null if there are no non-null ones', () => {
// arrange
const token = uuidv7()
mockURLGetter.mockReturnValue('https://www.example.com/some/path')
mockURL.mockReturnValue('https://www.example.com/some/path')
const { posthog, beforeSendMock } = setup({
token,
persistence_name: token,
Expand All @@ -234,7 +228,7 @@ describe('posthog core', () => {
it('should send present campaign params, and nulls for others', () => {
// arrange
const token = uuidv7()
mockURLGetter.mockReturnValue('https://www.example.com/some/path?utm_source=source')
mockURL.mockReturnValue('https://www.example.com/some/path?utm_source=source')
const { posthog, beforeSendMock } = setup({
token,
persistence_name: token,
Expand Down

0 comments on commit 4f1a2bc

Please sign in to comment.