Skip to content

Commit

Permalink
[TEST] Refactor the playwright configuration computation (#1751)
Browse files Browse the repository at this point in the history
Provide a single function to compute the configuration and introduce options.
Also fix the default browsers list for end-to-end tests. After the previous refactoring, it was 'chromium,firefox,webkit' instead of 'chromium'.

Add comments to better explain
  - why we override the protocol `serverOptions` in the jest playwright config
  - how we retrieve the browser console logs to log them in the node console
  • Loading branch information
tbouffard committed Jan 13, 2022
1 parent c860161 commit 2380758
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 30 deletions.
2 changes: 1 addition & 1 deletion test/bundles/jest-playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* limitations under the License.
*/

module.exports = require('../config/jest-playwright').computeConfigurationForStaticUsage();
module.exports = require('../config/jest-playwright').computeConfiguration({ startWebServer: false, defaultBrowsers: 'chromium,firefox,webkit' });
61 changes: 36 additions & 25 deletions test/config/jest-playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const computeBrowsersAndChannelConfiguration = defaultBrowsers => {
return config;
};

const computeLaunchOptionsAndBrowsersConfiguration = (defaultBrowsers = 'chromium,firefox,webkit') => {
const computeLaunchOptionsAndBrowsersConfiguration = defaultBrowsers => {
log('Computing launchOptions and browsers configuration');

/** @type {import('playwright-core/types/types').LaunchOptions} */
Expand All @@ -62,32 +62,14 @@ const computeLaunchOptionsAndBrowsersConfiguration = (defaultBrowsers = 'chromiu
return config;
};

const computeServerOptions = () => {
log('Computing serverOptions');
const options = {
command: `npm run start -- --config-server-port 10002`,
port: 10002,
protocol: 'http', // if default or tcp, the test starts right await whereas the dev server is not available on http
launchTimeout: 60000, // high value mainly for GitHub Workflows running on macOS (slow machines) and to build the bundle before start
debug: true,
usedPortAction: 'ignore', // your tests are executed, we assume that the server is already started
};
log('Computed serverOptions', options);
return options;
const computeConfigurationForStaticUsage = defaultBrowsers => {
log('Computing configuration for static usage');
return computeBaseConfiguration(defaultBrowsers);
};

const computeConfigurationForStaticUsage = () => {
const { browsers, launchOptions } = computeLaunchOptionsAndBrowsersConfiguration();
return {
launchOptions: launchOptions,
browsers: browsers,
};
};

const computeConfigurationForDevServerUsage = defaultBrowsers => {
const computeBaseConfiguration = defaultBrowsers => {
const { browsers, launchOptions } = computeLaunchOptionsAndBrowsersConfiguration(defaultBrowsers);
return {
serverOptions: computeServerOptions(),
launchOptions: launchOptions,
launchType: 'LAUNCH',
contextOptions: {
Expand All @@ -100,5 +82,34 @@ const computeConfigurationForDevServerUsage = defaultBrowsers => {
};
};

exports.computeConfigurationForStaticUsage = computeConfigurationForStaticUsage;
exports.computeConfigurationForDevServerUsage = computeConfigurationForDevServerUsage;
const computeConfigurationForDevServerUsage = defaultBrowsers => {
log('Computing configuration for dev server usage');
/** @type {import('jest-playwright-preset/types/global').ServerOptions} */
const serverOptions = {
command: `npm run start -- --config-server-port 10002`,
port: 10002,
// if default or tcp, the test starts right await whereas the dev server is not available on http
// for more details, see https://github.com/process-analytics/bpmn-visualization-js/pull/1056
protocol: 'http',
launchTimeout: 60_000, // high value mainly for GitHub Workflows running on macOS (slow machines) and to build the bundle before start
debug: true,
usedPortAction: 'ignore', // your tests are executed, we assume that the server is already started
};
return {
...computeBaseConfiguration(defaultBrowsers),
serverOptions: serverOptions,
};
};

const computeConfiguration = options => {
let configuration;
if (options.startWebServer ?? true) {
configuration = computeConfigurationForDevServerUsage(options.defaultBrowsers);
} else {
configuration = computeConfigurationForStaticUsage(options.defaultBrowsers);
}
log('Computed configuration', configuration);
return configuration;
};

exports.computeConfiguration = computeConfiguration;
3 changes: 1 addition & 2 deletions test/config/playwright.browser.logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import debugLogger from 'debug';
import 'jest-playwright-preset';

// Allow getting browser console logs
// this is from https://playwright.dev/docs/api/class-page#pageonconsole
// see https://github.com/microsoft/playwright/issues/4498 and https://github.com/microsoft/playwright/issues/4125
// this is from https://playwright.dev/docs/api/class-page#page-event-console
const browserLog = debugLogger('bv:test:browser');
page.on('console', msg => browserLog('<%s> %s', msg.type(), msg.text()));
2 changes: 1 addition & 1 deletion test/e2e/jest-playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* limitations under the License.
*/

module.exports = require('../config/jest-playwright').computeConfigurationForDevServerUsage();
module.exports = require('../config/jest-playwright').computeConfiguration({ defaultBrowsers: 'chromium' });
2 changes: 1 addition & 1 deletion test/performance/jest-playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
*/

// only collect on chromium for now
module.exports = module.exports = require('../config/jest-playwright').computeConfigurationForDevServerUsage('chromium');
module.exports = module.exports = require('../config/jest-playwright').computeConfiguration({ defaultBrowsers: 'chromium' });

0 comments on commit 2380758

Please sign in to comment.