kibana/x-pack/test/api_integration/services/usage_api.ts
Dzmitry Lemechko fa69337c94
[FTR] enable recommended mocha + no-floating-promises ESLint rules (#190690)
## Summary

This PR enforces ESLint rules in FTR tests, in particular:
- `no-floating-promises` rule to catch unawaited Promises in
tests/services/page objects

_Why is it important?_
- Keep correct test execution order: cleanup code may run before the
async operation is completed, leading to unexpected behavior in
subsequent tests
- Accurate test results: If a test completes before an async operation
(e.g., API request) has finished, Mocha might report the test as passed
or failed based on incomplete context.
```
198:11  error  Promises must be awaited, end with a call to .catch, end with a call to .then
with a rejection handler or be explicitly marked as ignored with the `void` operator 
@typescript-eslint/no-floating-promises
```
<img width="716" alt="Screenshot 2024-08-20 at 14 04 43"
src="https://github.com/user-attachments/assets/9afffe4c-4b51-4790-964c-c44a76baed1e">

- recommended rules from
[eslint-mocha-plugin](https://www.npmjs.com/package/eslint-plugin-mocha)
including:
-
[no-async-describe](https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/docs/rules/no-async-describe.md)
-
[no-identical-title.md](https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/docs/rules/no-identical-title.md)
-
[no-sibling-hooks.md](https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/docs/rules/no-sibling-hooks.md)
and others

Note for reviewers: some tests were skipped due to failures after
missing `await` was added. Most likely is a "false positive" case when
test is finished before async operation is actually completed. Please
work on fixing and re-enabling it

---------

Co-authored-by: Tiago Costa <tiago.costa@elastic.co>
Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
2024-08-23 11:54:36 -05:00

74 lines
2.4 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { UsageStatsPayload } from '@kbn/telemetry-collection-manager-plugin/server';
import {
ELASTIC_HTTP_VERSION_HEADER,
X_ELASTIC_INTERNAL_ORIGIN_REQUEST,
} from '@kbn/core-http-common';
import type { FtrProviderContext } from '../ftr_provider_context';
export interface UsageStatsPayloadTestFriendly extends UsageStatsPayload {
// Overwriting the `object` type to a more test-friendly type
stack_stats: Record<string, any>;
}
export interface GetTelemetryStatsOpts {
authHeader: Record<string, string>;
}
export function UsageAPIProvider({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const supertestWithoutAuth = getService('supertestWithoutAuth');
async function getTelemetryStats(
payload: {
unencrypted: true;
refreshCache?: boolean;
},
opts?: GetTelemetryStatsOpts
): Promise<Array<{ clusterUuid: string; stats: UsageStatsPayloadTestFriendly }>>;
async function getTelemetryStats(
payload: {
unencrypted: false;
refreshCache?: boolean;
},
opts?: GetTelemetryStatsOpts
): Promise<Array<{ clusterUuid: string; stats: string }>>;
async function getTelemetryStats(
payload: {
unencrypted?: boolean;
refreshCache?: boolean;
},
opts?: GetTelemetryStatsOpts
): Promise<Array<{ clusterUuid: string; stats: UsageStatsPayloadTestFriendly | string }>> {
const client = opts?.authHeader ? supertestWithoutAuth : supertest;
const request = client
.post('/internal/telemetry/clusters/_stats')
.set('kbn-xsrf', 'xxx')
.set(ELASTIC_HTTP_VERSION_HEADER, '2')
.set(X_ELASTIC_INTERNAL_ORIGIN_REQUEST, 'kibana');
if (opts?.authHeader) {
void request.set(opts.authHeader);
}
const { body } = await request.send({ refreshCache: true, ...payload }).expect(200);
return body;
}
return {
/**
* Retrieve the stats via the private telemetry API:
* It returns the usage in as a string encrypted blob or the plain payload if `unencrypted: false`
*
* @param payload Request parameters to retrieve the telemetry stats
*/
getTelemetryStats,
};
}