-
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
Replace method in tested file #339
Comments
More broadly, trying to mock something on the subject under test is considered an anti-pattern ("contaminated test subject"), because it reduces confidence in the veracity of the thing being tested. I'd call it a special subtype of a partial mock. My advice would be to keep the dependencies of the subject in separate modules so the delineation is crystal clear to the test and to readers. Of course, ignoring this advice, you can accomplish what you want with: const lib = require('./lib.js')
td.replace(lib, 'addOne') And only |
Thanks for your response. I understand what you are trying to say and will probably have to rewrite my code. However, I was trying to make the example work and it still fails. Actually the
|
Your issue is that you're assigning This could be a good example of why not to mock things on the subject. This would have worked: const lib = {
addOne: function (x) {
return x + 1
},
getTwo: function () {
return lib.addOne(1)
}
}
module.exports lib |
Ahh ok, thank you for the answer. |
If I replace the line : it shows there is no invocation of addOneDouble. |
@nohaibogdan1 because |
Hello, I am not sure if this feature is missing or I am just missing some piece of documentation but I am unable to replace the method inside the tested file. Here is the example what I am trying to accomplish.
As you can see I am trying to replace and verify
addOne(1)
was called when both functions are imported from same file. I know about this being possible withbabel-plugin-rewire
but did not manage to get it running withtd.replace()
. Perhaps there is a missing piece of documentation which should say this is not possible or example how to achieve it or I just can't find it.The text was updated successfully, but these errors were encountered: