tnwinc/Isolate — pull request #7 by stephenhand (2013-07-06 22:38:37+00:00)
Title: Added support for mapping objects & factories for specific requesting modules (or matchers for them) only. Unit tests now run on Windows

Firstly thanks for doing this project, I was laying the groundwork to do something exactly the same when I found it!

Secondly this is my first attempt at trying to collaborate on github so apologies if I'm not doing things wrong / not observing proper conventions

I have updated my fork of Isolate so it can be used to help me mock out my requirejs project in the same style as I'm used to in other projects. I always create mocks that are specific to each module under test, and it makes sense to define those mocks in the same file that the test is defined.

As it was, I couldn't do this elegantly with the head version of isolate because I had to define mocking behaviour for each mocked module in a single factory mapping (and put switch logic in their if I wanted different mocking behaviour for each module under test). To address this I extended the 'map' and 'mapAsFactory' methods so the factory / object can be mapped to be valid against a requested module AND a requesting module (or a matcher pattern). This allows developers to define a set of mappings specific to the module being tested at the top of the mocha test definition file.

A mapping that is for a specific calling module is used in preference to a mapping defined generally for a requested module, regardless of when each are defined, but otherwise they follow the existing 'use most recently defined' precedence logic.

I haven't added this functionality to the 'mapType' method, wasn't sure it was useful but can do so. There is no support for the new kind of mapping using the 'mapping hash' interface because I couldn't see a logical or elegant way of extending the existing convention to support it.

The extensions are unit tested & the readme has been updated.

In the process of adding unit tests I found that the existing test project I found it didn't work with Windows and newer node/npm versions so updated the rakefile & test files to remove Unix / Linux specific bash commands and use node_modules rather than updating NODE_PATH (which would require separate code for Windows & Linux to work and seemed flaky on my Windows tests anyway). I regression tested these changes on Ubuntu 12.04 using latest node/npm/ruby versions.

The functionality updates and compatibility changes are independent of each other, so if only one or the other are needed I can seperate them out.

Thanks

Steve