0

I'd like to use Vows to test DOM-free JavaScript code, ideally directly running against the compiled JS. My Vows are written in CoffeeScript, but I'm not sure how to load my JS; I've tried just inlining it using eval:

vows = require "vows"
assert = require "assert"
eval('var e=this;function f(a,g){var c=a.split("."),b=e;!(c[0]in b)&&b.execScript&&b.execScript("var "+c[0]);for(var d;c.length&&(d=c.shift());)!c.length&&g!==void 0?b[d]=g:b=b[d]?b[d]:b[d]={}}function h(a){a.call(e)};(function(){var a;a={};h(function(){a=function(a){this.a=a};a.prototype.b=function(a){return this.a+a.a};f("Cl.LinearExpression",a);f("Cl.LinearExpression.prototype.plus",a.prototype.b)})}).call(this);');

vows
  .describe("Linear Expression")
  .addBatch
    "initialized with a number":
      topic: -> new Cl.LinearExpression 5

      "adds up with scalar": (cle) -> assert.equal 7, cle.plus 2

  .export(module)

but I get "ReferenceError: Cl is not defined". Running the minified JS and new Cl.LinearExpression(5); in a browser console works fine, so the compiled code is okay. What's the best way to load JS into node for testing by Vows?

Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196
Kevin L.
  • 1,663
  • 1
  • 16
  • 21

3 Answers3

2

Rather than using eval, why not use Node's require? You can point to either a .js or .coffee file in a relative directory, like so:

Cl = require './cl.js'

In that file, add the line

module.exports = Cl

When the file is required, the return value of the require is the module's exports.

Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196
  • I'd like to avoid having to declare `module.exports` in my compiled JS since I won't be running it as a node library. I did just try that though, and I get the error "Cl is not defined" with the stacktrace showing it from "cl.js". It seems like Node is evaluating the JS somehow differently than Chrome does. – Kevin L. Dec 04 '11 at 18:12
  • You could also write `this.Cl = ...` (in the outermost scope). That will create a global named `Cl` in the browser, and an export named `Cl` in Node. Use `{Cl} = require './cl'` to access the exported object from you tests. – Trevor Burnham Dec 04 '11 at 18:22
  • I'm not saying `Cl =` anywhere in my JS---it's being compiled by Google's Closure compiler (with advanced optimizations on) and some magic is going on that seems to work in the browser but not in node. – Kevin L. Dec 04 '11 at 18:32
  • You might want to do your tests in a browser environment, then. If you use [js-test-driver](http://code.google.com/p/js-test-driver/), you can run browser tests from the command line. – Trevor Burnham Dec 04 '11 at 18:55
0

You can use backtick to embed javascript as-is.

`var e=this;function f(a,g){ ... `
Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
0

This is a namespace problem; importing with

codes = require "../out/compiled.js"
for k,v of codes
  global[k] = v

adds all of the compiled JS objects to the current namespace, where they can be accessed in Vows.

Unfortunately, I still don't know why using eval() or backticks with the inlined contents of compiled.js doesn't work.

Kevin L.
  • 1,663
  • 1
  • 16
  • 21