Skip to content

Commit 7bd5d2a

Browse files
ferrandiazsorccu
andauthored
feat: allow passing playwright config path at a check level (#1154)
* feat: allow passing playwright config path at a check level * fix: pw-test should load playwright config even if not defined on checkly config * fix: pw-test * chore: remove unused import * chore: log command output in failing test if exit code is not as expected * fix: must install dependencies in fixture or we can't resolve pw correctly --------- Co-authored-by: Simo Kinnunen <[email protected]>
1 parent 3676329 commit 7bd5d2a

File tree

5 files changed

+66
-28
lines changed

5 files changed

+66
-28
lines changed

packages/cli/e2e/__tests__/pw-test.spec.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,39 @@ import path from 'node:path'
22
import fs from 'node:fs'
33

44
import config from 'config'
5-
import { describe, it, expect, afterEach } from 'vitest'
5+
import { describe, it, expect, afterEach, beforeAll } from 'vitest'
66

77
import { runChecklyCli } from '../run-checkly'
88
import { loadChecklyConfig } from '../../src/services/checkly-config-loader'
99

10+
const FIXTURE_TEST_PWT_NATIVE = path.join(__dirname, 'fixtures', 'test-pwt-native')
11+
1012
describe('pw-test', { timeout: 45000 }, () => {
1113
afterEach(() => {
12-
const configPath = path.join(__dirname, 'fixtures', 'test-pwt-native')
13-
fs.copyFileSync(path.join(configPath, 'checkly.config.original.ts'), path.join(configPath, 'checkly.config.ts'))
14+
fs.copyFileSync(
15+
path.join(FIXTURE_TEST_PWT_NATIVE, 'checkly.config.original.ts'),
16+
path.join(FIXTURE_TEST_PWT_NATIVE, 'checkly.config.ts'),
17+
)
18+
})
19+
20+
beforeAll(async () => {
21+
// Install fixture dependencies or they will not resolve correctly.
22+
const { execa } = await import('execa')
23+
await execa('npm', ['install'], { cwd: FIXTURE_TEST_PWT_NATIVE })
1424
})
1525

1626
it('Playwright test should run successfully', async () => {
1727
const result = await runChecklyCli({
1828
args: ['pw-test', '--', `--grep`, '@TAG-B'],
1929
apiKey: config.get('apiKey'),
2030
accountId: config.get('accountId'),
21-
directory: path.join(__dirname, 'fixtures', 'test-pwt-native'),
31+
directory: FIXTURE_TEST_PWT_NATIVE,
2232
timeout: 120000, // 2 minutes
2333
})
34+
if (result.status !== 0) {
35+
// eslint-disable-next-line no-console
36+
console.log(result)
37+
}
2438
expect(result.status).toBe(0)
2539
}, 130000)
2640

@@ -29,11 +43,11 @@ describe('pw-test', { timeout: 45000 }, () => {
2943
args: ['pw-test', '--create-check', '--', `--grep`, '@TAG-B'],
3044
apiKey: config.get('apiKey'),
3145
accountId: config.get('accountId'),
32-
directory: path.join(__dirname, 'fixtures', 'test-pwt-native'),
46+
directory: FIXTURE_TEST_PWT_NATIVE,
3347
timeout: 120000, // 2 minutes
3448
})
3549
expect(result.status).toBe(0)
36-
const checklyConfig = await loadChecklyConfig(path.join(__dirname, 'fixtures', 'test-pwt-native'))
50+
const checklyConfig = await loadChecklyConfig(FIXTURE_TEST_PWT_NATIVE)
3751
expect(checklyConfig.config?.checks).toBeDefined()
3852
expect(checklyConfig.config?.checks?.playwrightConfigPath).toBe('./playwright.config.ts')
3953
expect(checklyConfig.config?.checks?.playwrightChecks).toBeDefined()

packages/cli/src/commands/pw-test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AuthCommand } from './authCommand'
22
import {
3+
findPlaywrightConfigPath,
34
getCiInformation,
45
getDefaultChecklyConfig,
56
getEnvs,
@@ -115,8 +116,20 @@ export default class PwTestCommand extends AuthCommand {
115116
config: checklyConfig,
116117
constructs: checklyConfigConstructs,
117118
} = await loadChecklyConfig(configDirectory, configFilenames, false, pwPathFlag)
118-
const playwrightConfigPath = checklyConfig.checks?.playwrightConfigPath
119-
const dir = path.dirname(playwrightConfigPath || '.')
119+
let playwrightConfigPath = pwPathFlag ?? checklyConfig.checks?.playwrightConfigPath
120+
121+
if (!playwrightConfigPath) {
122+
const foundPath = findPlaywrightConfigPath(configDirectory)
123+
if (!foundPath) {
124+
this.style.actionFailure()
125+
this.style.shortError('No Playwright config found. You can specify a custom path using the playwright --config flag.')
126+
this.exit(1)
127+
}
128+
playwrightConfigPath = `./${path.relative(configDirectory, foundPath)}`
129+
}
130+
131+
const absoluteConfigPath = path.resolve(configDirectory, playwrightConfigPath)
132+
const dir = path.dirname(absoluteConfigPath)
120133
const playwrightCheck = await PwTestCommand.createPlaywrightCheck(
121134
playwrightFlags,
122135
runLocation as keyof Region,

packages/cli/src/services/checkly-config-loader.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as path from 'path'
22
import fs from 'node:fs/promises'
3-
import { existsSync } from 'fs'
4-
import { getDefaultChecklyConfig, writeChecklyConfigFile } from './util'
3+
import { findPlaywrightConfigPath, getDefaultChecklyConfig, writeChecklyConfigFile } from './util'
54
import { CheckProps, RuntimeCheckProps } from '../constructs/check'
65
import { PlaywrightCheckProps } from '../constructs/playwright-check'
76
import { Session } from '../constructs'
@@ -35,7 +34,7 @@ export type CheckConfigDefaults =
3534
export type PlaywrightSlimmedProp = Pick<PlaywrightCheckProps, 'name' | 'activated'
3635
| 'muted' | 'shouldFail' | 'locations' | 'tags' | 'frequency' | 'environmentVariables'
3736
| 'alertChannels' | 'privateLocations' | 'retryStrategy' | 'alertEscalationPolicy'
38-
| 'pwProjects' | 'pwTags' | 'installCommand' | 'testCommand' | 'group' | 'groupName' | 'runParallel'> & { logicalId: string }
37+
| 'pwProjects' | 'pwTags' | 'installCommand' | 'testCommand' | 'group' | 'groupName' | 'runParallel'> & { logicalId: string, playwrightConfigPath?: string }
3938

4039
export type ChecklyConfig = {
4140
/**
@@ -192,12 +191,6 @@ async function handleMissingConfig (
192191
throw new ConfigNotFoundError(`Unable to locate a config at ${dir} with ${filenames.join(', ')}.`)
193192
}
194193

195-
function findPlaywrightConfigPath (dir: string): string | undefined {
196-
return ['playwright.config.ts', 'playwright.config.js']
197-
.map(file => path.resolve(dir, file))
198-
.find(filePath => existsSync(filePath))
199-
}
200-
201194
function validateConfigFields (config: ChecklyConfig, fields: (keyof ChecklyConfig)[]): void {
202195
for (const field of fields) {
203196
if (!config?.[field] || !isString(config[field])) {

packages/cli/src/services/project-parser.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as path from 'path'
22
import {
3-
findFilesWithPattern,
4-
3+
findFilesWithPattern, getPlaywrightConfigPath,
54
pathToPosix,
65
} from './util'
76
import {
@@ -121,21 +120,15 @@ async function loadPlaywrightChecks (
121120
playwrightConfigPath?: string,
122121
include?: string | string[],
123122
) {
124-
if (!playwrightConfigPath) {
125-
return
126-
}
127-
128-
// Resolve the playwrightConfigPath relative to the project directory
129-
const resolvedPlaywrightConfigPath = path.resolve(directory, playwrightConfigPath)
130-
131123
if (playwrightChecks?.length) {
132124
try {
133-
setCheckFilePaths(playwrightConfigPath, directory)
134125
for (const playwrightCheckProps of playwrightChecks) {
126+
const configPath = getPlaywrightConfigPath(playwrightCheckProps, playwrightConfigPath, directory)
127+
setCheckFilePaths(configPath, directory)
135128
// eslint-disable-next-line @typescript-eslint/no-unused-vars
136129
const playwrightCheck = new PlaywrightCheck(playwrightCheckProps.logicalId, {
137130
...playwrightCheckProps,
138-
playwrightConfigPath: resolvedPlaywrightConfigPath,
131+
playwrightConfigPath: configPath,
139132
include,
140133
})
141134
}
@@ -144,7 +137,11 @@ async function loadPlaywrightChecks (
144137
}
145138
} else {
146139
try {
140+
if (!playwrightConfigPath) {
141+
return
142+
}
147143
setCheckFilePaths(playwrightConfigPath, directory)
144+
const resolvedPlaywrightConfigPath = path.resolve(directory, playwrightConfigPath)
148145
const basePath = path.basename(resolvedPlaywrightConfigPath)
149146
// eslint-disable-next-line @typescript-eslint/no-unused-vars
150147
const playwrightCheck = new PlaywrightCheck(basePath, {

packages/cli/src/services/util.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
PNpmDetector,
2727
YarnDetector,
2828
} from './check-parser/package-files/package-manager'
29+
import { existsSync } from 'fs'
2930

3031
export interface GitInformation {
3132
commitId: string
@@ -358,3 +359,23 @@ export async function writeChecklyConfigFile (dir: string, config: ChecklyConfig
358359

359360
await fs.writeFile(configFile, configContent, { encoding: 'utf-8' })
360361
}
362+
363+
export function getPlaywrightConfigPath (
364+
playwrightCheckProps: PlaywrightSlimmedProp,
365+
playwrightConfigPath: string | undefined,
366+
dir: string,
367+
): string {
368+
if (playwrightCheckProps.playwrightConfigPath) {
369+
return path.resolve(dir, playwrightCheckProps.playwrightConfigPath)
370+
} else if (playwrightConfigPath) {
371+
return path.resolve(dir, playwrightConfigPath)
372+
} else {
373+
throw new Error('No Playwright config path provided.')
374+
}
375+
}
376+
377+
export function findPlaywrightConfigPath (dir: string): string | undefined {
378+
return ['playwright.config.ts', 'playwright.config.js']
379+
.map(file => path.resolve(dir, file))
380+
.find(filePath => existsSync(filePath))
381+
}

0 commit comments

Comments
 (0)