11import { CredentialsProviderError } from "@aws-sdk/property-provider" ;
2- import { loadSharedConfigFiles } from "@aws-sdk/shared-ini-file-loader" ;
2+ import { getProfileName , loadSharedConfigFiles } from "@aws-sdk/shared-ini-file-loader" ;
33import { ParsedIniData , Profile } from "@aws-sdk/types" ;
44
5- import { ENV_PROFILE , fromSharedConfigFiles , GetterFromConfig , SharedConfigInit } from "./fromSharedConfigFiles" ;
5+ import { fromSharedConfigFiles , GetterFromConfig , SharedConfigInit } from "./fromSharedConfigFiles" ;
66
77jest . mock ( "@aws-sdk/shared-ini-file-loader" , ( ) => ( {
8+ getProfileName : jest . fn ( ) ,
89 loadSharedConfigFiles : jest . fn ( ) ,
910} ) ) ;
1011
1112describe ( "fromSharedConfigFiles" , ( ) => {
12- const envProfile = process . env [ ENV_PROFILE ] ;
1313 const configKey = "config_key" ;
1414 const configGetter : GetterFromConfig < string > = ( profile : Profile ) => profile [ configKey ] ;
1515
16- beforeEach ( ( ) => {
17- delete process . env [ ENV_PROFILE ] ;
18- } ) ;
19-
20- afterAll ( ( ) => {
21- process . env [ ENV_PROFILE ] = envProfile ;
22- } ) ;
23-
2416 const getCredentialsProviderError = ( profile : string , getter : GetterFromConfig < string > ) =>
2517 new CredentialsProviderError (
2618 `Cannot load config for profile ${ profile } in SDK configuration files with getter: ${ getter } `
@@ -122,6 +114,7 @@ describe("fromSharedConfigFiles", () => {
122114 configFile : iniDataInConfig ,
123115 credentialsFile : iniDataInCredentials ,
124116 } ) ;
117+ ( getProfileName as jest . Mock ) . mockReturnValueOnce ( profile ?? "default" ) ;
125118 return expect ( fromSharedConfigFiles ( configGetter , { profile, preferredFile } ) ( ) ) . resolves . toBe (
126119 configValueToVerify
127120 ) ;
@@ -135,6 +128,7 @@ describe("fromSharedConfigFiles", () => {
135128 configFile : iniDataInConfig ,
136129 credentialsFile : iniDataInCredentials ,
137130 } ) ;
131+ ( getProfileName as jest . Mock ) . mockReturnValueOnce ( profile ?? "default" ) ;
138132 return expect ( fromSharedConfigFiles ( configGetter , { profile, preferredFile } ) ( ) ) . rejects . toMatchObject (
139133 getCredentialsProviderError ( profile ?? "default" , configGetter )
140134 ) ;
@@ -165,24 +159,15 @@ describe("fromSharedConfigFiles", () => {
165159 } ,
166160 } ;
167161
168- describe ( "when profile is not defined" , ( ) => {
169- beforeEach ( ( ) => {
170- ( loadSharedConfigFiles as jest . Mock ) . mockResolvedValueOnce ( loadedConfigData ) ;
171- } ) ;
172-
173- it ( `returns configValue from value in '${ ENV_PROFILE } ' env var if it is set` , ( ) => {
174- const profile = "foo" ;
175- process . env [ ENV_PROFILE ] = profile ;
176- return expect ( fromSharedConfigFiles ( configGetter ) ( ) ) . resolves . toBe (
177- loadedConfigData . configFile [ profile ] [ configKey ]
178- ) ;
179- } ) ;
162+ beforeEach ( ( ) => {
163+ ( loadSharedConfigFiles as jest . Mock ) . mockResolvedValueOnce ( loadedConfigData ) ;
164+ } ) ;
180165
181- it ( `returns configValue from default profile if ' ${ ENV_PROFILE } ' env var is not set` , ( ) => {
182- return expect ( fromSharedConfigFiles ( configGetter ) ( ) ) . resolves . toBe (
183- loadedConfigData . configFile . default [ configKey ]
184- ) ;
185- } ) ;
166+ it . each ( [ "foo" , " default" ] ) ( "returns config value from %s profile" , ( profile ) => {
167+ ( getProfileName as jest . Mock ) . mockReturnValueOnce ( profile ) ;
168+ return expect ( fromSharedConfigFiles ( configGetter ) ( ) ) . resolves . toBe (
169+ loadedConfigData . configFile [ profile ] [ configKey ]
170+ ) ;
186171 } ) ;
187172 } ) ;
188173} ) ;
0 commit comments