vandium-io / lambda-tester

Helper for unit testing AWS Lambda functions
BSD 3-Clause "New" or "Revised" License
272 stars 51 forks source link

Encountering issue while writing my first test case. #47

Closed kahmad07 closed 5 years ago

kahmad07 commented 5 years ago

First of all thanks for writing this simple but really effective tool. I am a novice to javascript testing through Jasmine/Karma/Chai/Mocha etc. I am trying to test one of our very simple lambdas, however, encountering an issue while running the test.

Lambda: `exports.handler = (event, context, callback) => { const request = event.Records[0].cf.request;

var host = request.headers.host; if (!host) { host = request.headers.Host; }

if (request.uri == '/apps/secure/myapps/') { const response = { status: '302', statusDescription: 'Found', httpVersion: request.httpVersion, headers: { Location: ['/apps/myapps/'], }, }; callback(null, response); } else { callback(null, request); } };`

Test: `const expect = require( 'chai' ).expect; const LambdaTester = require( 'lambda-tester' ); const proxyquire = require( 'proxyquire' ).noCallThru(); const sinon = require( 'sinon' ); const response = { status: '302', statusDescription: 'Found', httpVersion: 'v1', header: { Location: ['/apps/myapps/'], }, }
describe( 'index', function() {

let lambda;

let AWSStub;

let snsStub;

beforeEach(function(){

    snsStub = {

        publish: sinon.stub()
    };

    AWSStub = {

        SNS: sinon.stub().returns( snsStub )
    };

    lambda = proxyquire( '../index',{

        'aws-sdk': AWSStub
    });
});

describe( '.handler', function(){

    let expectedUri = '/apps/secure/myapps/';

    it('success', function(){

        return LambdaTester(lambda.handler)
            .event( { uri : expectedUri })
            .expctedResult((result) => {

                expect(result).to.be.true;

            });
    });

});

});`

When I try to run the test through mocha it gives me below error ` index .handler 1) success

0 passing (13ms) 1 failing

1) index .handler success: TypeError: LambdaTester(...).event(...).expctedResult is not a function at Context. (test/index.test.js:79:18)`

Any help to resolve this issue and help to improve my test will be highly appreciated.

kahmad07 commented 5 years ago

Somehow am not able to paste my code properly. Apologies for that.

richardhyatt commented 5 years ago

Looks like a typo in your code - try .expectResult( .... )

On Nov 8, 2018, at 16:05, kahmad07 notifications@github.com<mailto:notifications@github.com> wrote:

Somehow am not able to paste my code properly. Apologies for that.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/vandium-io/lambda-tester/issues/47#issuecomment-437155575, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AKVWX5mMNiYHhccRJM0YnZosmHiYWAqWks5utJyVgaJpZM4YVeGJ.

kahmad07 commented 5 years ago

Thanks, Richard for the prompt reply and identifying the issue. That helped me move forward, however, got stuck at another place now. The test after the execution started reporting an error about the version of node.js, which when I have updated broke the mocha, following is the error am encountering:

` handler 1) success

0 passing (10ms) 1 failing

1) handler success: Error: Please test with node.js versions: 8.10.0 - 8.999.0 at doVersionCheck (node_modules/lambda-tester/lib/index.js:30:15) at Promise (node_modules/lambda-tester/lib/index.js:40:13) at new Promise () at resolveHandler (node_modules/lambda-tester/lib/index.js:36:12) at LambdaTester.expectResult (node_modules/lambda-tester/lib/index.js:236:23) at Context. (test/index.test.js:28:18)

LAMU02RR4UWG8WP:myapps-redirect kahmad1$ nvm install v10.13.0 v10.13.0 is already installed. Now using node v10.13.0 (npm v6.4.1) LAMU02RR4UWG8WP:myapps-redirect kahmad1$ nvm install v10.13.0 v10.13.0 is already installed. Now using node v10.13.0 (npm v6.4.1) LAMU02RR4UWG8WP:myapps-redirect kahmad1$ mocha test -bash: mocha: command not found`

My Test after clean up:

