JavaScript is a dynamic language, and sometimes we can bend its rules a little.
One of the rules we rely upon is that one cannot override variables accessible via returned function.
We call these variables part of the closure, and turns out they can be mocked!
I will show how to fake variables defined outside the function's own scope and several practical use cases:
mocking server responses in live AngularJs application without any external tools
patching Node system require function to make it more powerful
testing functions for side-effects (purity test)
Anyone trying to really master the JavaScript should be aware of this technique, in my opinion.
speaker bio
Gleb Bahmutov is JavaScript ninja, image processing expert and software quality fanatic.
After receiving a PhD in computer science from Purdue University,
Gleb worked on laser scanners, 3D reconstruction, and panorama-based virtual tours at EveryScape.
Later Gleb switched to writing browser data visualization software at MathWorks.
After a year, Gleb went back to the startup environment and developed software quality
analysis tools at uTest (now Applause).
Today Gleb is developing real-time financial analysis tools at Kensho.
He blogs about software development topics at http://glebbahmutov.com/blog/
and links his projects at http://glebbahmutov.com/.
You can follow him and his work @bahmutov
abstract
JavaScript is a dynamic language, and sometimes we can bend its rules a little. One of the rules we rely upon is that one cannot override variables accessible via returned function. We call these variables part of the closure, and turns out they can be mocked! I will show how to fake variables defined outside the function's own scope and several practical use cases:
Anyone trying to really master the JavaScript should be aware of this technique, in my opinion.
speaker bio
Gleb Bahmutov is JavaScript ninja, image processing expert and software quality fanatic. After receiving a PhD in computer science from Purdue University, Gleb worked on laser scanners, 3D reconstruction, and panorama-based virtual tours at EveryScape. Later Gleb switched to writing browser data visualization software at MathWorks. After a year, Gleb went back to the startup environment and developed software quality analysis tools at uTest (now Applause). Today Gleb is developing real-time financial analysis tools at Kensho. He blogs about software development topics at http://glebbahmutov.com/blog/ and links his projects at http://glebbahmutov.com/. You can follow him and his work @bahmutov