-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scripts: Fork jest-environment-puppeteer to use puppeteer-core directly #29418
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* Parts of this source were derived and modified from the package | ||
* jest-environment-puppeteer, released under the MIT license. | ||
* | ||
* https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-environment-puppeteer | ||
* | ||
* Copyright 2018 Smooth Code | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
const fs = require( 'fs' ); | ||
const path = require( 'path' ); | ||
const { promisify } = require( 'util' ); | ||
const cwd = require( 'cwd' ); | ||
const merge = require( 'merge-deep' ); | ||
|
||
const exists = promisify( fs.exists ); | ||
|
||
const DEFAULT_CONFIG = { | ||
launch: {}, | ||
browser: 'chromium', | ||
browserContext: 'default', | ||
exitOnPageError: true, | ||
}; | ||
const DEFAULT_CONFIG_CI = merge( DEFAULT_CONFIG, { | ||
launch: { | ||
args: [ | ||
'--no-sandbox', | ||
'--disable-setuid-sandbox', | ||
'--disable-background-timer-throttling', | ||
'--disable-backgrounding-occluded-windows', | ||
'--disable-renderer-backgrounding', | ||
], | ||
}, | ||
} ); | ||
|
||
async function readConfig() { | ||
const defaultConfig = | ||
process.env.CI === 'true' ? DEFAULT_CONFIG_CI : DEFAULT_CONFIG; | ||
|
||
const hasCustomConfigPath = !! process.env.JEST_PUPPETEER_CONFIG; | ||
const configPath = | ||
process.env.JEST_PUPPETEER_CONFIG || 'jest-puppeteer.config.js'; | ||
const absConfigPath = path.resolve( cwd(), configPath ); | ||
const configExists = await exists( absConfigPath ); | ||
|
||
if ( hasCustomConfigPath && ! configExists ) { | ||
throw new Error( | ||
`Error: Can't find a root directory while resolving a config file path.\nProvided path to resolve: ${ configPath }` | ||
); | ||
} | ||
|
||
if ( ! hasCustomConfigPath && ! configExists ) { | ||
return defaultConfig; | ||
} | ||
|
||
const localConfig = await require( absConfigPath ); | ||
return merge( {}, defaultConfig, localConfig ); | ||
} | ||
|
||
function getPuppeteer( { browser } ) { | ||
switch ( browser.toLowerCase() ) { | ||
case 'chromium': | ||
try { | ||
return require( 'puppeteer' ); | ||
} catch ( e ) { | ||
return require( 'puppeteer-core' ); | ||
} | ||
case 'firefox': | ||
return require( 'puppeteer-firefox' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aside: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it's now integrated into We could drop this |
||
default: | ||
throw new Error( | ||
`Error: "browser" config option is given an unsupported value: ${ browser }` | ||
); | ||
} | ||
} | ||
|
||
module.exports = { | ||
readConfig, | ||
getPuppeteer, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* Parts of this source were derived and modified from the package | ||
* jest-environment-puppeteer, released under the MIT license. | ||
* | ||
* https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-environment-puppeteer | ||
* | ||
* Copyright 2018 Smooth Code | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
const { | ||
setup: setupServer, | ||
teardown: teardownServer, | ||
ERROR_TIMEOUT, | ||
ERROR_NO_COMMAND, | ||
} = require( 'jest-dev-server' ); | ||
const chalk = require( 'chalk' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { readConfig, getPuppeteer } = require( './config' ); | ||
|
||
let browser; | ||
|
||
let didAlreadyRunInWatchMode = false; | ||
|
||
async function setup( jestConfig = {} ) { | ||
const config = await readConfig(); | ||
const puppeteer = getPuppeteer( config ); | ||
if ( config.connect ) { | ||
browser = await puppeteer.connect( config.connect ); | ||
} else { | ||
browser = await puppeteer.launch( config.launch ); | ||
} | ||
process.env.PUPPETEER_WS_ENDPOINT = browser.wsEndpoint(); | ||
|
||
// If we are in watch mode, - only setupServer() once. | ||
if ( jestConfig.watch || jestConfig.watchAll ) { | ||
if ( didAlreadyRunInWatchMode ) return; | ||
didAlreadyRunInWatchMode = true; | ||
} | ||
|
||
if ( config.server ) { | ||
try { | ||
await setupServer( config.server ); | ||
} catch ( error ) { | ||
const { error: printError } = console; | ||
if ( error.code === ERROR_TIMEOUT ) { | ||
printError( '' ); | ||
printError( chalk.red( error.message ) ); | ||
printError( | ||
chalk.blue( | ||
`\n☝️ You can set "server.launchTimeout" in jest-puppeteer.config.js` | ||
) | ||
); | ||
process.exit( 1 ); | ||
} | ||
if ( error.code === ERROR_NO_COMMAND ) { | ||
printError( '' ); | ||
printError( chalk.red( error.message ) ); | ||
printError( | ||
chalk.blue( | ||
`\n☝️ You must set "server.command" in jest-puppeteer.config.js` | ||
) | ||
); | ||
process.exit( 1 ); | ||
} | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
async function teardown( jestConfig = {} ) { | ||
const config = await readConfig(); | ||
|
||
if ( config.connect ) { | ||
await browser.disconnect(); | ||
} else { | ||
await browser.close(); | ||
} | ||
|
||
if ( ! jestConfig.watch && ! jestConfig.watchAll ) { | ||
await teardownServer(); | ||
} | ||
} | ||
|
||
module.exports = { | ||
setup, | ||
teardown, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer use Travis so this code is not used. I also don't think that
travis-fold-passes-reporter.js
file is still present in the Jest preset.