The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/packages/grafana-prometheus/src/gcopypaste/public/test/matchers/toEmitValues.test.ts

142 lines
4.5 KiB

// Core Grafana history https://github.com/grafana/grafana/blob/v11.0.0-preview/public/test/matchers/toEmitValues.test.ts
Prometheus: Create Prometheus library (#81641) * Move to the library * copy from library * move them in src * have additional files * add unmigrated/dulicated code and files * migrate from brendan's pr module.ts, query_hints.ts, tracking.ts, and remove plugin.json * migrate from brendan's pr metric_find_query.test.ts * migrate from brendan's pr language_utils.test.ts * migrate from brendan's pr index.ts in root and in configuration * migrate from brendan's pr datasource.test.ts * migrate from brendan's pr typings folder * migrate from brendan's pr querycache folder * migrate from brendan's pr monaco-query-field folder * migrate from brendan's pr components folder without monaco-query-field folder * migrate from brendan's pr configuration/overhaul folder * migrate from brendan's pr AlertingSettingsOverhaul.tsx * Remove azure related code * migrate from brendan's pr ConfigEditor.tsx, DataSourceHttpSettingsOverhaul.tsx, ExemplarSetting.tsx, configuration/mocks.ts, PromSettings.test.tsx, PromSettings.tsx * migrate from brendan's pr useFlag.ts * migrate from brendan's pr metrics-modal folder * migrate from brendan's pr files inside components folder * migrate from brendan's pr LabelFilters* files because they are now under components folder * migrate from brendan's pr files under querybuilder/shared folder * migrate from brendan's pr aggregations.ts, QueryPattern.tsx, QueryPatternsModal.tsx, state.ts, testUtils.ts under querybuilder folder * Apply Ivana's PR https://github.com/grafana/grafana/pull/81656 * Apply jack's suggestions in this PR https://github.com/grafana/grafana/pull/77762 * Apply Ivana's PR https://github.com/grafana/grafana/pull/81656 * Fix type import * add monaco-promql to transformIgnorePatterns to run prometheus frontend library tests * remove Loki specific tests because we removed Loki code to decouple Loki * add prometheus specific references * We are moving these betterer issues from core Prometheus to the Library and we promise to remove all issues in the future, thank you * include prometheus library in package.json * add yarn lock with prometheus frontend library * decouple final core import from metric_find_query.test.ts * run prettier * fix core imports in promqail * fix lint errors * run prettier * add grafana-ui to devdeps to fix lint errors * update yarn.lock * grafana-ui fix * trying to fix grafana-ui type errors with lerna drone check * trying to fix grafana-ui type errors with lerna drone check * trying to fix grafana-ui type errors with lerna drone check * trying to fix grafana-ui type errors with lerna drone check * try to pass typecheck --------- Co-authored-by: Brendan O'Handley <brendan.ohandley@grafana.com>
1 year ago
import { interval, Observable, of, throwError } from 'rxjs';
import { map, mergeMap, take } from 'rxjs/operators';
import { OBSERVABLE_TEST_TIMEOUT_IN_MS } from './types';
describe('toEmitValues matcher', () => {
describe('failing tests', () => {
describe('passing null in expect', () => {
it('should fail', async () => {
const observable = null as unknown as Observable<number>;
const rejects = expect(() => expect(observable).toEmitValues([1, 2, 3])).rejects;
await rejects.toThrow();
});
});
describe('passing undefined in expect', () => {
it('should fail', async () => {
const observable = undefined as unknown as Observable<number>;
const rejects = expect(() => expect(observable).toEmitValues([1, 2, 3])).rejects;
await rejects.toThrow();
});
});
describe('passing number instead of Observable in expect', () => {
it('should fail', async () => {
const observable = 1 as unknown as Observable<number>;
const rejects = expect(() => expect(observable).toEmitValues([1, 2, 3])).rejects;
await rejects.toThrow();
});
});
describe('wrong number of emitted values', () => {
it('should fail', async () => {
const observable = interval(10).pipe(take(3));
const rejects = expect(() => expect(observable).toEmitValues([0, 1])).rejects;
await rejects.toThrow();
});
});
describe('wrong emitted values', () => {
it('should fail', async () => {
const observable = interval(10).pipe(take(3));
const rejects = expect(() => expect(observable).toEmitValues([1, 2, 3])).rejects;
await rejects.toThrow();
});
});
describe('wrong emitted value types', () => {
it('should fail', async () => {
const observable = interval(10).pipe(take(3)) as unknown as Observable<string>;
const rejects = expect(() => expect(observable).toEmitValues(['0', '1', '2'])).rejects;
await rejects.toThrow();
});
});
describe(`observable that does not complete within ${OBSERVABLE_TEST_TIMEOUT_IN_MS}ms`, () => {
it('should fail', async () => {
const observable = interval(600);
const rejects = expect(() => expect(observable).toEmitValues([0])).rejects;
await rejects.toThrow();
});
});
});
describe('passing tests', () => {
describe('correct emitted values', () => {
it('should pass with correct message', async () => {
const observable = interval(10).pipe(take(3));
await expect(observable).toEmitValues([0, 1, 2]);
});
});
describe('using nested arrays', () => {
it('should pass with correct message', async () => {
const observable = interval(10).pipe(
map((interval) => [{ text: interval.toString(), value: interval }]),
take(3)
);
await expect(observable).toEmitValues([
[{ text: '0', value: 0 }],
[{ text: '1', value: 1 }],
[{ text: '2', value: 2 }],
]);
});
});
describe('using nested objects', () => {
it('should pass with correct message', async () => {
const observable = interval(10).pipe(
map((interval) => ({ inner: { text: interval.toString(), value: interval } })),
take(3)
);
await expect(observable).toEmitValues([
{ inner: { text: '0', value: 0 } },
{ inner: { text: '1', value: 1 } },
{ inner: { text: '2', value: 2 } },
]);
});
});
describe('correct emitted values with throw', () => {
it('should pass with correct message', async () => {
const observable = interval(10).pipe(
map((interval) => {
if (interval > 1) {
throw 'an error';
}
return interval;
})
) as unknown as Observable<string | number>;
await expect(observable).toEmitValues([0, 1, 'an error']);
});
});
describe('correct emitted values with throwError', () => {
it('should pass with correct message', async () => {
const observable = interval(10).pipe(
mergeMap((interval) => {
if (interval === 1) {
return throwError('an error');
}
return of(interval);
})
) as unknown as Observable<string | number>;
await expect(observable).toEmitValues([0, 'an error']);
});
});
});
});