mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
rip. you served a purpose once, but now you're just getting in the way. this improves typesafety quite a bit, because `BrowserInterface` had a whole bunch of random `any`s everywhere also - removes `evalAsync`. no idea why that was needed, but we're happily using promises in normal eval, so it can be dropped - adds more safety to `chain`
72 lines
1.6 KiB
TypeScript
72 lines
1.6 KiB
TypeScript
import { Playwright } from 'next-webdriver'
|
|
|
|
export const getPathname = (url: string) => {
|
|
const urlObj = new URL(url)
|
|
return urlObj.pathname
|
|
}
|
|
|
|
export const browserConfigWithFixedTime = {
|
|
beforePageLoad: (page) => {
|
|
page.addInitScript(() => {
|
|
const startTime = new Date()
|
|
const fixedTime = new Date('2023-04-17T00:00:00Z')
|
|
|
|
// Override the Date constructor
|
|
// @ts-ignore
|
|
// eslint-disable-next-line no-native-reassign
|
|
Date = class extends Date {
|
|
constructor() {
|
|
super()
|
|
// @ts-ignore
|
|
return new startTime.constructor(fixedTime)
|
|
}
|
|
|
|
static now() {
|
|
return fixedTime.getTime()
|
|
}
|
|
}
|
|
})
|
|
},
|
|
}
|
|
|
|
export const fastForwardTo = (ms) => {
|
|
// Increment the fixed time by the specified duration
|
|
const currentTime = new Date()
|
|
currentTime.setTime(currentTime.getTime() + ms)
|
|
|
|
// Update the Date constructor to use the new fixed time
|
|
// @ts-ignore
|
|
// eslint-disable-next-line no-native-reassign
|
|
Date = class extends Date {
|
|
constructor() {
|
|
super()
|
|
// @ts-ignore
|
|
return new currentTime.constructor(currentTime)
|
|
}
|
|
|
|
static now() {
|
|
return currentTime.getTime()
|
|
}
|
|
}
|
|
}
|
|
|
|
export const createRequestsListener = async (browser: Playwright) => {
|
|
// wait for network idle
|
|
await browser.waitForIdleNetwork()
|
|
|
|
let requests = []
|
|
|
|
browser.on('request', (req) => {
|
|
requests.push([req.url(), !!req.headers()['next-router-prefetch']])
|
|
})
|
|
|
|
await browser.refresh()
|
|
|
|
return {
|
|
getRequests: () => requests,
|
|
clearRequests: () => {
|
|
requests = []
|
|
},
|
|
}
|
|
}
|