Hi

I test client-side code with node and have some troubles with browser 
global objects and functions (when do mocking)

Here is the code.

----- lib
var run = require('lib').init()
module.exports = {
  'test global objects': function () {
    var jQueryMock = {}
    run(jQueryMock)
  }
}
----- test
exports.init = function ($) {
  return function () {
    var title = document.title
    var el = $('#element')
    var text = escape("text")
  }
}

One solution is to redefine global object/functions, which I don't like 
since it might interfere with other
tests that run in parallel.
----- lib
var run = require('lib').init()
module.exports = {
  'test global objects': function () {
    var jQueryMock = {}
    global.document = {}
    global.escape = function () {}
    run(jQueryMock)
  }
}
----- test
exports.init = function ($) {
  return function () {
    var title = document.title
    var el = $('#element')
    var text = escape("text")
  }
}

Another one is to provide a context for browser global functions.
Seems like a better way to go to me ...
----- lib
var run = require('lib').init()
module.exports = {
  'test global objects': function () {
    var jQueryMock = {}
    var win = {document: {}, escape: {}}
    var escape = function () {}
    run(jQueryMock)
  }
}
----- test
exports.init = function ($, window) {
  return function () {
    var title = window.document.title
    var el = $('#element')
    var text = window.escape("text")
  }
}

What would the good way to go in your opinions?
How do you do it?

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to