You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have read Caveats documentation and didn't find a solution for this problem there.
Bug description
Using ts-jest with experimental ESM support enabled, I am struggling to mock the SecretsManager. I have been able to get other AWS services to mock properly in this same ecosystem, so I'm not sure what about SecretsManager is different.
It detects that the command was called but doesn't get the resolved value it should have.
When I try to use this in a real test, where the credentials are being pulled in a separate class that I'm testing, the test fails and I see the error "CredentialsProviderError: Could not load credentials from any providers":
(see example 2 below)
When you inspect the mock in this scenario, you'll see that the mock never registered any command being run (ie, secretsManagerMock.commandCalls(GetSecretValueCommand).length == 0).
I have also tried moving the mock creation code to a jest setup file as was suggested in this previous issue. However, that didn't change this behavior at all. The mock was not used unless it was declared in the same file with the actual invocation.
Reproduction
// Example 1:
import { mockClient } from "aws-sdk-client-mock";
import {
SecretsManager,
GetSecretValueCommand,
} from "@aws-sdk/client-secrets-manager";
const secretsManagerMock = mockClient(SecretsManager);
secretsManagerMock.on(GetSecretValueCommand).resolves({
SecretString: "test_secret",
});
it("Pulls credentials from the secrets manager", async () => {
const client = new SecretsManager();
const input = { SecretId: "test_api_key" };
const result = await client.send(new GetSecretValueCommand(input));
expect(secretsManagerMock.commandCalls(GetSecretValueCommand)).toHaveLength(
1,
);
expect(result).toBe("test_secret");
});
// Example 2:
module.test.ts
import { mockClient } from "aws-sdk-client-mock";
import {
SecretsManager,
GetSecretValueCommand,
} from "@aws-sdk/client-secrets-manager";
const secretsManagerMock = mockClient(SecretsManager);
secretsManagerMock.on(GetSecretValueCommand).resolves({
SecretString: "test_secret",
});
// Import class after mock is created using dynamic top level awaited import
const TestModule = (await import("module.ts")).default;
it("Pulls credentials from the secrets manager", async () => {
const mod = new TestModule();
const result = await mod.run();
expect(result).toBe("test_secret");
});
module.ts
import {
SecretsManager,
GetSecretValueCommand,
} from "@aws-sdk/client-secrets-manager";
export default class TestModule {
async run() {
const client = new SecretsManager();
const input = { SecretId: "test_api_key" };
const result = await client.send(new GetSecretValueCommand(input));
return result.SecretString;
}
}
Hey, can you provide a small repo with a reproduction? With Jest setup for ESM and exact commands to run to get the error (npm install and npm test in the simplest scenario).
Checklist
Bug description
Using ts-jest with experimental ESM support enabled, I am struggling to mock the SecretsManager. I have been able to get other AWS services to mock properly in this same ecosystem, so I'm not sure what about SecretsManager is different.
(see Example 1 below)
This simple, all in one file test fails:
It detects that the command was called but doesn't get the resolved value it should have.
When I try to use this in a real test, where the credentials are being pulled in a separate class that I'm testing, the test fails and I see the error "CredentialsProviderError: Could not load credentials from any providers":
(see example 2 below)
When you inspect the mock in this scenario, you'll see that the mock never registered any command being run (ie, secretsManagerMock.commandCalls(GetSecretValueCommand).length == 0).
I have also tried moving the mock creation code to a jest setup file as was suggested in this previous issue. However, that didn't change this behavior at all. The mock was not used unless it was declared in the same file with the actual invocation.
Reproduction
// Example 1:
// Example 2:
module.test.ts
module.ts
Environment
Typescript version: 5.3.3
AWS SDK v3 Client mock version:
AWS JS SDK libs and versions:
The text was updated successfully, but these errors were encountered: