Closed samuela closed 5 years ago
Hi, proxyquire operates on modules, and not by munging your local scope within a module. You can't stub something in the local scope of the module under test. You can stub something that your module under test requires. We believe this is important to maintainable tests.
If your tests can manipulate the local variables of modules under test, there is no isolation between your tests and your source code. Changing an internal function name within your source code should not break tests.
If you'd like to use proxyquire, I'd recommend pulling that function out to its own module. You'll then stub it by its path relative to the module under test, and not its identifier (which is private).
proxyquire('../src/ts-node', {'./load-ts-config': stubLoader})
Understood, thank you for the quick response.
Check out this PR (https://github.com/oclif/config/pull/70) at this commit (https://github.com/oclif/config/pull/70/commits/eb4b9156e5a5e887e364342c23596deff3f27790). The
ts-node.ts
module looks something like:and I'm requiring it with proxyquire as follows:
But unfortunately proxyquire is not actually injecting
stubLoader
into the module as expected.I've tried:
ts-node.ts
tsNodePlugin.registerTSNode
, and eventsNodePlugin.loadTSConfig
directlydelete require.cache[require.resolve('../src/ts-node')]
all to no avail.