`const expect = require( 'chai' ).expect;

const LambdaTester = require( 'lambda-tester' );

const myLambda = require( '../index' );

const response = { status: '302', statusDescription: 'Found', httpVersion: 'v1', header: { Location: ['/apps/myapps/'], }, }

describe( 'handler', function(){

let expectedUri = '/apps/secure/myapps/';

    it('success', function(){

        return LambdaTester(myLambda.handler)
            .event( { uri : expectedUri })
            .expectResult((result) => {

                expect(result.response).to.be.true;

            });
    });

});`

kahmad07 commented 5 years ago

Managed to resolve by reinstalling mocha with -g option, however, stuck at a different error now. Which I believe is the problem in the test and not in your framework but if you can provide some input that will be really great. Thanks in advance.

Lambda: `exports.handler = (event, context, callback) => { const request = event.Records[0].cf.request;

var host = request.headers.host; if (!host) { host = request.headers.Host; }

if (request.uri == '/apps/secure/myapps/') { const response = { status: '302', statusDescription: 'Found', httpVersion: request.httpVersion, headers: { Location: ['/apps/myapps/'], }, }; callback(null, response); } else { callback(null, request); } };`

Test:

`const expect = require( 'chai' ).expect;

const LambdaTester = require( 'lambda-tester' );

const myLambda = require( '../index' );

const response = { status: '302', statusDescription: 'Found', httpVersion: 'v1', header: { Location: ['/apps/myapps/'], }, }

describe( 'handler', function(){

let expectedUri = '/apps/secure/myapps/';

    it('success', function(){

        return LambdaTester(myLambda.handler)
            .event( { uri : expectedUri })
            .expectResult((result) => {

                expect(result.response).to.be.true;

            });
    });

});`

richardhyatt commented 5 years ago

Your expect statement is not right - the result object is the response object.

On Nov 8, 2018, at 17:22, kahmad07 notifications@github.com<mailto:notifications@github.com> wrote:

Managed to resolve by reinstalling mocha with -g option, however, stuck at a different error now. Which I believe is the problem in the test and not in your framework but if you can provide some input that will be really great. Thanks in advance.

Lambda: `exports.handler = (event, context, callback) => { const request = event.Records[0].cf.request;

var host = request.headers.host; if (!host) { host = request.headers.Host; }

if (request.uri == '/apps/secure/myapps/') { const response = { status: '302', statusDescription: 'Found', httpVersion: request.httpVersion, headers: { Location: ['/apps/myapps/'], }, }; callback(null, response); } else { callback(null, request); } };`

Test:

`const expect = require( 'chai' ).expect;

const LambdaTester = require( 'lambda-tester' );

const myLambda = require( '../index' );

const response = { status: '302', statusDescription: 'Found', httpVersion: 'v1', header: { Location: ['/apps/myapps/'], }, }

describe( 'handler', function(){

let expectedUri = '/apps/secure/myapps/';

it('success', function(){

    return LambdaTester(myLambda.handler)
        .event( { uri : expectedUri })
        .expectResult((result) => {

            expect(result.response).to.be.true;

        });
});

});`

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/vandium-io/lambda-tester/issues/47#issuecomment-437177597, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AKVWX7P-Ihxf-64ffA5MvAEXM3yM9Yk7ks5utK6fgaJpZM4YVeGJ.

kahmad07 commented 5 years ago

Thanks again Richard for your prompt reply and help. I am still encountering the same error even if use the result object in my expect statement, I know am making some silly mistake here but I guess that's how we all learn. If you can provide some help to resolve this error that will be really great. Also, you mark this issue as close.

`const expect = require( 'chai' ).expect;

const LambdaTester = require( 'lambda-tester' );

const myLambda = require( '../index' );

const response = { status: '302', statusDescription: 'Found', httpVersion: 'v1', header: { Location: ['/apps/myapps/'], }, }

describe( 'handler', function(){

let expectedUri = '/apps/secure/myapps/';

    it('success', function(){

        return LambdaTester(myLambda.handler)
            .event( { uri : expectedUri })
            .expectResult((result) => {

                expect(result).to.be.true;

            });
    });

});`

`apps-redirect kahmad1$ mocha test

handler 1) success

0 passing (13ms) 1 failing

1) handler success: TypeError: Cannot read property '0' of undefined at exports.handler (index.js:4:32) at Promise (node_modules/lambda-tester/lib/runner.js:245:37) at new Promise () at Promise.resolve.then.then (node_modules/lambda-tester/lib/runner.js:232:24) at `