Skip to content

Commit 177e5fc

Browse files
authored
feat: allow configuring the CLI to error on no matching tests (#984)
1 parent c5fd24f commit 177e5fc

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

packages/cli/e2e/__tests__/trigger.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,20 @@ describe('trigger', () => {
6565
expect(result.stdout).toContain('No matching checks were found.')
6666
expect(result.status).toBe(0)
6767
})
68+
69+
test('Should return code 1 when no checks match and the fail-on-no-match flag is set', async () => {
70+
const result = await runChecklyCli({
71+
args: [
72+
'trigger',
73+
'--tags',
74+
'no-checks-match-this-tag',
75+
'--fail-on-no-matching',
76+
],
77+
apiKey: config.get('apiKey'),
78+
accountId: config.get('accountId'),
79+
})
80+
81+
expect(result.stdout).toContain('No matching checks were found.')
82+
expect(result.status).toBe(1)
83+
})
6884
})

packages/cli/src/commands/trigger.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export default class Trigger extends AuthCommand {
5858
description: 'Always show the full logs of the checks.',
5959
allowNo: true,
6060
}),
61+
'fail-on-no-matching': Flags.boolean({
62+
description: 'Exit with a failing status code when there are no matching tests.',
63+
}),
6164
reporter: Flags.string({
6265
char: 'r',
6366
description: 'A list of custom reporters for the test output.',
@@ -96,6 +99,7 @@ export default class Trigger extends AuthCommand {
9699
tags: targetTags,
97100
timeout,
98101
verbose: verboseFlag,
102+
'fail-on-no-matching': failOnNoMatchingFlag,
99103
record: shouldRecord,
100104
reporter: reporterFlag,
101105
env,
@@ -116,6 +120,7 @@ export default class Trigger extends AuthCommand {
116120
privateRunLocation,
117121
})
118122
const verbose = this.prepareVerboseFlag(verboseFlag, checklyConfig?.cli?.verbose)
123+
const failOnNoMatching = this.prepareFailOnNoMatching(failOnNoMatchingFlag, checklyConfig?.cli?.failOnNoMatching)
119124
const reporterTypes = this.prepareReportersTypes(reporterFlag as ReporterType, checklyConfig?.cli?.reporters)
120125
const reporters = createReporters(reporterTypes, location, verbose)
121126
const testRetryStrategy = this.prepareTestRetryStrategy(retries, checklyConfig?.cli?.retries)
@@ -167,6 +172,9 @@ export default class Trigger extends AuthCommand {
167172
if (err instanceof NoMatchingChecksError) {
168173
// For consistency with `checkly test`, we log a message and exit with code 0.
169174
this.log('No matching checks were found.')
175+
if (failOnNoMatching) {
176+
process.exitCode = 1
177+
}
170178
return
171179
}
172180
reporters.forEach(r => r.onError(err))
@@ -221,6 +229,10 @@ export default class Trigger extends AuthCommand {
221229
return verboseFlag ?? cliVerboseFlag ?? false
222230
}
223231

232+
prepareFailOnNoMatching (failOnNoMatchingFlag?: boolean, cliFailOnNoMatchingFlag?: boolean) {
233+
return failOnNoMatchingFlag ?? cliFailOnNoMatchingFlag ?? false
234+
}
235+
224236
prepareReportersTypes (reporterFlag: ReporterType, cliReporters: ReporterType[] = []): ReporterType[] {
225237
if (!reporterFlag && !cliReporters.length) {
226238
return [isCI ? 'ci' : 'list']

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export type ChecklyConfig = {
6767
runLocation?: keyof Region,
6868
privateRunLocation?: string,
6969
verbose?: boolean,
70+
failOnNoMatching?: boolean,
7071
reporters?: ReporterType[],
7172
retries?: number,
7273
}

0 commit comments

Comments
 (0)