You-Dont-Know-JS icon indicating copy to clipboard operation
You-Dont-Know-JS copied to clipboard

"this & object prototypes": Unit testing classless code?

Open novaluke opened this issue 11 years ago • 7 comments

JavaScript's classless and dynamic system of objects is fascinating and fantastic - I can't wait to try out what I've learned from reading this book. However, despite all the power of using the prototype system properly I do feel as if it's taken the legs out from under me in one regard - unit testing.

With a class-based approach an object's class is a specification of how it can be expected to behave, etc. Therefore checking if an object is an instance of a class is a concise and easy way to check if it supports the behaviour it needs to support. With JavaScript's lack of static classes though, it seems like there's no longer any way to obtain a reliable descriptor of how an object can be expected to behave. I don't find this to be an issue in production JavaScript code since checking an object's 'class' is a code smell, but when unit testing being able to check it's 'class' seemed a very useful tool to me to quickly verify if a function created the right kind of object.

For example, using CoffeeScript 'classes' I whipped up a quick ORM (inspired by Rails' ORM) for the non-standard database that we're currently using on a project. Each database record is an instance of a specific class with methods specific to the table that it comes from (eg. getRelatedUser()), and all these classes inherit from a single base abstract class which defines generic methods such as save() and delete().

In many places in my current codebase I unit test a function to check that it passes the right class of object to another function. This is a concise way to check that, for example, it supports the relatedOtherTable() and save() functions - if it didn't, the other function would end up throwing a TypeError when trying to call them. Obviously this check relies on checking the object's 'class', though, and that no longer works when going classless. Simply checking that the functions exist isn't necessarily good enough either, because they could exist and be noop functions, and the app would break and the test would be a false positive. Checking that the object is an instance of a class where the functions have been tested to exist and work appropriately is a way to make sure that the code is functioning properly while keeping the tests DRY (otherwise we'd have to repeat the tests for the methods every time we passed around an object that needs to have them).

I suppose it would be possible to use the Foo.isPrototypeOf(Bar) and Object.getPrototypeOf(Bar) methods to check this sort of thing when utilizing prototypes properly as per Chapter 6, but I don't know if these kinds of checks in the unit tests would be a code smell or not. It also doesn't seem to quite do the same thing - in a class-based language if an object is an instance of a class we know it's an exact copy of the class 'blueprint', and therefore getting its class also gives us information about its own properties/methods. On the other hand, just because an object has a specific prototype in JavaScript doesn't necessarily mean it's anything at all like its prototype - it could have redefined any/all of the delegate properties!

I'm super stoked on how awesome this 'new' (to me at least) way of approaching JavaScript is, but I just don't see how to adopt it while still maintaining proper testing. Any thoughts? :)

novaluke avatar Apr 09 '15 20:04 novaluke

Great questions and points. I will try to come back to this at some point soon and provide some more commentary/feedback.

getify avatar Apr 09 '15 20:04 getify

Thanks! Looking forward to it!

novaluke avatar Apr 09 '15 21:04 novaluke

Hi @getify, any further thoughts on this?

novaluke avatar May 07 '15 01:05 novaluke

sorry for being so slow... got a bunch of issue threads i need to get to here. it's busy travel season. :/

getify avatar May 07 '15 02:05 getify

Im looking forward to the response.

astericky avatar May 07 '15 03:05 astericky

I understand - I just wanted to make sure I didn't fade into obscurity :)

novaluke avatar May 08 '15 13:05 novaluke

Sorry it's been so long and I never came back and responded. Fell off my radar.

It also doesn't seem to quite do the same thing - in a class-based language if an object is an instance of a class we know it's an exact copy of the class 'blueprint', and therefore getting its class also gives us information about its own properties/methods. On the other hand, just because an object has a specific prototype in JavaScript doesn't necessarily mean it's anything at all like its prototype - it could have redefined any/all of the delegate properties!

It seems your question comes down to this observation you make. In a prototypal system, there is no guarantee provided by any sort of "isA" check that it really conforms to some behavior, so you don't get that shortcut for unit testing something.

Regardless of if you use OLOO or ES6 class or just ES5 pseudo-classical inheritance, no usage of instanceof or isPrototypeOf is going to give you the same kind of assertion in JS as similar code in another non-prototypal language would.

Bottom line: I don't think we can test JS code based on such assumptions. We have to test every part of the unit's behavior, not just its shape.

getify avatar Jan 15 '16 13:01 getify