Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow configuring the CLI to error on no matching tests #984

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/cli/e2e/__tests__/trigger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,20 @@ describe('trigger', () => {
expect(result.stdout).toContain('No matching checks were found.')
expect(result.status).toBe(0)
})

test('Should return code 1 when no checks match and the fail-on-no-match flag is set', async () => {
const result = await runChecklyCli({
args: [
'trigger',
'--tags',
imbstack marked this conversation as resolved.
Show resolved Hide resolved
'no-checks-match-this-tag',
'--fail-on-no-matching',
],
apiKey: config.get('apiKey'),
accountId: config.get('accountId'),
})

expect(result.stdout).toContain('No matching checks were found.')
expect(result.status).toBe(1)
})
})
12 changes: 12 additions & 0 deletions packages/cli/src/commands/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export default class Trigger extends AuthCommand {
description: 'Always show the full logs of the checks.',
allowNo: true,
}),
'fail-on-no-matching': Flags.boolean({
description: 'Exit with a failing status code when there are no matching tests.',
}),
reporter: Flags.string({
char: 'r',
description: 'A list of custom reporters for the test output.',
Expand Down Expand Up @@ -96,6 +99,7 @@ export default class Trigger extends AuthCommand {
tags: targetTags,
timeout,
verbose: verboseFlag,
'fail-on-no-matching': failOnNoMatchingFlag,
record: shouldRecord,
reporter: reporterFlag,
env,
Expand All @@ -116,6 +120,7 @@ export default class Trigger extends AuthCommand {
privateRunLocation,
})
const verbose = this.prepareVerboseFlag(verboseFlag, checklyConfig?.cli?.verbose)
const failOnNoMatching = this.prepareFailOnNoMatching(failOnNoMatchingFlag, checklyConfig?.cli?.failOnNoMatching)
const reporterTypes = this.prepareReportersTypes(reporterFlag as ReporterType, checklyConfig?.cli?.reporters)
const reporters = createReporters(reporterTypes, location, verbose)
const testRetryStrategy = this.prepareTestRetryStrategy(retries, checklyConfig?.cli?.retries)
Expand Down Expand Up @@ -167,6 +172,9 @@ export default class Trigger extends AuthCommand {
if (err instanceof NoMatchingChecksError) {
// For consistency with `checkly test`, we log a message and exit with code 0.
this.log('No matching checks were found.')
if (failOnNoMatching) {
process.exitCode = 1
}
return
}
reporters.forEach(r => r.onError(err))
Expand Down Expand Up @@ -221,6 +229,10 @@ export default class Trigger extends AuthCommand {
return verboseFlag ?? cliVerboseFlag ?? false
}

prepareFailOnNoMatching (failOnNoMatchingFlag?: boolean, cliFailOnNoMatchingFlag?: boolean) {
return failOnNoMatchingFlag ?? cliFailOnNoMatchingFlag ?? false
}

prepareReportersTypes (reporterFlag: ReporterType, cliReporters: ReporterType[] = []): ReporterType[] {
if (!reporterFlag && !cliReporters.length) {
return [isCI ? 'ci' : 'list']
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/services/checkly-config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export type ChecklyConfig = {
runLocation?: keyof Region,
privateRunLocation?: string,
verbose?: boolean,
failOnNoMatching?: boolean,
reporters?: ReporterType[],
retries?: number,
}
Expand Down