Skip to content

Commit 76f41d9

Browse files
committed
Add ability for configuration through jest-playwright.config.js file
1 parent 0b8e833 commit 76f41d9

File tree

4 files changed

+58
-15
lines changed

4 files changed

+58
-15
lines changed

src/PlaywrightEnvironment.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import fs from 'fs'
22
import NodeEnvironment from 'jest-environment-node'
33
import playwright from 'playwright'
44
import { WS_ENDPOINT_PATH } from './constants'
5+
import { checkBrowserEnv, getBrowserType, readConfig } from "./utils";
56

67
const handleError = error => {
78
process.emit('uncaughtException', error)
89
};
910

1011
class PlaywrightEnvironment extends NodeEnvironment {
1112
async teardown() {
12-
console.log('Teardown Test Environment.');
1313
await super.teardown()
1414
}
1515

@@ -18,11 +18,13 @@ class PlaywrightEnvironment extends NodeEnvironment {
1818
if (!wsEndpoint) {
1919
throw new Error('wsEndpoint not found')
2020
}
21-
const browserType = process.env.BROWSER;
22-
this.global.browser = await playwright[browserType].connect({
23-
browserWSEndpoint: wsEndpoint,
24-
});
25-
this.global.context = await this.global.browser.newContext();
21+
const config = await readConfig();
22+
const browserType = getBrowserType(config);
23+
checkBrowserEnv(browserType);
24+
const { connect, context } = config;
25+
const connectOptions = Object.assign({}, { browserWSEndpoint: wsEndpoint }, connect );
26+
this.global.browser = await playwright[browserType].connect(connectOptions);
27+
this.global.context = await this.global.browser.newContext(context);
2628
this.global.page = await this.global.context.newPage();
2729
this.global.page.on('pageerror', handleError)
2830
}

src/constants.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ export const WS_ENDPOINT_PATH = path.join(DIR, 'wsEndpoint');
77
export const CHROMIUM = 'chromium';
88
export const FIREFOX = 'firefox';
99
export const WEBKIT = 'webkit';
10+
11+
export const DEFAULT_CONFIG = {
12+
launchBrowserApp: { webSocket: true},
13+
context: {},
14+
browser: CHROMIUM,
15+
exitOnPageError: true,
16+
};

src/global.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@ import fs from 'fs'
22
import rimraf from 'rimraf'
33
import playwright from 'playwright'
44
import { DIR, WS_ENDPOINT_PATH } from './constants'
5-
import checkBrowserEnv from "./utils";
5+
import { checkBrowserEnv, readConfig, getBrowserType } from "./utils";
66

77
let browser;
88

99
export async function setup() {
10-
const browserType = process.env.BROWSER;
10+
const config = await readConfig();
11+
const browserType = getBrowserType(config);
1112
checkBrowserEnv(browserType);
12-
console.log('Setup Playwright');
13-
const OPTIONS = { webSocket: true};
14-
browser = await playwright[browserType].launchBrowserApp(OPTIONS);
13+
const { launchBrowserApp } = config;
14+
browser = await playwright[browserType].launchBrowserApp(launchBrowserApp);
1515
// Instead, we expose the connection details via file system to be used in tests
1616
fs.mkdirSync(DIR, { recursive: true });
1717
fs.writeFileSync(WS_ENDPOINT_PATH, browser.wsEndpoint())
1818
}
1919

2020
export async function teardown() {
21-
console.log('Teardown Playwright');
2221
await browser.close()
2322
rimraf.sync(DIR)
2423
}

src/utils.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,44 @@
1-
import { CHROMIUM, FIREFOX, WEBKIT } from './constants';
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { promisify } from 'util';
4+
import { CHROMIUM, FIREFOX, WEBKIT, DEFAULT_CONFIG } from './constants';
25

3-
function checkBrowserEnv(param) {
6+
const exists = promisify(fs.exists);
7+
8+
export function checkBrowserEnv(param) {
49
if (param !== CHROMIUM && param !== FIREFOX && param !== WEBKIT) {
510
throw new Error(`Wrong browser type. Should be one of [${CHROMIUM}, ${FIREFOX}, ${WEBKIT}], but got ${param}`)
611
}
712
}
813

9-
export default checkBrowserEnv
14+
export function getBrowserType(config) {
15+
const processBrowser = process.env.BROWSER;
16+
if (processBrowser) {
17+
return processBrowser
18+
} else {
19+
return config.browser || CHROMIUM
20+
}
21+
}
22+
23+
export async function readConfig() {
24+
const defaultConfig = DEFAULT_CONFIG;
25+
26+
const hasCustomConfigPath = !!process.env.JEST_PLAYWRIGHT_CONFIG;
27+
const configPath =
28+
process.env.JEST_PLAYWRIGHT_CONFIG || 'jest-playwright.config.js';
29+
const absConfigPath = path.resolve(process.cwd(), configPath)
30+
const configExists = await exists(absConfigPath);
31+
32+
if (hasCustomConfigPath && !configExists) {
33+
throw new Error(
34+
`Error: Can't find a root directory while resolving a config file path.\nProvided path to resolve: ${configPath}`,
35+
)
36+
}
37+
38+
if (!hasCustomConfigPath && !configExists) {
39+
return defaultConfig
40+
}
41+
42+
const localConfig = await require(absConfigPath);
43+
return Object.assign({}, defaultConfig, localConfig);
44+
}

0 commit comments

Comments
 (0)