@@ -11,9 +11,10 @@ import {
1111 ConflictingPropertyDiagnostic ,
1212 DeprecatedPropertyDiagnostic ,
1313 InvalidPropertyValueDiagnostic ,
14+ UnsatisfiedLocalPrerequisitesDiagnostic ,
1415 UnsupportedPropertyDiagnostic ,
1516} from './construct-diagnostics'
16- import { Diagnostics } from './diagnostics'
17+ import { Diagnostics , ErrorDiagnostic } from './diagnostics'
1718import { PlaywrightCheckBundle } from './playwright-check-bundle'
1819import { Session } from './project'
1920import { Ref } from './ref'
@@ -126,7 +127,7 @@ export interface PlaywrightCheckProps extends Omit<RuntimeCheckProps, 'retryStra
126127 */
127128export class PlaywrightCheck extends RuntimeCheck {
128129 installCommand ?: string
129- testCommand : string
130+ testCommand ? : string
130131 playwrightConfigPath : string
131132 pwProjects : string [ ]
132133 pwTags : string [ ]
@@ -149,7 +150,7 @@ export class PlaywrightCheck extends RuntimeCheck {
149150 this . include = config . include
150151 ? ( Array . isArray ( config . include ) ? config . include : [ config . include ] )
151152 : [ ]
152- this . testCommand = config . testCommand ?? 'npx playwright test'
153+ this . testCommand = config . testCommand
153154 this . groupName = config . groupName
154155 this . playwrightConfigPath = this . resolveContentFilePath ( config . playwrightConfigPath )
155156 Session . registerConstruct ( this )
@@ -240,6 +241,7 @@ export class PlaywrightCheck extends RuntimeCheck {
240241
241242 async validate ( diagnostics : Diagnostics ) : Promise < void > {
242243 await super . validate ( diagnostics )
244+ await this . #validateWorkspace( diagnostics )
243245 await this . validateRetryStrategy ( diagnostics )
244246
245247 try {
@@ -254,6 +256,29 @@ export class PlaywrightCheck extends RuntimeCheck {
254256 this . #validateGroupReferences( diagnostics )
255257 }
256258
259+ // eslint-disable-next-line require-await
260+ async #validateWorkspace ( diagnostics : Diagnostics ) : Promise < void > {
261+ const workspace = Session . workspace
262+ if ( workspace . isOk ( ) ) {
263+ const lockfile = workspace . ok ( ) . lockfile
264+ if ( lockfile . isErr ( ) ) {
265+ diagnostics . add ( new UnsatisfiedLocalPrerequisitesDiagnostic ( new Error (
266+ `A lockfile is required for Playwright checks, but none could be `
267+ + `detected.`
268+ + '\n\n'
269+ + `Cause: ${ lockfile . err ( ) . message } ` ,
270+ ) ) )
271+ }
272+ } else if ( workspace . isErr ( ) ) {
273+ diagnostics . add ( new UnsatisfiedLocalPrerequisitesDiagnostic ( new Error (
274+ `A workspace is required for Playwright checks, but none could be `
275+ + `detected.`
276+ + '\n\n'
277+ + `Cause: ${ workspace . err ( ) . message } ` ,
278+ ) ) )
279+ }
280+ }
281+
257282 #validateGroupReferences ( diagnostics : Diagnostics ) : void {
258283 if ( this . groupName ) {
259284 diagnostics . add ( new DeprecatedPropertyDiagnostic (
@@ -295,6 +320,12 @@ export class PlaywrightCheck extends RuntimeCheck {
295320 return `${ testCommand } --config ${ quotedPath } ${ projectArg } ${ tagArg } `
296321 }
297322
323+ static contextifyCommand ( command : string ) : string {
324+ return Session . basePath === Session . contextPath
325+ ? command
326+ : `env --chdir "${ Session . relativePosixPath ( Session . contextPath ! ) } " -- ${ command } `
327+ }
328+
298329 static async bundleProject ( playwrightConfigPath : string , include : string [ ] ) {
299330 let dir = ''
300331 try {
@@ -333,12 +364,12 @@ export class PlaywrightCheck extends RuntimeCheck {
333364 relativePlaywrightConfigPath,
334365 } = await PlaywrightCheck . bundleProject ( this . playwrightConfigPath , this . include ?? [ ] )
335366
336- const testCommand = PlaywrightCheck . buildTestCommand (
337- this . testCommand ,
367+ const testCommand = PlaywrightCheck . contextifyCommand ( PlaywrightCheck . buildTestCommand (
368+ this . testCommand ?? this . #defaultTestCommand ( ) ,
338369 relativePlaywrightConfigPath ,
339370 this . pwProjects ,
340371 this . pwTags ,
341- )
372+ ) )
342373
343374 return new PlaywrightCheckBundle ( this , {
344375 groupId,
@@ -351,6 +382,10 @@ export class PlaywrightCheck extends RuntimeCheck {
351382 } )
352383 }
353384
385+ #defaultTestCommand ( ) : string {
386+ return Session . packageManager . execCommand ( [ 'playwright' , 'test' ] ) . unsafeDisplayCommand
387+ }
388+
354389 synthesize ( ) {
355390 return {
356391 ...super . synthesize ( ) ,
0 commit comments