-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(region-provider): add extensive unit tests for region-provider (#…
- Loading branch information
Showing
6 changed files
with
360 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,72 @@ | ||
import { defaultProvider } from "./defaultProvider"; | ||
import { ProviderError } from "@aws-sdk/property-provider"; | ||
import { fromEnv } from "./fromEnv"; | ||
import { fromSharedConfigFiles } from "./fromSharedConfigFiles"; | ||
import { chain, memoize } from "@aws-sdk/property-provider"; | ||
import { | ||
defaultProvider, | ||
RegionProviderConfiguration | ||
} from "./defaultProvider"; | ||
|
||
jest.mock("./fromEnv", () => { | ||
const envProvider = jest.fn(); | ||
return { | ||
fromEnv: jest.fn().mockReturnValue(envProvider) | ||
}; | ||
}); | ||
import { fromEnv, EnvConfiguration } from "./fromEnv"; | ||
jest.mock("./fromEnv", () => ({ | ||
fromEnv: jest.fn() | ||
})); | ||
|
||
jest.mock("./fromSharedConfigFiles", () => { | ||
const iniProvider = jest.fn(); | ||
return { | ||
fromSharedConfigFiles: jest.fn().mockReturnValue(iniProvider) | ||
}; | ||
}); | ||
import { | ||
fromSharedConfigFiles, | ||
SharedConfigInit | ||
} from "./fromSharedConfigFiles"; | ||
jest.mock("./fromSharedConfigFiles", () => ({ | ||
fromSharedConfigFiles: jest.fn() | ||
})); | ||
|
||
beforeEach(() => { | ||
(fromEnv() as any).mockClear(); | ||
(fromSharedConfigFiles() as any).mockClear(); | ||
(fromEnv as any).mockClear(); | ||
(fromSharedConfigFiles as any).mockClear(); | ||
}); | ||
jest.mock("@aws-sdk/property-provider", () => ({ | ||
chain: jest.fn(), | ||
memoize: jest.fn() | ||
})); | ||
|
||
describe("defaultProvider", () => { | ||
it("should stop after the environmental provider if a region has been found", async () => { | ||
const region = "foo"; | ||
(fromEnv() as any).mockImplementation(() => Promise.resolve(region)); | ||
const configuration: RegionProviderConfiguration = { | ||
profile: "profile", | ||
environmentVariableName: "environmentVariableName" | ||
}; | ||
|
||
expect(await defaultProvider()()).toEqual(region); | ||
expect((fromEnv() as any).mock.calls.length).toBe(1); | ||
expect((fromSharedConfigFiles() as any).mock.calls.length).toBe(0); | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should continue on to the ini provider if no environment variable region has been found", async () => { | ||
const region = "foo"; | ||
it("passes fromEnv() and fromSharedConfigFiles() to chain", () => { | ||
const mockFromEnvReturn = "mockFromEnvReturn"; | ||
(fromEnv as jest.Mock).mockReturnValueOnce(mockFromEnvReturn); | ||
|
||
(fromEnv() as any).mockImplementation(() => | ||
Promise.reject(new ProviderError("Nothing here!")) | ||
const mockFromSharedConfigFilesReturn = "mockFromSharedConfigFilesReturn"; | ||
(fromSharedConfigFiles as jest.Mock).mockReturnValueOnce( | ||
mockFromSharedConfigFilesReturn | ||
); | ||
(fromSharedConfigFiles() as any).mockImplementation(() => | ||
Promise.resolve(region) | ||
); | ||
|
||
expect(await defaultProvider()()).toEqual(region); | ||
expect((fromEnv() as any).mock.calls.length).toBe(1); | ||
expect((fromSharedConfigFiles() as any).mock.calls.length).toBe(1); | ||
}); | ||
|
||
it("should pass configuration on to the env provider", async () => { | ||
const envConfig: EnvConfiguration = { | ||
environmentVariableName: "foo" | ||
}; | ||
|
||
(fromEnv() as any).mockImplementation(() => Promise.resolve("region")); | ||
(fromEnv as any).mockClear(); | ||
|
||
await expect(defaultProvider(envConfig)()).resolves; | ||
|
||
expect((fromEnv as any).mock.calls.length).toBe(1); | ||
expect((fromEnv as any).mock.calls[0][0]).toBe(envConfig); | ||
}); | ||
defaultProvider(configuration); | ||
|
||
it("should pass configuration on to the ini provider", async () => { | ||
const iniConfig: SharedConfigInit = { | ||
profile: "foo", | ||
filepath: "/home/user/.secrets/credentials.ini", | ||
configFilepath: "/home/user/.secrets/credentials.ini" | ||
}; | ||
expect(fromEnv).toHaveBeenCalledTimes(1); | ||
expect(fromEnv).toHaveBeenCalledWith(configuration); | ||
expect(fromSharedConfigFiles).toHaveBeenCalledTimes(1); | ||
expect(fromSharedConfigFiles).toHaveBeenCalledWith(configuration); | ||
|
||
(fromEnv() as any).mockImplementation(() => | ||
Promise.reject(new ProviderError("Keep moving!")) | ||
expect(chain).toHaveBeenCalledTimes(1); | ||
expect(chain).toHaveBeenCalledWith( | ||
mockFromEnvReturn, | ||
mockFromSharedConfigFilesReturn | ||
); | ||
(fromSharedConfigFiles() as any).mockImplementation(() => | ||
Promise.resolve("region") | ||
); | ||
|
||
(fromSharedConfigFiles as any).mockClear(); | ||
|
||
await expect(defaultProvider(iniConfig)()).resolves; | ||
|
||
expect((fromSharedConfigFiles as any).mock.calls.length).toBe(1); | ||
expect((fromSharedConfigFiles as any).mock.calls[0][0]).toBe(iniConfig); | ||
}); | ||
|
||
it("should return the same promise across invocations", async () => { | ||
const region = "foo"; | ||
(fromEnv() as any).mockImplementation(() => Promise.resolve(region)); | ||
it("passes output of chain to memoize", () => { | ||
const mockChainReturn = "mockChainReturn"; | ||
(chain as jest.Mock).mockReturnValueOnce(mockChainReturn); | ||
|
||
const provider = defaultProvider(); | ||
defaultProvider(configuration); | ||
|
||
expect(await provider()).toEqual(region); | ||
expect(chain).toHaveBeenCalledTimes(1); | ||
expect(memoize).toHaveBeenCalledTimes(1); | ||
expect(memoize).toHaveBeenCalledWith(mockChainReturn); | ||
}); | ||
|
||
expect(provider()).toBe(provider()); | ||
it("returns output memoize", () => { | ||
const mockMemoizeReturn = "mockMemoizeReturn"; | ||
(memoize as jest.Mock).mockReturnValueOnce(mockMemoizeReturn); | ||
|
||
expect(await provider()).toEqual(region); | ||
expect((fromEnv() as any).mock.calls.length).toBe(1); | ||
expect(defaultProvider(configuration)).toEqual(mockMemoizeReturn); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.