문제

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?

도움이 되었습니까?

해결책 3

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.

다른 팁

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.

You can use backtick to embed javascript as-is.

`var e=this;function f(a,g){ ... `
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top