Skip to content

Commit

Permalink
feat!: config function receives cli options and process.env.PW_TEST r…
Browse files Browse the repository at this point in the history
…emove in favor of stringified process.env.PW_OPTIONS

use `playwright-test/client` to access runner options and other exposed internals.
  • Loading branch information
hugomrdias committed Nov 23, 2023
1 parent 7cca595 commit 6d6892f
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ sade2

// if the supplied config module was a function, invoke it
if (config && typeof config.config === 'function') {
config.config = await config.config()
config.config = await config.config(opts)
}

/**
Expand Down
3 changes: 2 additions & 1 deletion mocks/sw/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url)
if (url.pathname.endsWith('favicon.ico')) {
const env = JSON.parse(process.env.PW_OPTIONS)
const data = {
env: process.env.PW_TEST,
env,
kv: KV,
}
event.respondWith(new Response(JSON.stringify(data)))
Expand Down
56 changes: 42 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,22 @@ ${paths.map((url) => `await import('${url}')`).join('\n')}
export default config
```
```ts
````ts
export interface TestRunner {
/**
* Module ID name used to import the test runner runtime.
* Used in auto detection of the test runner.
*/
moduleId: string
/**
* Options made available to the compiled runtime, accessable with `process.env.PW_TEST.testRunner.options`.
* Options made available to the compiled runtime.
* This is useful to pass options to the test runner.
*
* @example
* ```js
* const options = JSON.parse(process.env.PW_OPTIONS)
* const testRunnerOptions = options.testRunner.options
* ```
*/
options?: unknown
/**
Expand All @@ -192,7 +199,7 @@ export interface TestRunner {
*/
compileRuntime: (options: RunnerOptions, testPaths: string[]) => string
}
```
````
## Config
Expand All @@ -201,17 +208,23 @@ export interface TestRunner {
Configuration can be done with cli flags or config files.
```text
'package.json', // using property `pw-test` or `playwright-test`
`.playwright-testrc.json`,
`.playwright-testrc.js`,
`playwright-test.config.js`,
`.playwright-testrc.cjs`,
`playwright-test.config.cjs`,
`.pw-testrc.json`,
`.pw-testrc.js`,
`pw-test.config.js`,
`.pw-testrc.cjs`,
`pw-test.config.cjs`,
package.json, // using property `pw-test` or `playwright-test`
.playwright-testrc.json,
.playwright-testrc.js,
.playwright-testrc.cjs,
playwright-test.config.js,
playwright-test.config.cjs,
.pw-testrc.json,
.pw-testrc.js,
.pw-testrc.cjs,
pw-test.config.js,
pw-test.config.cjs,
.config/playwright-testrc.json
.config/playwright-testrc.js
.config/playwright-testrc.cjs
.config/pw-testrc.json
.config/pw-testrc.js
.config/pw-testrc.cjs
```
The config type can be imported from the entrypoint.
Expand All @@ -225,6 +238,21 @@ const config = {
export default config
```
The config file can also export a function that receives the cli options as argument.
```ts
/** @type {import('playwright-test').ConfigFn} */
function buildConfig(cliOptions) {
return {
buildConfig: {
bundle: cliOptions.mode !== 'node',
},
}
}
export default buildConfig
```
### Interface
```ts
Expand Down
14 changes: 8 additions & 6 deletions src/client/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
const config = /** @type { import('../types.js').RunnerEnv} */ (
export const env = /** @type { import('../types.js').RunnerEnv} */ (
/** @type { unknown } */ (process.env)
)

/**
* Playwright Test mode
* Playwright Test options
*
* @type {import('../types.js').RunnerOptions}
*/
export const mode = config.PW_TEST.mode
export const options = JSON.parse(env.PW_OPTIONS)

/**
* Playwright Test options
* Playwright Test mode
*/
export const options = config.PW_TEST
export const mode = options.mode

/**
* Playwright Test server url
*
* @type {string}
*/
export const server = config.PW_SERVER
export const server = env.PW_SERVER

/**
* Playwright Test browser context
Expand Down
2 changes: 1 addition & 1 deletion src/node/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class NodeRunner {
* @type {import('../types.js').RunnerEnv}
*/
this.env = merge(JSON.parse(JSON.stringify(process.env)), {
PW_TEST: this.options,
PW_OPTIONS: JSON.stringify(this.options),
NODE_ENV: 'test',
})
this.tests =
Expand Down
5 changes: 4 additions & 1 deletion src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const merge = mergeOptions.bind({ ignoreUndefined: true, concatArrays: true })
* @typedef {import('playwright-core').ChromiumBrowserContext} ChromiumBrowserContext
* @typedef {import('./types').RunnerOptions} RunnerOptions
* @typedef {import('./types').TestRunner} TestRunner
* @typedef {import('./types').RunnerEnv} RunnerEnv
* @typedef {import('./types').CliOptions} CliOptions
* @typedef {import('./types').ConfigFn} ConfigFn
*/

export class Runner {
Expand All @@ -59,7 +62,7 @@ export class Runner {

/** @type {import('./types').RunnerEnv} */
this.env = merge(JSON.parse(JSON.stringify(process.env)), {
PW_TEST: this.options,
PW_OPTIONS: JSON.stringify(this.options),
NODE_ENV: 'test',
})
this.tests =
Expand Down
5 changes: 3 additions & 2 deletions src/test-runners.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ export const mocha = {
import mocha from 'mocha/mocha.js'
${options.mode === 'node' ? `globalThis.location={}` : ``}
const { allowUncaught, bail, reporter, timeout, color, ui, grep } =
process.env.PW_TEST.testRunner.options
const options = JSON.parse(process.env.PW_OPTIONS)
const { allowUncaught, bail, reporter, timeout, color, ui, grep } = options.testRunner.options
mocha.setup({
allowUncaught,
bail,
Expand Down
35 changes: 31 additions & 4 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,43 @@ export interface RunnerOptions {
/**
* Before tests hook
*
* @param env - Runner environment. Use `env.PW_TEST` to access runner options.
* @param env - Runner environment. Use `JSON.parse(env.PW_OPTIONS)` to access runner options.
*/
beforeTests: (env: RunnerEnv) => Promise<unknown>
/**
* After tests hook
*
* @param env - Runner environment. Use `env.PW_TEST` to access runner options.
* @param env - Runner environment. Use `JSON.parse(env.PW_OPTIONS)` to access runner options.
*/
afterTests: (env: RunnerEnv) => Promise<unknown>
}

export interface RunnerEnv extends NodeJS.ProcessEnv {
PW_SERVER: string
PW_TEST: RunnerOptions
PW_OPTIONS: string
NODE_ENV: 'test'
}

export interface CliOptions {
runner: 'mocha' | 'zora' | 'tape' | 'uvu' | 'benchmark' | 'none'
browser: 'chromium' | 'firefox' | 'webkit'
mode: 'main' | 'worker' | 'node'
debug: boolean
incognito: boolean
extension: boolean
cov: boolean
reportDir: string
watch?: boolean
before?: string
sw?: string
assets: string
cwd: string
extensions: string
config?: string
}

export type ConfigFn = (options: CliOptions) => RunnerOptions

export type PwResult<TBrowser> = TBrowser extends 'webkit'
? WebKitBrowser
: TBrowser extends 'firefox'
Expand All @@ -64,7 +84,14 @@ export interface TestRunner {
*/
moduleId: string
/**
* Options made available to the compiled runtime, accessable with `process.env.PW_TEST.testRunner.options`.
* Options made available to the compiled runtime.
* This is useful to pass options to the test runner.
*
* @example
* ```js
* const options = JSON.parse(process.env.PW_OPTIONS)
* const testRunnerOptions = options.testRunner.options
* ```
*/
options?: unknown
/**
Expand Down
5 changes: 3 additions & 2 deletions src/utils/inject-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ function hrtime(previousTimestamp) {
const p = {
..._process,
exit: (code = 0) => {
const options = JSON.parse(process.env.PW_OPTIONS)
if (code === 0) {
if (process.env.PW_TEST.mode === 'worker') {
if (options.mode === 'worker') {
postMessage({
pwRunEnded: true,
pwRunFailed: false,
Expand All @@ -43,7 +44,7 @@ const p = {
globalThis.PW_TEST.end(false)
}
} else {
if (process.env.PW_TEST.mode === 'worker') {
if (options.mode === 'worker') {
postMessage({
pwRunEnded: true,
pwRunFailed: true,
Expand Down
3 changes: 2 additions & 1 deletion src/utils/proxy-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ let runningCount = 0
const signalFinished = () => {
if (runningCount === 0) {
setTimeout(() => {
const options = JSON.parse(process.env.PW_OPTIONS)
// eslint-disable-next-line no-undef
if (process.env.PW_TEST.mode === 'worker') {
if (options.mode === 'worker') {
postMessage({ pwRunEnded: true })
} else {
self.PW_TEST.end()
Expand Down

0 comments on commit 6d6892f

Please sign in to comment.