Skip to content

Commit

Permalink
feat!: before/after hooks receive the runner env instead of just the …
Browse files Browse the repository at this point in the history
…options.

Also the output of before hook will not be inputed to the after hook anymore.
  • Loading branch information
hugomrdias committed Nov 13, 2023
1 parent 702eae6 commit b392f7d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
7 changes: 3 additions & 4 deletions src/node/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export class NodeRunner {
})
this.stopped = false
this.watching = false
this.beforeTestsOutput = undefined
/**
* @type {import('../types.js').RunnerEnv}
*/
Expand Down Expand Up @@ -122,7 +121,7 @@ export class NodeRunner {
})

await this.#setupServer()
this.beforeTestsOutput = await this.options.beforeTests(this.options)
await this.options.beforeTests(this.env)

try {
const { outName } = await this.runTests()
Expand Down Expand Up @@ -167,7 +166,7 @@ export class NodeRunner {
})

await this.#setupServer()
this.beforeTestsOutput = await this.options.beforeTests(this.options)
await this.options.beforeTests(this.env)

const { files, outName } = await this.runTests()
try {
Expand Down Expand Up @@ -208,7 +207,7 @@ export class NodeRunner {

async #clean() {
// Run after tests hook
await this.options.afterTests(this.options, this.beforeTestsOutput)
await this.options.afterTests(this.env)
await premove(this.dir)
const serverClose = new Promise((resolve, reject) => {
if (this.server) {
Expand Down
9 changes: 3 additions & 6 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export class Runner {
PW_TEST: this.options,
NODE_ENV: 'test',
})
this.beforeTestsOutput = undefined
this.tests =
testFiles ??
findTests({
Expand Down Expand Up @@ -305,11 +304,10 @@ export class Runner {
wait: 1000,
})

this.beforeTestsOutput = await this.options.beforeTests(this.options)

try {
// Setup the context
const context = await this.setupContext()
this.beforeTestsOutput = await this.options.beforeTests(this.env)

// Run the before script
if (this.options.before) {
Expand Down Expand Up @@ -396,10 +394,9 @@ export class Runner {
wait: 1000,
})

this.beforeTestsOutput = await this.options.beforeTests(this.options)

// Setup the context
const context = await this.setupContext()
await this.options.beforeTests(this.env)

// Run the before script
if (this.options.before) {
Expand Down Expand Up @@ -436,7 +433,7 @@ export class Runner {

async #clean() {
// Run after tests hook
await this.options.afterTests(this.options, this.beforeTestsOutput)
await this.options.afterTests(this.env)

premove(this.dir)

Expand Down
25 changes: 16 additions & 9 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ export interface RunnerOptions {
buildConfig: BuildOptions
buildSWConfig: BuildOptions
browserContextOptions?: BrowserContextOptions
beforeTests: (opts: RunnerOptions) => Promise<unknown>
afterTests: (
opts: RunnerOptions,
beforeTestsOutput: unknown
) => Promise<unknown>
/**
* Before tests hook
*
* @param env - Runner environment. Use `env.PW_TEST` to access runner options.
*/
beforeTests: (env: RunnerEnv) => Promise<unknown>
/**
* After tests hook
*
* @param env - Runner environment. Use `env.PW_TEST` to access runner options.
*/
afterTests: (env: RunnerEnv) => Promise<unknown>
}

export interface RunnerEnv extends NodeJS.ProcessEnv {
Expand All @@ -40,10 +47,10 @@ export interface RunnerEnv extends NodeJS.ProcessEnv {
export type PwResult<TBrowser> = TBrowser extends 'webkit'
? WebKitBrowser
: TBrowser extends 'firefox'
? FirefoxBrowser
: TBrowser extends 'chromium'
? ChromiumBrowser
: never
? FirefoxBrowser
: TBrowser extends 'chromium'
? ChromiumBrowser
: never

export interface CompilerOutput {
outName: string
Expand Down

0 comments on commit b392f7d

Please sign in to comment.