Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock doesn't work inside of a class #231

Open
TooManyLogs opened this issue Mar 8, 2021 · 1 comment
Open

Mock doesn't work inside of a class #231

TooManyLogs opened this issue Mar 8, 2021 · 1 comment

Comments

@TooManyLogs
Copy link

Hi, having some trouble getting mock to work inside a class.

In the following code the mock is not being called at all when testing class

describe(`Mock test`, () => {

    it(`should mock call out from lambda`, done => {

        AWS.mock('Lambda', 'invoke', (params, callback) => callback(null, {
            Payload: {}
        }));

        executeLambda(lambda, event, (callbackErr, callbackDone) => {
            done();
        });
    });

});

This is an example of the class based approach i'm trying to test

module.exports = class MyIntent {

async handle () {
    await this.invoke('myParam');
}

invoke (paramName = {
}) {

    const params = {
        paramName
    };

    return Client.invoke(params)
        .promise()
        .then(response =>
            JSON.parse(response.Payload)
        );
}

};

The same code without class will run the mock when testing

exports.handle = event => {

async function handle () {
    await invoke('myParam');
}

function invoke (paramName = {
}) {

    const params = {
        paramName
    };

    return Client.invoke(params)
        .promise()
        .then(response =>
            JSON.parse(response.Payload)
        );
}

};
@thomaux
Copy link
Collaborator

thomaux commented Nov 25, 2021

@TooManyLogs I believe the issue here is that your class is not initialised and hence not picked up as a handler.

Change the export statement to:

class MyIntent {
    async handle () {
        await this.invoke('myParam');
    }

    invoke (paramName = {}) {
        const params = {
            paramName
        };

        return Client.invoke(params)
            .promise()
            .then(response =>
                JSON.parse(response.Payload)
            );
        };
    }
}

module.exports = new MyIntent();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants