Search code examples
node.jsmocha.jssinonproxyquiremock-fs

How to Mock non-existent file import in nodejs Lambda


I am facing issue in testing where My logger is in lambda layer thus non-existing for nodeJs import in lambda.js during mocha-chai testing. I tried mock-fs but getting errors Can not find module /opt/logger.js or maybe I am trying wrong way and not sure if it is useful in this way. Please check below code for reference. Any help or suggestion is most welcome.

lambda.js -

const logger = require('/opt/logger.js') // coming from lambda_layer.js

lambda.test.js -

mock({
    "/opt/logger.js": console.log('hello')
});

Solution

  • Was able to accomplish this with mockery. Thanks for allowing me to put this here for everyone. Will update this with better code soon.

    mockery.registerMock({'/test/': { destroyAllHumans: actionStub })
    mockery.enabled();