How does proxyquire handle second level (indirect) requires of proxies modules?
Asked Answered
F

1

12

If we have three modules names A, B and C so module A requires B and B requires C: what would be the effect of this call?

var A = proxyquire('A', {'C': mockedModule})

Would module B get the mock or the real C module?

Foretop answered 29/5, 2014 at 23:45 Comment(0)
B
13

Only direct dependencies will be mocked.

But you can nest proxyquire statements, so in your example you could:

const A = proxyquire('../A', {
    './B': proxyquire('../B', {
        'C': mockC
    })
});

Where the file structure is like

root
 |-- A.js
 |-- B.js
 `-- tests
       `-- A.spec.js

And import C is not local.

Burrows answered 8/3, 2017 at 14:7 Comment(1)
property name 'B': … must be relative to where A is; the 'B' argument of the second proxyquire call must be relative to the file this expression is inUnbelt

© 2022 - 2024 — McMap. All rights reserved.