babel-plugin-module-resolver icon indicating copy to clipboard operation
babel-plugin-module-resolver copied to clipboard

proxyquire relative path issue

Open hualu00 opened this issue 7 years ago • 2 comments

Bellowing is my original code sample:

const ProxyInboxModel = proxyquire('../model.js', {
        'messages': {
          ...
        }
}).default

It didn't work as 'messages' is not resolved by module-resolver, after reading this great explanation,
https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md#usage-with-proxyquire
I've changed my code to this:

const resolvePath =  c => c
const ProxyInboxModel = proxyquire('../model.js', {
       [ resolvePath('messages')]: {
          ...
        }
}).default

This time 'messages' was resolved, but with the wrong path. And the root cause is I use proxyquire in a test file, and this test file is not at the same folder with its test-ee.
Let's say
'a/b/c.js' and 'a/b/test/c-test.js'
in 'a/b/c.js', 'messages' is resolved as '../../path/messages',
but in 'a/b/test/c-test.js' , it is '../../../path/messages'

hualu00 avatar Jan 26 '18 06:01 hualu00

Did you also add the function to transformFunctions?

Shouldn't your code be like this?

const resolvePath =  c => c
const ProxyInboxModel = proxyquire('../model.js', {
       [ resolvePath('../model.js')]: {
          ...
        }
}).default

I'm not familiar with proxyquire, so I'm just trying to find the error with you :)

tleunen avatar Feb 05 '18 21:02 tleunen

seeing this in a project I inherited and am fixing up - moving the test to the same level as the file fixed it - not ideal, but works

patrickleet avatar Mar 01 '21 23:03 patrickleet