我可以在加载 Node 模块之前将全局变量注入到该模块中吗?
我正在编写一个面向浏览器的应用程序,并且我想为我的库代码编写自动化测试。我想在浏览器之外的命令行环境(例如 Node)中运行这些测试。我还没有选择测试框架,尽管我倾向于 BDD 框架,例如 Mocha 或茉莉花。
我的代码取决于 Knockout 的可观察属性。不幸的是,Knockout 库不仅包含可观察的属性,还包含将它们绑定到 DOM 的代码。因此,如果我只是尝试从 Node 中 require()
Knockout 库,我会收到错误,因为该库试图引用 window
、navigator
和 Node.js 中未定义的 document
变量。 Knockout 实际上并没有使用这些变量中的任何一个 - 它主要只是 DOM 绑定的一些功能检测,这对我来说并不重要 - 但因为它确实使用了它们,所以 Node甚至不会加载文件。
所以这是我的问题:我可以在模块加载时将虚拟变量注入到模块中吗?我可以做一些事情说“在加载源文件之后,但在开始运行它之前,让我在模块内设置了一些变量,以便代码能够成功运行”?
我知道 CoffeeScript 已经修补了 Node 的模块加载器,因此除了 .coffee
文件之外,它还可以加载 .coffee
文件代码>.js;所以看起来可能还有一些方法可以修补它以注入变量,如果我知道如何的话。
I'm writing an app that's destined for the browser, and I want to write automated tests for my library code. I'd like to run those tests outside the browser, in a command-line environment like Node. I haven't yet selected a test framework, though I'm leaning toward a BDD framework like Mocha or Jasmine.
My code depends on Knockout's observable properties. Unfortunately, the Knockout library contains not just observable properties, but also code for binding them to the DOM. So if I just try to require()
the Knockout library from Node, I get errors because the library is trying to reference the window
, navigator
, and document
variables that are undefined in Node. Knockout actually doesn't use any of these variables much -- it's mostly just a bit of feature detection for the DOM bindings, which don't matter to me -- but because it does use them, Node won't even load the file.
So here's my question: can I inject dummy variables into the module as it loads? Could I do something to say "after you've loaded the source file, but before you've started running it, let me set a few variables inside the module so the code will run successfully"?
I know CoffeeScript already patches Node's module loader so it can load .coffee
files in addition to .js
; so it seems like there might also be some way to patch it to inject variables, if I only knew how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,是的,你可以——创建它们然后使用它们,全局变量没有隔离性。
或者,您可能希望以文本方式将填充程序包裹在代码周围,将它们提供在闭包内。 jsdom 执行类似的操作来运行 jQuery。 (它为此提供了一个真正的 DOM!)
Yes, yes you can -- create them and then use them, globals have no isolation.
Or, you might want to wrap a shim around the code textually, providing them inside a closure. jsdom does things like this to run jQuery. (And it provides a real DOM for that!)
也许使用 felixge 的节点 sandboxed-module,您可以对淘汰库进行沙箱并将全局变量注入到沙盒模块。
来自自述文件:
Perhaps using felixge's node sandboxed-module, you can sandbox the knockout library and inject globals into the sandboxed module.
From the readme:
Node.js 并不是常用意义上的“无头”环境(无头浏览器)。
jsdom 可以带你走得更远,但你应该看看 PhantomJS,它是无头 webkit,带有 DOM 和所有内容,并且应该完美适合您的用例。
Node.js isn't a "headless" environment in the sense it's commonly used (a headless browser).
jsdom can take you further along, but you should look into PhantomJS, which is headless webkit, with DOM and all, and should fit your use case perfectly.