-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scripts: Fork jest-environment-puppeteer to use puppeteer-core direct…
…ly (#29418) * Scripts: Fork jest-environment-puppeteer to use puppeteer-core directly * Remove obsolete jest-environment-puppeteer from dependencies
- Loading branch information
Showing
12 changed files
with
405 additions
and
95 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -187,7 +187,6 @@ | |
"postcss-loader": "3.0.0", | ||
"prettier": "npm:[email protected]", | ||
"progress": "2.0.3", | ||
"puppeteer": "npm:[email protected]", | ||
"react": "16.13.1", | ||
"react-dom": "16.13.1", | ||
"react-native": "0.61.5", | ||
|
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
89 changes: 89 additions & 0 deletions
89
packages/scripts/config/jest-environment-puppeteer/config.js
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,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' ); | ||
default: | ||
throw new Error( | ||
`Error: "browser" config option is given an unsupported value: ${ browser }` | ||
); | ||
} | ||
} | ||
|
||
module.exports = { | ||
readConfig, | ||
getPuppeteer, | ||
}; |
99 changes: 99 additions & 0 deletions
99
packages/scripts/config/jest-environment-puppeteer/global.js
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,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, | ||
}; |
Oops, something went wrong.