Saturday, February 4, 2012

Javascript Woes

Ever since I attended GeekFest at Groupon on Tuesday, I have been interested in working with Javascript a little more. The talk was about sharing code between client and server via Node.js. It really got me curious about what that would look like on real project. However, I've not had the opportunity to try it out yet. My exploration into Node.js and Javascript over the past week has led me down a very dark and desperate path. The first woe that I encountered in my exploration was modules. As I tinkered, I began to realize that defining and loading modules was a very fragile process. I was quickly dismayed. I did some googling and found and library called RequireJS. This library implements AMD protocol for Javascript modules, which is great because it offers a standard definition that Node and Browsers can use. So, in theory, one could write one application that runs in both places. I was happy again. Then I decided to explore some previously existing Javascript projects to see how they handle modularization. I took a look at Jasmine, the BDD framework for Javascript. As I was browsing their code, I realized that they didn't attempt to use any sort of module loading like I hoped. Instead, they compile everything into one file, test against it, and distribute it as such. This isn't a bad method per se, but I think that it is possible to do better now that the toolset for Node has matured. Also, I was really displeased to see that Jasmine has a lot of Ruby files floating around their repository (mostly rake tasks). This also isn't bad per se, but once again, the Node toolset has matured and I think its possible to do better now. So, with that in mind, I decided I wanted to hack on Jasmine a little bit. First, I deleted all the Ruby files. Second, I decided to reorganize the project structure to separate Node specific files from Browser specific files. Now, I'm trying to convert all of Jasmine core into Node modules via RequireJS. This is when the headaches started. As it turns out, it is hard to test a testing framework. For instance, when I change the test Runner, not only does that break the tests, but it breaks the runner that which shows me that the tests are broken. I know, headaches. Anyways, I'm continuing to refactor Jasmine and learn more about Node. I'm hoping that soon I will have enough grasp to write a simple Todo's application where code is shared between client and server.

1 comment:

  1. You should look into Vows.js for testing JavaScript in Node.js

    ReplyDelete