diff --git a/packages/grafana-data/src/types/time.ts b/packages/grafana-data/src/types/time.ts index d195bbf251b..b417e7dd201 100644 --- a/packages/grafana-data/src/types/time.ts +++ b/packages/grafana-data/src/types/time.ts @@ -43,9 +43,11 @@ export type TimeFragment = string | DateTime; export const TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'; export function getDefaultTimeRange(): TimeRange { + const now = dateTime(); + return { - from: dateTime().subtract(6, 'hour'), - to: dateTime(), + from: dateTime(now).subtract(6, 'hour'), + to: now, raw: { from: 'now-6h', to: 'now' }, }; } diff --git a/public/app/features/explore/state/datasource.test.ts b/public/app/features/explore/state/datasource.test.ts index e742c74a3b2..1841895c664 100644 --- a/public/app/features/explore/state/datasource.test.ts +++ b/public/app/features/explore/state/datasource.test.ts @@ -22,7 +22,13 @@ describe('Datasource reducer', () => { queries, queryKeys, } as unknown) as ExploreItemState; - const expectedState: any = { + + const result = datasourceReducer( + initialState, + updateDatasourceInstanceAction({ exploreId: ExploreId.left, datasourceInstance, history: [] }) + ); + + const expectedState: Partial = { datasourceInstance, queries, queryKeys, @@ -31,13 +37,14 @@ describe('Datasource reducer', () => { tableResult: null, latency: 0, loading: false, - queryResponse: createEmptyQueryResponse(), + queryResponse: { + // When creating an empty query response we also create a timeRange object with the current time. + // Copying the range from the reducer here prevents intermittent failures when creating them at different times. + ...createEmptyQueryResponse(), + timeRange: result.queryResponse.timeRange, + }, }; - const result = datasourceReducer( - initialState, - updateDatasourceInstanceAction({ exploreId: ExploreId.left, datasourceInstance, history: [] }) - ); expect(result).toMatchObject(expectedState); }); });