Skip to content
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

Merged
merged 2 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 11 additions & 80 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@
"postcss-loader": "3.0.0",
"prettier": "npm:wp-prettier@2.2.1-beta-1",
"progress": "2.0.3",
"puppeteer": "npm:puppeteer-core@5.5.0",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "0.61.5",
Expand Down
1 change: 0 additions & 1 deletion packages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ _Example:_
"cross-spawn": "^5.1.0",
"eslint": "^7.1.0",
- "jest": "^26.6.3",
"jest-puppeteer": "^4.4.0",
"minimist": "^1.2.0",
"npm-package-json-lint": "^3.6.0",
```
Expand Down
8 changes: 5 additions & 3 deletions packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

## Unreleased

### Breaking Changes

- Lint TypeScript files as part of `lint-js`. [#27143](https://github.com/WordPress/gutenberg/pull/27143)

### New Features

- Default `check-engines` command to the `engines` config in `package.json` file of the current project ([#29066](https://github.com/WordPress/gutenberg/pull/29066)).
### Breaking Changes

- Lint TypeScript files as part of `lint-js`. [#27143](https://github.com/WordPress/gutenberg/pull/27143)

### Enhancements

- Make `check-licenses` command compatible with npm v7 ([#28909](https://github.com/WordPress/gutenberg/pull/28909)).
- Add `Python 2.0` to non-GPL compatible OSS licenses allowed for development in `check-licenses` command ([#29968](https://github.com/WordPress/gutenberg/pull/28968)).
- Updated `check-node-version` to version `^4.1.0` that no longer processes unrelated engines with `check-engines` command ([#29066](https://github.com/WordPress/gutenberg/pull/29066)).
- Replace `jest-puppeteer` with the forked version of `jest-environment-puppeteer` to use `puppeteer-core` directly ([#29418](https://github.com/WordPress/gutenberg/pull/29418)).

## 13.0.0 (2021-01-21)

Expand Down
15 changes: 8 additions & 7 deletions packages/scripts/config/jest-e2e.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ const path = require( 'path' );
const { hasBabelConfig } = require( '../utils' );

const jestE2EConfig = {
preset: 'jest-puppeteer',
globalSetup: path.join( __dirname, 'jest-environment-puppeteer', 'setup' ),
globalTeardown: path.join(
__dirname,
'jest-environment-puppeteer',
'teardown'
),
testEnvironment: path.join( __dirname, 'jest-environment-puppeteer' ),
setupFilesAfterEnv: [ 'expect-puppeteer' ],
testMatch: [ '**/specs/**/*.[jt]s', '**/?(*.)spec.[jt]s' ],
testPathIgnorePatterns: [ '/node_modules/' ],
reporters:
Copy link
Member Author

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.

'TRAVIS' in process.env && 'CI' in process.env
? [
'@wordpress/jest-preset-default/scripts/travis-fold-passes-reporter.js',
]
: undefined,
};

if ( ! hasBabelConfig() ) {
Expand Down
89 changes: 89 additions & 0 deletions packages/scripts/config/jest-environment-puppeteer/config.js
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' );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside:
I have just learned that https://www.npmjs.com/package/puppeteer-firefox is deprecated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's now integrated into puppeteer proper, and can be run via the --puppeteer-product=firefox arg, see e.g. #28498.

We could drop this case branch (which amounts to re-organizing -- or even dropping -- the whole getPuppeteer function).

default:
throw new Error(
`Error: "browser" config option is given an unsupported value: ${ browser }`
);
}
}

module.exports = {
readConfig,
getPuppeteer,
};
99 changes: 99 additions & 0 deletions packages/scripts/config/jest-environment-puppeteer/global.js
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,
};
Loading