Skip to content

Commit

Permalink
feat(e2e-runner): Support injecting env variables into checkUrl
Browse files Browse the repository at this point in the history
Fixes #88
  • Loading branch information
TriPSs committed Dec 19, 2023
1 parent f7f630c commit 321e745
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/e2e-runner/src/executors/run/run.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function endToEndRunner(

const { runner, targets, ...rest } = options

runningTargets = targets.map((targetOptions) => new NxTarget(targetOptions, options))
runningTargets = targets.map((targetOptions) => new NxTarget(targetOptions))

try {
// Start all targets
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NxTarget } from '../nx-target'

describe('NxTarget', () => {
it('should inject env variables in "checkUrl" option', () => {
process.env.SUPER_FAKE_PORT = '3000'

const target = new NxTarget({
target: 'fake',
checkUrl: 'http://LOCALHOST:$SUPER_FAKE_PORT/$DOES_NOT_EXIST'
})

// @ts-expect-error is a private prop
expect(target.options.checkUrl).toEqual('http://LOCALHOST:3000/$DOES_NOT_EXIST')
})
})
26 changes: 20 additions & 6 deletions packages/e2e-runner/src/executors/run/utils/nx-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { readCachedProjectGraph } from '@nx/workspace/src/core/project-graph'
import { getPackageManagerExecCommand, USE_VERBOSE_LOGGING } from '@nx-extend/core'
import * as childProcess from 'child_process'

import type { RunOptions } from '../run.impl'

import { isApiLive } from './is-api-live'
import { wait } from './wait'

Expand All @@ -24,13 +22,11 @@ export class NxTarget {

private readonly isAvailable: () => Promise<boolean>
private readonly options: NxTargetOptions
private readonly runOptions: RunOptions

private killed = false

constructor(options: NxTargetOptions, runOptions: RunOptions) {
this.options = options
this.runOptions = runOptions
constructor(options: NxTargetOptions) {
this.options = this.processOptions(options)

this.isAvailable = () => isApiLive(options.checkUrl, {
rejectUnauthorized: options.rejectUnauthorized
Expand Down Expand Up @@ -61,6 +57,24 @@ export class NxTarget {
this.killed = true
}

private processOptions(options: NxTargetOptions): NxTargetOptions {
if (options.checkUrl.includes('$')) {
const variables = options.checkUrl.match(/\$[A-Z_-]+/g)

if (variables && variables.length > 0) {
for (const variable of variables) {
const envVariable = variable.replace('$', '')

if (envVariable in process.env) {
options.checkUrl = options.checkUrl.replace(variable, process.env[envVariable])
}
}
}
}

return options
}

private async startProcess(): Promise<void> {
let processExitedReject = (error: Error) => {}

Expand Down

0 comments on commit 321e745

Please sign in to comment.