thlorenz / proxyquire

🔮 Proxies nodejs require in order to allow overriding dependencies during testing.
MIT License
2.75k stars 100 forks source link

global require not working #253

Closed jcald1 closed 4 years ago

jcald1 commented 4 years ago

I'm using version 2.1.3.

I have the following code in my test:

const proxyquire = require('proxyquire');
const awkMock = {
    Lambda: function Lambda() {
        this.invoke = () => ({
            promise: () => Promise.resolve({})
        })
    },
    '@global': true
}
const lambdaMocked = proxyquire('../../handler.js', { 'aws-sdk': awkMock});

lambdaMocked.handler(..)

../..handler.js requires a library with this statement: const AWS = require('aws-sdk')

In the library code in node_modules, when I add console.log(AWS.Lambda), I get:

function (params, callback) {
          return this.makeRequest(method, params, callback);
        }

which is the original function, not the mock.

bendrucker commented 4 years ago

Need to see a full code example here, including how you're requiring the SDK. It'll be easiest to help if you give me something I can clone and then npm install && npm test and get an error.

jcald1 commented 4 years ago

@bendrucker , it worked for me in the small repro, so there must be some other issue going on with my code. Closing the ticket.