plasticine / inject-loader

💉📦 A Webpack loader for injecting code into modules via their dependencies.
https://www.npmjs.com/package/inject-loader
MIT License
482 stars 47 forks source link

Mocking out the 'request-promise' library #56

Closed jml6m closed 6 years ago

jml6m commented 6 years ago

I have npm library request-promise and am importing as so at the top of a NodeJS file:

import * as original_request from 'request-promise';

Exported function that uses this as seen here:

return original_request({
        uri: myUri,
        method: 'POST',
        form: {
            userName: username,
            password: password
        }
    }).then(resp => {
        // Do more logic
    });

In test file I am mocking out like this:

injector({
     'request-promise': () => [{id: '12345'}]
})

Getting error: TypeError: original_request(...).then is not a function

I think I need to change the request-promise function in injector but I am not sure how to structure it. I've tried doing () => Promise.resolve([{id: '12345'}]) but that gives me error JavaScript SyntaxError{} (no information with it)

plasticine commented 6 years ago

Getting error: TypeError: original_request(...).then is not a function

You’re correct — you’ve changed original_request(...) to return an array;

injector({'request-promise': () => [{id: '12345'}]})

and are then trying to call .then on it.

As for your SyntaxError, I’d need more information to be able to assist you with that. Regardless I don’t think this is an error with inject-loader itself, so I’m going to close this out unless you can manage to create a repro for me.