【发布时间】:2011-12-05 03:27:10
【问题描述】:
我想使用 Vows 来测试无 DOM 的 JavaScript 代码,最好是直接针对已编译的 JS 运行。
我的誓言是用 CoffeeScript 编写的,但我不知道如何加载我的 JS;我尝试使用 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)
但我得到“ReferenceError: Cl is not defined”。
在浏览器控制台中运行缩小的 JS 和 new Cl.LinearExpression(5); 可以正常工作,因此编译后的代码是可以的。
将 JS 加载到节点以供 Vows 测试的最佳方式是什么?
【问题讨论】:
-
你可以用反引号内联JS,不要使用eval。
标签: node.js coffeescript google-closure-compiler vows