diff --git a/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.test.ts b/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.test.ts index 25c25318896..416099f72bc 100644 --- a/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.test.ts +++ b/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.test.ts @@ -48,6 +48,32 @@ describe('LegacyAnnotationQueryRunner', () => { }); }); + describe('when run is called without a valid datasource', () => { + it('then it should return empty results when datasource is undefined', async () => { + const datasource = undefined; + const options = { ...getDefaultOptions(), datasource }; + + await expect(runner.run(options)).toEmitValuesWith((received) => { + expect(received).toHaveLength(1); + const results = received[0]; + expect(results).toEqual([]); + }); + }); + + it('then it should return empty results when annotationQuery is undefined', async () => { + const datasource = { + annotationQuery: undefined, + } as unknown as DataSourceApi; + const options = { ...getDefaultOptions(), datasource }; + + await expect(runner.run(options)).toEmitValuesWith((received) => { + expect(received).toHaveLength(1); + const results = received[0]; + expect(results).toEqual([]); + }); + }); + }); + describe('when canWork is called with incorrect props', () => { it('then it should return false', () => { const datasource = { diff --git a/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.ts b/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.ts index f383897f5d9..be28bbbb533 100644 --- a/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.ts +++ b/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.ts @@ -25,8 +25,17 @@ export class LegacyAnnotationQueryRunner implements AnnotationQueryRunner { return of([]); } - return from(datasource!.annotationQuery!({ range, rangeRaw: range.raw, annotation, dashboard })).pipe( - catchError(handleAnnotationQueryRunnerError) - ); + if (datasource?.annotationQuery === undefined) { + console.warn('datasource does not have an annotation query'); + return of([]); + } + + const annotationQuery = datasource.annotationQuery({ range, rangeRaw: range.raw, annotation, dashboard }); + if (annotationQuery === undefined) { + console.warn('datasource does not have an annotation query'); + return of([]); + } + + return from(annotationQuery).pipe(catchError(handleAnnotationQueryRunnerError)); } }