Giter Club home page Giter Club logo

Comments (6)

m-radzikowski avatar m-radzikowski commented on July 28, 2024 8

I run the test with both v3.28.0 and v3.34.0 of @aws-sdk/client-secrets-manager and it looks to work fine.

The code you posted has a slight mistake - you either pass the client class to the mockClient() function, or the same instance that you want to mock.

This works good for me:

import {mockClient} from 'aws-sdk-client-mock';
import {GetSecretValueCommand, SecretsManagerClient} from '@aws-sdk/client-secrets-manager';

it('mocks SecretsManagerClient', async () => {
    const smMock = mockClient(SecretsManagerClient);
    smMock.on(GetSecretValueCommand)
        .resolves({
            SecretString: JSON.stringify({my_secret_key: 'my_secret_value'}),
        });

    const sm = new SecretsManagerClient({});
    const response = await sm.send(new GetSecretValueCommand({
        SecretId: 'qq',
    }));

    expect(response.SecretString).toBe('{"my_secret_key":"my_secret_value"}');
});

Alternatively, if you want to mock only a single instance of the Client, you need to provide the same instance in the mock as you use later in code:

import {mockClient} from 'aws-sdk-client-mock';
import {GetSecretValueCommand, SecretsManagerClient} from '@aws-sdk/client-secrets-manager';

it('mocks SecretsManagerClient', async () => {
    const sm = new SecretsManagerClient({});

    const smMock = mockClient(sm);
    smMock.on(GetSecretValueCommand)
        .resolves({
            SecretString: JSON.stringify({my_secret_key: 'my_secret_value'}),
        });

    const response = await sm.send(new GetSecretValueCommand({
        SecretId: 'qq',
    }));

    expect(response.SecretString).toBe('{"my_secret_key":"my_secret_value"}');
});

Please let me know if that solves your problem.

from aws-sdk-client-mock.

m-radzikowski avatar m-radzikowski commented on July 28, 2024 1

Hey, I was vacationing so sorry for the delay, but I will look into this in the next days.

from aws-sdk-client-mock.

FizzBuzz791 avatar FizzBuzz791 commented on July 28, 2024

Just bumping this to see if there's any ideas/updates?

from aws-sdk-client-mock.

Brawrdon avatar Brawrdon commented on July 28, 2024

Just wanted to reopen this because I got the same error (ExpiredTokenException: The security token included in the request is expired).

It seems you need to have valid credentials / token to access your AWS account from the CLI for it to work. I don't expect you'd need these set to do a mocked unit test 😅

from aws-sdk-client-mock.

m-radzikowski avatar m-radzikowski commented on July 28, 2024

@Brawrdon can you create a sample repository with problem reproduction? As noted above, I did mock Secrets Manager successfully.

from aws-sdk-client-mock.

Brawrdon avatar Brawrdon commented on July 28, 2024

Okay so very strange... It seems to work fine now even though my tokens expired so now I'm finding it hard to recreate the error.

from aws-sdk-client-mock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.