Skip to content

Commit

Permalink
fix jest
Browse files Browse the repository at this point in the history
  • Loading branch information
metal-messiah committed Mar 11, 2025
1 parent 9dc9b50 commit 4b4ab95
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 18 deletions.
19 changes: 19 additions & 0 deletions tests/components/page_view_timing/aggregate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ beforeAll(async () => {
downlink: 700
}

Object.defineProperty(performance, 'getEntriesByType', {
value: jest.fn().mockImplementation(entryType => {
return [
{
cancelable: true,
duration: 17,
entryType,
name: 'pointer',
processingEnd: 8860,
processingStart: 8859,
startTime: 8853,
target: { tagName: 'button' }
}
]
}),
configurable: true,
writable: true
})

mainAgent = setupAgent()
})

Expand Down
19 changes: 19 additions & 0 deletions tests/components/page_view_timing/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ const expectedNetworkInfo = {
'net-dlink': expect.any(Number)
}

Object.defineProperty(performance, 'getEntriesByType', {
value: jest.fn().mockImplementation(entryType => {
return [
{
cancelable: true,
duration: 17,
entryType,
name: 'pointer',
processingEnd: 8860,
processingStart: 8859,
startTime: 8853,
target: { tagName: 'button' }
}
]
}),
configurable: true,
writable: true
})

let pvtAgg
const agentIdentifier = 'abcd'
describe('pvt aggregate tests', () => {
Expand Down
41 changes: 38 additions & 3 deletions tests/unit/common/vitals/first-contentful-paint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ describe('fcp', () => {
initiallyHidden: false,
isBrowserScope: true
}))
// global.performance.getEntriesByType = jest.fn(() => [{ name: 'first-contentful-paint', startTime: 1 }])

Object.defineProperty(performance, 'getEntriesByType', {
value: jest.fn().mockImplementation(entryType => {
return [
{
name: 'first-contentful-paint',
startTime: 1
}
]
}),
configurable: true,
writable: true
})

getFreshFCPImport(firstContentfulPaint => firstContentfulPaint.subscribe(({ value }) => {
expect(value).toEqual(1)
Expand Down Expand Up @@ -66,7 +78,19 @@ describe('fcp', () => {
initiallyHidden: false,
isBrowserScope: true
}))
// global.performance.getEntriesByType = jest.fn(() => [{ name: 'other-timing-name', startTime: 1 }])

Object.defineProperty(performance, 'getEntriesByType', {
value: jest.fn().mockImplementation(entryType => {
return [
{
name: 'other-timing-name',
startTime: 1
}
]
}),
configurable: true,
writable: true
})

getFreshFCPImport(firstContentfulPaint => {
firstContentfulPaint.subscribe(() => {
Expand All @@ -84,7 +108,18 @@ describe('fcp', () => {
initiallyHidden: true,
isBrowserScope: true
}))
// global.performance.getEntriesByType = jest.fn(() => [{ name: 'first-contentful-paint', startTime: 1 }])
Object.defineProperty(performance, 'getEntriesByType', {
value: jest.fn().mockImplementation(entryType => {
return [
{
name: 'first-contentful-paint',
startTime: 1
}
]
}),
configurable: true,
writable: true
})

getFreshFCPImport(firstContentfulPaint => {
firstContentfulPaint.subscribe(() => {
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/features/page_view_timing/aggregate/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ describe('PVT aggregate', () => {
})

test('addConnectionAttributes', () => {
Object.defineProperty(performance, 'getEntriesByType', {
value: jest.fn().mockImplementation(entryType => {
return [
{
cancelable: true,
duration: 17,
entryType,
name: 'pointer',
processingEnd: 8860,
processingStart: 8859,
startTime: 8853,
target: { tagName: 'button' }
}
]
}),
configurable: true,
writable: true
})
global.navigator.connection = {}
pvtAgg.addTiming('abc', 1)
expect(pvtAgg.events.get()[0].data[0].attrs).toEqual(expect.objectContaining({}))
Expand Down
15 changes: 0 additions & 15 deletions tools/jest/globals.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,4 @@ if (typeof window !== 'undefined') {
window.Response = jest.fn()
}

Object.defineProperty(global.performance, 'getEntriesByType', {
value: jest.fn(entryType => ([
{
cancelable: true,
duration: 17,
entryType,
name: 'pointer',
processingEnd: 8860,
processingStart: 8859,
startTime: 8853,
target: { tagName: 'button' }
}
]))
})

jest.retryTimes(3)

0 comments on commit 4b4ab95

Please sign in to comment.