From 9ca8baeb39549b64388f415909a157c8ff1bef77 Mon Sep 17 00:00:00 2001 From: George Fu Date: Tue, 16 Jul 2024 17:46:16 -0400 Subject: [PATCH] test(credential-provider-node): fix tests related to defaultProvider chain (#6285) --- .../src/defaultProvider.spec.ts | 15 +++++++++++---- .../src/defaultProvider.ts | 8 ++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/credential-provider-node/src/defaultProvider.spec.ts b/packages/credential-provider-node/src/defaultProvider.spec.ts index 03924600cab8..ceaff2aa4def 100644 --- a/packages/credential-provider-node/src/defaultProvider.spec.ts +++ b/packages/credential-provider-node/src/defaultProvider.spec.ts @@ -49,7 +49,17 @@ describe(defaultProvider.name, () => { const mockTokenFileFn = jest.fn().mockImplementation(() => credentials()); const mockRemoteProviderFn = jest.fn().mockImplementation(() => finalCredentials()); + const ORIGINAL_ENV = { + ...process.env, + }; + beforeEach(() => { + process.env = { + ...ORIGINAL_ENV, + }; + delete process.env.AWS_PROFILE; + delete process.env.AWS_ACCESS_KEY_ID; + delete process.env.AWS_SECRET_ACCESS_KEY; [ [fromEnv, mockEnvFn], [fromSSO, mockSsoFn], @@ -64,6 +74,7 @@ describe(defaultProvider.name, () => { afterEach(async () => { jest.clearAllMocks(); + process.env = ORIGINAL_ENV; }); describe("without fromEnv", () => { @@ -102,9 +113,7 @@ describe(defaultProvider.name, () => { }); it(`if env['${ENV_PROFILE}'] is set`, async () => { - const ORIGINAL_ENV = process.env; process.env = { - ...ORIGINAL_ENV, [ENV_PROFILE]: "envProfile", }; @@ -117,8 +126,6 @@ describe(defaultProvider.name, () => { expect(fromFn).toHaveBeenCalledWith(mockInitWithoutProfile); } expect(fromSSO).not.toHaveBeenCalled(); - - process.env = ORIGINAL_ENV; }); }); diff --git a/packages/credential-provider-node/src/defaultProvider.ts b/packages/credential-provider-node/src/defaultProvider.ts index f7213f7ec8d7..b16112434ec6 100644 --- a/packages/credential-provider-node/src/defaultProvider.ts +++ b/packages/credential-provider-node/src/defaultProvider.ts @@ -84,11 +84,11 @@ export const defaultProvider = (init: DefaultProviderInit = {}): MemoizedProvide ); multipleCredentialSourceWarningEmitted = true; } - throw new CredentialsProviderError("AWS_PROFILE is set, skipping fromEnv provider.", { - logger: init.logger, - tryNextLink: true, - }); } + throw new CredentialsProviderError("AWS_PROFILE is set, skipping fromEnv provider.", { + logger: init.logger, + tryNextLink: true, + }); } init.logger?.debug("@aws-sdk/credential-provider-node - defaultProvider::fromEnv"); return fromEnv(init)();