Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
193 lines
5.5 KiB
TypeScript
193 lines
5.5 KiB
TypeScript
import {
|
|
hasErrorToast,
|
|
getRedboxComponentStack,
|
|
getRedboxDescription,
|
|
getRedboxHeader,
|
|
getRedboxSource,
|
|
getVersionCheckerText,
|
|
waitForRedbox,
|
|
waitForNoRedbox,
|
|
waitFor,
|
|
openRedbox,
|
|
getRedboxDescriptionWarning,
|
|
getRedboxErrorLink,
|
|
} from './next-test-utils'
|
|
import webdriver, { WebdriverOptions } from './next-webdriver'
|
|
import { NextInstance } from './next-modes/base'
|
|
import { Playwright } from 'next-webdriver'
|
|
|
|
export async function waitForHydration(browser: Playwright) {
|
|
await browser.eval(() => {
|
|
return new Promise<void>((resolve) => {
|
|
if ((window as any).__NEXT_HYDRATED) {
|
|
resolve()
|
|
} else {
|
|
var timeout = setTimeout(resolve, 30 * 1000)
|
|
;(window as any).__NEXT_HYDRATED_CB = function () {
|
|
clearTimeout(timeout)
|
|
resolve()
|
|
}
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
export async function createSandbox(
|
|
next: NextInstance,
|
|
initialFiles?: Map<string, string | ((contents: string) => string)>,
|
|
initialUrl: string = '/',
|
|
webDriverOptions: WebdriverOptions | undefined = undefined
|
|
) {
|
|
let unwrappedByTypeScriptUsingKeyword = false
|
|
|
|
try {
|
|
await next.stop()
|
|
await next.clean()
|
|
if (initialFiles) {
|
|
for (const [k, v] of initialFiles.entries()) {
|
|
await next.patchFile(k, v)
|
|
}
|
|
}
|
|
await next.start()
|
|
|
|
const browser = await webdriver(next.url, initialUrl, webDriverOptions)
|
|
|
|
async function evaluate<TFn extends (...args: any[]) => any>(
|
|
fn: TFn,
|
|
...args: Parameters<TFn>
|
|
): Promise<ReturnType<TFn>>
|
|
async function evaluate(fn: string): Promise<unknown>
|
|
async function evaluate(
|
|
snippet: string | ((...args: any) => any)
|
|
): Promise<any> {
|
|
if (typeof snippet === 'function' || typeof snippet === 'string') {
|
|
const result = await browser.eval(snippet)
|
|
await waitFor(30)
|
|
return result
|
|
} else {
|
|
throw new Error(
|
|
`You must pass a string or function to be evaluated in the browser.`
|
|
)
|
|
}
|
|
}
|
|
|
|
return {
|
|
browser,
|
|
session: {
|
|
async write(filename, content) {
|
|
// Update the file on filesystem
|
|
await next.patchFile(filename, content)
|
|
},
|
|
async patch(filename, content) {
|
|
// Register an event for HMR completion
|
|
await browser.eval(function () {
|
|
;(window as any).__HMR_STATE = 'pending'
|
|
|
|
var timeout = setTimeout(() => {
|
|
;(window as any).__HMR_STATE = 'timeout'
|
|
}, 30 * 1000)
|
|
;(window as any).__NEXT_HMR_CB = function () {
|
|
clearTimeout(timeout)
|
|
;(window as any).__HMR_STATE = 'success'
|
|
}
|
|
})
|
|
|
|
await this.write(filename, content)
|
|
|
|
for (;;) {
|
|
const status = await browser.eval(() => (window as any).__HMR_STATE)
|
|
if (!status) {
|
|
await waitFor(750)
|
|
|
|
// Wait for application to re-hydrate:
|
|
await waitForHydration(browser)
|
|
|
|
console.log('Application re-loaded.')
|
|
// Slow down tests a bit:
|
|
await waitFor(750)
|
|
return false
|
|
}
|
|
if (status === 'success') {
|
|
console.log('Hot update complete.')
|
|
break
|
|
}
|
|
if (status !== 'pending') {
|
|
throw new Error(
|
|
`Application is in inconsistent state: ${status}.`
|
|
)
|
|
}
|
|
|
|
await waitFor(30)
|
|
}
|
|
|
|
// Slow down tests a bit (we don't know how long re-rendering takes):
|
|
await waitFor(750)
|
|
return true
|
|
},
|
|
async remove(filename) {
|
|
await next.deleteFile(filename)
|
|
},
|
|
async renameFolder(...args: Parameters<(typeof next)['renameFolder']>) {
|
|
await next.renameFolder(...args)
|
|
},
|
|
evaluate,
|
|
async waitForRedbox() {
|
|
return waitForRedbox(browser)
|
|
},
|
|
async waitForNoRedbox() {
|
|
return waitForNoRedbox(browser)
|
|
},
|
|
async openRedbox() {
|
|
return openRedbox(browser)
|
|
},
|
|
async hasErrorToast() {
|
|
return Boolean(await hasErrorToast(browser))
|
|
},
|
|
async getRedboxDescription() {
|
|
return getRedboxDescription(browser)
|
|
},
|
|
async getRedboxDescriptionWarning() {
|
|
return getRedboxDescriptionWarning(browser)
|
|
},
|
|
async getRedboxErrorLink() {
|
|
return getRedboxErrorLink(browser)
|
|
},
|
|
async getRedboxSource(includeHeader = false) {
|
|
const header = includeHeader ? await getRedboxHeader(browser) : ''
|
|
const source = await getRedboxSource(browser)
|
|
|
|
if (includeHeader) {
|
|
return `${header}\n\n${source}`
|
|
}
|
|
return source
|
|
},
|
|
/**
|
|
* @returns `null` if there are no frames
|
|
*/
|
|
getRedboxComponentStack() {
|
|
return getRedboxComponentStack(browser)
|
|
},
|
|
async getVersionCheckerText() {
|
|
return getVersionCheckerText(browser)
|
|
},
|
|
},
|
|
get [Symbol.asyncDispose]() {
|
|
unwrappedByTypeScriptUsingKeyword = true
|
|
return async () => {
|
|
await browser.close()
|
|
await next.stop()
|
|
await next.clean()
|
|
}
|
|
},
|
|
}
|
|
} finally {
|
|
setImmediate(() => {
|
|
if (!unwrappedByTypeScriptUsingKeyword) {
|
|
throw new Error(
|
|
'You must use `using` to create a sandbox, i.e., `await using sandbox = await createSandbox(`'
|
|
)
|
|
}
|
|
})
|
|
}
|
|
}
|