简体   繁体   English

使用带有jsTestDriver的闭包库

[英]Using closure library with jsTestDriver

I'm learning about google closure tools by writing a simple JavaScript game. 我正在通过编写一个简单的JavaScript游戏来学习谷歌关闭工具。 I'm having trouble figuring out how to set up jsTestDriver so that it works well with closure library. 我无法弄清楚如何设置jsTestDriver以便它与闭包库一起使用。

Specifically: I'd like to use the goog.require mechanism to include any additional JavaScript files rather than have to manually add them all to the config file. 具体来说:我想使用goog.require机制来包含任何其他JavaScript文件,而不必手动将它们全部添加到配置文件中。

Following meyertee's suggestion I made a simple script to automatically write the dependencies to a config file 根据meyertee的建议,我制作了一个简单的脚本来自动将依赖项写入配置文件

#!/bin/bash
cp tests/jsTestDriver.conf.proto tests/jsTestDriver.conf
libs/closure-library/closure/bin/build/closurebuilder.py --root="./libs/closure-library" --root="./js" --namespace="lds" | sed "s#^#  - \.\./#" >> tests/jsTestDriver.conf

The tests/jsTestDriver.conf.proto file is a simple template: tests / jsTestDriver.conf.proto文件是一个简单的模板:

test:
  - "*.js"
load:
  - ../libs/knockout-2.1.0.js
# Crucial, the load key needs to be last, and this comment must be followed by a newline.

It is a very fragile script, but hopefully someone (other than me) will find it useful. 这是一个非常脆弱的脚本,但希望有人(除了我)会发现它很有用。

You can do it semi-automatically by letting Closure Compile generate a manifest file, which will output all files in the correct order of dependency. 您可以通过让Closure Compile生成清单文件来半自动执行,该清单文件将以正确的依赖顺序输出所有文件。 You can then transform that file to relative paths and paste them into the JsTestDriver config file. 然后,您可以将该文件转换为相对路径并将其粘贴到JsTestDriver配置文件中。 That's how I do it. 我就是这样做的。
You could even write a script that does this transformation automatically. 您甚至可以编写一个自动执行此转换的脚本。

This is the relevant compiler argument: 这是相关的编译器参数:

--output_manifest manifest.MF

There are some details on the Closure Compiler's Google Code Wiki Closure Compiler的Google Code Wiki有一些细节

Edit: There are also some Python scripts to help you calculate dependencies. 编辑:还有一些Python脚本可以帮助您计算依赖项。 You can use calcdeps.py or closurebuilder.py to generate a manifest file, which even includes files that haven't been 'required' by your code. 您可以使用calcdeps.py或closurebuilder.py生成清单文件,该文件甚至包含您的代码未“必需”的文件。

Since JsTestDriver does not following the Closure Library convention of declaring dependencies with goog.provide() and goog.require() , your best option may be meyertee's solution. 由于JsTestDriver没有遵循使用goog.provide()goog.require()声明依赖关系的Closure Library约定,因此您最好的选择可能是meyertee的解决方案。

However, the Closure Library includes its own testing framework. 但是,Closure Library包含自己的测试框架。 See: 看到:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM