Skip to content

Commit

Permalink
feat: add server url to the client
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Nov 9, 2023
1 parent 993c04e commit 454abec
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 15 deletions.
4 changes: 4 additions & 0 deletions mocks/test.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ describe('Array', () => {
equal(import.meta.env, process.env)
})

it('should has server', async () => {
equal(process.env.PW_SERVER, Client.server)
})

it('should setoffline', async () => {
if (Client.mode === 'main' && Client.options.extension === false) {
globalThis.addEventListener('offline', () => {
Expand Down
27 changes: 23 additions & 4 deletions src/client/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
const config = /** @type { import('../types.js').RunnerOptions} */ (
/** @type { unknown } */ (process.env.PW_TEST)
const config = /** @type { import('../types.js').RunnerEnv} */ (
/** @type { unknown } */ (process.env)
)

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

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

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

/**
* Playwright Test browser context
*
* Methods to interact with the browser context
*
*/
export const context = {
/** @type {import('playwright-core').BrowserContext['setOffline']} */
setOffline(offline) {
Expand Down
35 changes: 34 additions & 1 deletion src/node/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { nanoid } from 'nanoid'
import { watch } from 'chokidar'
import { execa } from 'execa'
import { premove } from 'premove'
import { findTests, log } from '../utils/index.js'
import { createPolka, findTests, log } from '../utils/index.js'
import * as DefaultRunners from '../test-runners.js'
import { build } from './utils.js'

Expand Down Expand Up @@ -57,13 +57,18 @@ export class NodeRunner {
constructor(options = {}, testFiles) {
/** @type {import('../types.js').RunnerOptions} */
this.options = merge(defaultOptions, options)
/** @type {import('polka').Polka["server"] | undefined} */
this.server = undefined
this.dir = path.join(__dirname, '../.tmp', nanoid())
mkdirSync(this.dir, {
recursive: true,
})
this.stopped = false
this.watching = false
this.beforeTestsOutput = undefined
/**
* @type {import('../types.js').RunnerEnv}
*/
this.env = merge(JSON.parse(JSON.stringify(process.env)), {
PW_TEST: this.options,
NODE_ENV: 'test',
Expand All @@ -81,6 +86,17 @@ export class NodeRunner {
}
}

async #setupServer() {
// setup http server
const { server, url } = await createPolka(
this.dir,
this.options.cwd,
this.options.assets
)
this.env.PW_SERVER = url
this.server = server
}

/**
* Run the tests
*
Expand All @@ -104,6 +120,8 @@ export class NodeRunner {
asyncExitHook(this.#clean.bind(this), {
wait: 1000,
})

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

try {
Expand Down Expand Up @@ -148,6 +166,7 @@ export class NodeRunner {
wait: 1000,
})

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

const { files, outName } = await this.runTests()
Expand Down Expand Up @@ -191,6 +210,20 @@ export class NodeRunner {
// Run after tests hook
await this.options.afterTests(this.options, this.beforeTestsOutput)
await premove(this.dir)
const serverClose = new Promise((resolve, reject) => {
if (this.server) {
this.server.close((err) => {
if (err) {
return reject(err)
}
resolve(true)
})
} else {
resolve(true)
}
})

await serverClose
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export class Runner {
this.url = ''
this.stopped = false
this.watching = false

/** @type {import('./types').RunnerEnv} */
this.env = merge(JSON.parse(JSON.stringify(process.env)), {
PW_TEST: this.options,
NODE_ENV: 'test',
Expand All @@ -81,7 +83,14 @@ export class Runner {
await cpy(path.join(__dirname, './../static') + '/**', this.dir)

// setup http server
await createPolka(this)
const { server, url } = await createPolka(
this.dir,
this.options.cwd,
this.options.assets
)
this.env.PW_SERVER = url
this.url = url
this.server = server

// download playwright if needed
const pw = await getPw(this.options.browser)
Expand Down
6 changes: 6 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export interface RunnerOptions {
) => Promise<unknown>
}

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

export type PwResult<TBrowser> = TBrowser extends 'webkit'
? WebKitBrowser
: TBrowser extends 'firefox'
Expand Down
23 changes: 14 additions & 9 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,17 +601,22 @@ function getPort(port = 3000, host = '127.0.0.1') {
}

/**
* @param {import('../runner').Runner} runner
* Create polka server
*
* @param {string} dir - Runner directory
* @param {string} cwd - Current working directory
* @param {string} assets - Assets directory
* @returns {Promise<{ url: string; server: import('http').Server }>}
*/
export async function createPolka(runner) {
export async function createPolka(dir, cwd, assets) {
const host = '127.0.0.1'
const port = await getPort(0, host)
const url = `http://${host}:${port}/`
return new Promise((resolve, reject) => {
const { server } = polka()
.use(
// @ts-ignore
sirv(runner.dir, {
sirv(dir, {
dev: true,
setHeaders: (
/** @type {{ setHeader: (arg0: string, arg1: string) => void; }} */ rsp,
Expand All @@ -626,19 +631,19 @@ export async function createPolka(runner) {
)
.use(
// @ts-ignore
sirv(path.join(runner.options.cwd, runner.options.assets), {
sirv(path.join(cwd, assets), {
dev: true,
})
)
.listen(port, host, (/** @type {Error} */ err) => {
if (err) {
reject(err)
return reject(err)
}

return
if (!server) {
return reject(new Error('No server'))
}
runner.url = url
runner.server = server
resolve(true)
resolve({ url, server })
})
})
}

0 comments on commit 454abec

Please sign in to comment.