Javascript TDD, with Require and Jasmine

As part of some code for a not-yet-publicly-visible project, I needed to extend an existing Javascript API.  Because I know my code generally needs all the help it can get, and because our team does unit-testing, I’m building it out via TDD: write a test that fails, write the code to make it run, rinse, repeat.

Challenge: our team is using PhantomJS to run Jasmine tests via karma.  All of the rest of our infrastructure, both test and development, makes use of RequireJS to handle dependencies.   I’m not well-versed in RequireJS, but it hasn’t been an issue so far, as I’d been extending stuff already built out.  Today, I needed to test code that was in a new set of files I’d created.   Wasn’t working.

Collecting notes here in terms of what I needed, which also provides clues to the discerning reader of what bit me….   My hope is to revisit this post sometime in the future and amend it as I’m a bit more expert in all of these areas…

– my code needed to be set up as a Require module with a define() block at the top, even though the thing I’m extending isn’t in a require module.  (We cheat and stub out our other invocations of the underlying API, so this was a new thought to me, that I needed to set this up as a Require module itself…)

– that module’s function needed to return something.  Just having things happen in the function within the define block really doesn’t do it.

– the new module needed to be made visible as a name in my test-main.js

– the new file needed to be made visible to the configuration to Phantom

Beyond the TDD, to then have my new module work within my webapp, I no longer need to link explicitly to the Javascript file, but instead just let our RequireJS stuff do its magic.   That required both adding it as a referenced package within our dojoConfig and then also adding it in as a dependency to the primary app module itself.  Not sure I’ve got in the spot I finally want it, in terms of usage within the app, but I can at least see my code come in/through now.

Takeaway: Lots of places to put things to make things “just work”.




Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>