diff --git a/sdk/eventhub/event-hubs/test/utils/logging.ts b/sdk/eventhub/event-hubs/test/utils/logging.ts index 2b2714b417b5..1ed389f9e1e6 100644 --- a/sdk/eventhub/event-hubs/test/utils/logging.ts +++ b/sdk/eventhub/event-hubs/test/utils/logging.ts @@ -2,10 +2,10 @@ // Licensed under the MIT License. import { afterAll, beforeAll, inject } from "vitest"; -import type { AzureLogLevel } from "@azure/logger"; import { setLogLevel } from "@azure/logger"; +import { EnvVarKeys } from "./constants.js"; -const logLevel = inject("AZURE_LOG_LEVEL") as AzureLogLevel; +const logLevel = inject(EnvVarKeys.AZURE_LOG_LEVEL); const localStorage: { debug?: string } = {}; beforeAll(async function () { diff --git a/sdk/eventhub/event-hubs/test/utils/setup.ts b/sdk/eventhub/event-hubs/test/utils/setup.ts index 194dc058be5d..80b64d1c0e2b 100644 --- a/sdk/eventhub/event-hubs/test/utils/setup.ts +++ b/sdk/eventhub/event-hubs/test/utils/setup.ts @@ -10,17 +10,20 @@ import { resolve as resolvePath } from "path"; import type { GlobalSetupContext } from "vitest/node"; import { EnvVarKeys } from "./constants.js"; import * as MOCKS from "./constants.js"; +import type { AzureLogLevel } from "@azure/logger"; declare module "vitest" { type MyEnvVarKeys = { - [K in keyof typeof EnvVarKeys]: string; - }; + [K in keyof Omit]: string; + } & { AZURE_LOG_LEVEL: AzureLogLevel | undefined }; export interface ProvidedContext extends MyEnvVarKeys {} } -const defaultLogLevel = "error"; - -function assertEnvironmentVariable(key: string): string { +function assertEnvironmentVariable( + key: K, +): AzureLogLevel | undefined; +function assertEnvironmentVariable(key: string): string; +function assertEnvironmentVariable(key: string): string | undefined { const value = process.env[key]; // handle defaults if (!value) { @@ -28,7 +31,7 @@ function assertEnvironmentVariable(key: string): string { case EnvVarKeys.TEST_MODE: return "mock"; case EnvVarKeys.AZURE_LOG_LEVEL: - return defaultLogLevel; + return undefined; default: throw new Error(`Environment variable ${key} is not defined.`); } @@ -51,6 +54,14 @@ function createMockServer(options: MockServerOptions = {}): MockEventHub { }); } +function getAzureLogLevel(): AzureLogLevel | undefined { + const val = process.env[EnvVarKeys.AZURE_LOG_LEVEL]; + if (![undefined, "error", "warning", "info", "verbose"].includes(val)) { + throw new Error(`Invalid value for ${EnvVarKeys.AZURE_LOG_LEVEL}: ${val}`); + } + return val as AzureLogLevel | undefined; +} + export default async function ({ provide }: GlobalSetupContext) { if (process.env[EnvVarKeys.TEST_MODE]?.toLowerCase() === "live") { const kvUri = assertEnvironmentVariable("KEYVAULT_URI"); @@ -76,7 +87,7 @@ export default async function ({ provide }: GlobalSetupContext) { provide(EnvVarKeys.EVENTHUB_NAME, MOCKS.EVENTHUB_NAME); provide(EnvVarKeys.EVENTHUB_CONSUMER_GROUP_NAME, MOCKS.EVENTHUB_CONSUMER_GROUP_NAME); provide(EnvVarKeys.EVENTHUB_FQDN, MOCKS.EVENTHUB_FQDN); - provide(EnvVarKeys.AZURE_LOG_LEVEL, process.env[EnvVarKeys.AZURE_LOG_LEVEL] || defaultLogLevel); + provide(EnvVarKeys.AZURE_LOG_LEVEL, getAzureLogLevel()); provide(EnvVarKeys.EVENTHUB_CONNECTION_STRING, MOCKS.EVENTHUB_CONNECTION_STRING_WITH_KEY); const server = createMockServer(); await server.start();