You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionregisterTSNode(root: string){consttsconfig=loadTSConfig(root)}functionloadTSConfig(root: string): TSConfig|undefined{console.log('loadTSConfig unstubbed')// function to be stubbed...}exportfunctiontsPath(root: string,orig: string|undefined): string|undefined{registerTSNode(root)}
and I'm requiring it with proxyquire as follows:
conststubLoader=(_: any)=>{console.log('loadTSConfig proxyquire')returnconfig}consttsNodePlugin=proxyquire('../src/ts-node',{'loadTSConfig': stubLoader})// This prints "loadTSConfig unstubbed" not "loadTSConfig proxyquire"!tsNodePlugin.tsPath('poop','asdf')
But unfortunately proxyquire is not actually injecting stubLoader into the module as expected.
I've tried:
Exporting all of the functions in ts-node.ts
Calling tsNodePlugin.registerTSNode, and even tsNodePlugin.loadTSConfig directly
Adjusting the stub key: '.loadTSConfig', './loadTSConfig', etc
Clearing the cache with delete require.cache[require.resolve('../src/ts-node')]
all to no avail.
The text was updated successfully, but these errors were encountered:
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).
Check out this PR (oclif/config#70) at this commit (oclif/config@eb4b915). 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.
The text was updated successfully, but these errors were encountered: