-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stub one function and use originals for rest #368
Comments
This is by design, because what you're describing is a partial mock, and more specifically, a contaminated test subject, described in the linked wiki as:
That said, you can accomplish what you are asking with: const pathToJsFile = require('./path-to-js-file')
td.replace(pathToJsFile, 'fakeOne')
… |
@searls I don't think your example works - at least not for me. Although I am aware partial mocks are bad practise, legacy codebase requires that. Unfortunately, I cannot find a workaround around that :/ |
@froxCZ that's ok. Can you give me a reproducible example showing why the above isn't working in your case? |
@searls Thanks a lot for your quick reply. Here it is.
|
The issue you're having is made a little less obvious because you're (apparently) transpiling, so here's your example rewritten in something that doesn't require a build step: //file.js
function a() {
return 'a'
}
function b() {
return a() + 'b'
}
module.exports.a = a
module.exports.b = b //test.js
const td = require('testdouble')
const file = require('./file')
const mockA = td.replace(file, 'a')
td.when(mockA()).thenReturn('x')
console.log(file.b()) And in that case, as you said, running The reason that this is happening is because the reference to So, for instance, if you change the let stuff
module.exports = stuff = {
a: function a () {
return 'a'
},
b: function b() {
return stuff.a() + 'b'
}
} The test will print |
Yea I was expecting something like this. Thanks a lot for clarification! |
Description
I have a js file that has multiple functions. I want to stub out just one function in order to test another function that has a dependency on the first function.
Issue
When I do td.replace('path-to-js-file'), it replaces all the functions with a fake one. Probably as designed. Want to know if there is a way to stub one function and call another function(which is in the same file) that behaves as the original implementation.
The text was updated successfully, but these errors were encountered: