-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add SSR tests (test command) (#23457)
* chore: add SSR tests (test command) * use console.warn & console.error * address review comments * use waitForSelector * inline the script * use ts-node directly * avoid mocks in buildAssets.test.ts * Update apps/ssr-tests-v9/src/test.ts Co-authored-by: Martin Hochel <[email protected]> * Update apps/ssr-tests-v9/src/test.ts Co-authored-by: Martin Hochel <[email protected]> * Update apps/ssr-tests-v9/src/test.ts Co-authored-by: Martin Hochel <[email protected]> * close browser always * add name to the error * fix versions * fix versions Co-authored-by: Martin Hochel <[email protected]>
- Loading branch information
1 parent
c76e61e
commit 4405e16
Showing
13 changed files
with
176 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import * as chalk from 'chalk'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import type { Browser } from 'puppeteer'; | ||
|
||
import { PROVIDER_ID } from './utils/constants'; | ||
import { hrToSeconds } from './utils/helpers'; | ||
import { launchBrowser } from './utils/launchBrowser'; | ||
|
||
class RenderError extends Error { | ||
public name = 'RangeError'; | ||
} | ||
|
||
export async function runTest(browser: Browser, url: string): Promise<void> { | ||
const page = await browser.newPage(); | ||
await page.setRequestInterception(true); | ||
|
||
let error: Error | undefined; | ||
|
||
page.on('console', message => { | ||
if (message.type() === 'error') { | ||
// Ignoring network errors as we have an interceptor that prevents loading everything except our JS bundle | ||
if (!message.text().includes('net::ERR_FAILED')) { | ||
error = new RenderError(message.text()); | ||
} | ||
} | ||
}); | ||
|
||
page.on('request', request => { | ||
// Our interceptor allows only our HTML and JS output | ||
if (request.url() === url || request.url().endsWith('/out-esm.js')) { | ||
return request.continue(); | ||
} | ||
|
||
return request.abort(); | ||
}); | ||
|
||
page.on('pageerror', err => { | ||
error = err; | ||
}); | ||
|
||
await page.goto(url); | ||
await page.waitForSelector(`#${PROVIDER_ID}`); | ||
|
||
await page.close(); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
async function test(): Promise<void> { | ||
const startTime = process.hrtime(); | ||
console.log('Starting a browser...'); | ||
|
||
let browser: Browser | undefined; | ||
|
||
try { | ||
browser = await launchBrowser(); | ||
console.log('Using', await browser.version()); | ||
|
||
const htmlPath = path.resolve(__dirname, '..', 'dist', 'index.html'); | ||
|
||
if (!fs.existsSync(htmlPath)) { | ||
throw new Error('"dist/index.html" does not exist, please run "yarn build" first'); | ||
} | ||
|
||
const url = `file://${htmlPath}`; | ||
console.log(`Using "${url}"`); | ||
|
||
await runTest(browser, url); | ||
console.log(`Test finished successfully in ${hrToSeconds(process.hrtime(startTime))}`); | ||
} finally { | ||
if (browser) { | ||
await browser.close(); | ||
} | ||
} | ||
} | ||
|
||
test().catch((err: Error) => { | ||
console.error(''); | ||
console.error(chalk.bgRed.whiteBright(' @fluentui/ssr-tests-v9 ')); | ||
|
||
if (err instanceof RenderError) { | ||
console.error( | ||
[ | ||
' The test failed.', | ||
'Please use `$ npx serve dist` or `$ open dist/index.html` to open a HTML page that is used in tests.', | ||
].join(' '), | ||
); | ||
console.error(' The reference error is below, you will see it in Devtools on the opened page.'); | ||
console.error(''); | ||
} else { | ||
console.error(' The test failed, the error below contains relevant information.'); | ||
console.error(''); | ||
} | ||
|
||
console.error(err); | ||
|
||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const PROVIDER_ID = 'root-provider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { launchBrowser } from './launchBrowser'; | ||
|
||
export async function getChromeVersion(): Promise<number> { | ||
const browser = await launchBrowser(); | ||
|
||
// includes browser name, example: HeadlessChrome/103.0.5058.0 | ||
const rawVersion = await browser.version(); | ||
const version = rawVersion.split('/')[1].split('.')[0]; | ||
|
||
await browser.close(); | ||
|
||
return parseInt(version, 10); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Browser, launch } from 'puppeteer'; | ||
|
||
export async function launchBrowser(): Promise<Browser> { | ||
let browser; | ||
let attempt = 1; | ||
|
||
while (!browser) { | ||
try { | ||
browser = await launch(); | ||
} catch (err) { | ||
if (attempt === 5) { | ||
console.error(`Failed to launch a browser after 5 attempts...`); | ||
throw err; | ||
} | ||
|
||
console.warn('A browser failed to start, retrying...'); | ||
console.warn(err); | ||
attempt++; | ||
} | ||
} | ||
|
||
return browser; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters