-
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.
- Loading branch information
1 parent
82bd30d
commit ff8a7c2
Showing
10 changed files
with
278 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import chalk from 'chalk'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { launch, Browser } from 'puppeteer'; | ||
|
||
import { hrToSeconds } from './utils/helpers'; | ||
|
||
class RenderError extends Error {} | ||
|
||
async function startBrowser(): Promise<Browser> { | ||
let browser; | ||
let attempt = 1; | ||
|
||
while (!browser) { | ||
try { | ||
browser = await launch(); | ||
} catch (err) { | ||
if (attempt === 5) { | ||
console.log(`Failed to launch a browser after 5 attempts...`); | ||
throw err; | ||
} | ||
|
||
console.log('A browser failed to start, retrying...'); | ||
console.log(err); | ||
attempt++; | ||
} | ||
} | ||
|
||
return browser; | ||
} | ||
|
||
export async function runTest(browser: Browser, url: string): Promise<void> { | ||
const page = await browser.newPage(); | ||
await page.setRequestInterception(true); | ||
|
||
let error; | ||
|
||
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.close(); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
async function test(): Promise<void> { | ||
const startTime = process.hrtime(); | ||
console.log('Starting a browser...'); | ||
|
||
const browser = await startBrowser(); | ||
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); | ||
await browser.close(); | ||
|
||
console.log(`Test finished successfully in ${hrToSeconds(process.hrtime(startTime))}`); | ||
} | ||
|
||
test().catch(err => { | ||
console.log(''); | ||
console.log(chalk.bgRed.whiteBright(' @fluentui/ssr-tests-v9 ')); | ||
|
||
if (err instanceof RenderError) { | ||
console.log( | ||
[ | ||
' 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.log(' The reference error is below, you will see it in Devtools on the opened page.'); | ||
console.log(''); | ||
} else { | ||
console.log(' The test is failed, the error below contains relevant information.'); | ||
console.log(''); | ||
} | ||
|
||
console.log(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
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
Oops, something went wrong.