0

I have a web app written in Coffeescript that I'm testing with nodeunit, and I can't seem to get access to global variables ("session" vars in the app) set in the test:

src/test.coffee

root = exports ? this

this.test_exports = ->
    console.log root.export
    root.export

test/test.coffee

exports["test"] = (test) ->
    exports.export = "test"
    test.equal test_file.test_exports(), "test"
    test.done()

Results in output:

test.coffee
undefined
✖ test

AssertionError: undefined == 'test'

How do I access globals across tests?

4
  • Try to compile it with the b-Flag. This prevents the security closure from being added. Commented Jan 15, 2012 at 15:01
  • My source is compiled with the flag: nice -n 19 coffee -o web/ -b -c -w src/, I added the -b to Trevor Burnham's continuous Cakefile build = (watch, callback) -> if typeof watch is 'function' callback = watch watch = false options = ['-b', '-c', '-o', 'lib', 'src'] options.unshift '-w' if watch but no dice. Commented Jan 15, 2012 at 15:20
  • Try not using global variables? Commented Jan 16, 2012 at 12:52
  • Valid, but I'm porting my application to coffeescript and refactoring for unit tests on node, and I'd rather leave the de-globalizing of the session data as a last step. In addition, it may not be worth the marginal increase in complexity to ditch the globals. Commented Jan 16, 2012 at 14:01

2 Answers 2

1

Create fake window exported global for node:

src/window.coffee

exports["window"] = {}

src/test.coffee

if typeof(exports) == "object"
    window = require('../web/window')

this.test_exports = ->
    console.log window.export
    window.export

test/test.coffee

test_file = require "../web/test"
window = require "../web/window'"

exports["test"] = (test) ->
    window.export = "test"
    test.equal test_file.test_exports(), "test"
    test.done()

Not very elegant, but it works.

Sign up to request clarification or add additional context in comments.

Comments

0

You can share global state using the "global" object.

one.coffee:

console.log "At the top of one.coffee, global.one is", global.one
global.one = "set by one.coffee"

two.coffee:

console.log "At the top of two.coffee, global.one is", global.one
global.two = "set by two.coffee"

Load each one from a third module (an interactive session in this example)

$ coffee
coffee> require "./one"; require "./two"
At the top of one.coffee, global.one is undefined
At the top of two.coffee, global.one is set by one.coffee
{}

7 Comments

I fixed the example code to use "root" instead of "this" (if that was the particular you're referring to). Still no love. Basically, I'm assuming if I attach a var to exports in my test, I should be able to access it from exports in any file in the same node vm.
Yeah but there's no magic "export" variable. That is undefined since you haven't defined it. root vs this doesn't matter.
You're correct that "export" has no magic, but neither does "foo", "bar", "monkey", or "cheetah". To put it simply: How does one share data across files in node?
"exports" with an s. It's already working or else your test wouldn't even run. Just do "exports.foo = 42" in mod1.coffee and "mod1 = require 'mod1';console.log mod1.foo" in test.coffee.
I get that the global object in node is exports with an s. The code above is using it both to the publish the test and, theoretically, publish the variable (unfortunately named) export. I can't have src/test.coffee import test/test.coffee--it can't know about the test, obviously. In short, the answer is, you can't access globals in node without explicitly require-ing them.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.