我有一些单页演示,如下所示:http://notjs.org/examples/forms/formmode_demo.html
我想,哇,如果可以在我现有的 Mocha 茶坏度设置中编写一些测试来直接测试示例,那就太棒了:
example = 'forms/formmode_demo.html'
describe "examples/#{example}", () ->
before (done) ->
loadExample example, () => done()
it 'should load in full input mode', () ->
window.$('input').length.should.equal 2
describe "in full input mode", () ->
it 'should have visible save', () ->
window.$('button.success').is(':visible').should.be.true
it 'should have visible cancel', () ->
window.$('button.cancel').is(':visible').should.be.true
it 'should not have put inputs on readonly class elements', ()->
window.$('.readonly input').length.should.equal 0
it 'should have input with author name', () ->
window.$("[data-not_attr='name'] input:text").val().should.equal window.dataObject.name
如果我能做到这一点,那么我就永远不会遭受那种尴尬的时刻,“你的示例 x 几个月前就停止工作了”。
我让它工作,但只能使用来自 http://url 的页面 src 中的脚本标记。如果我必须在测试之前将包部署到服务器,那么测试的效果就不那么有效了。
示例页面加载 jquery、underscore 和 notjs,并且需要加载它们才能作为独立演示运行。它还应该能够从本地文件系统运行,并且可以使用相对 URL。我最初在页面上有 notjs 脚本标记指向相对路径,例如
<script src="../../notjs.basics.js"></script>
而不是
<script src="http://notjs.org/notjs.basics.js"></script>
但是 notjs.basics.js 永远不会加载(window.Notjs == 在测试中未定义),并且我没有看到 jsdom 的任何错误。我尝试过设置 documentRoot jsdom 选项,但这似乎不起作用。
作为引用,loadExample 方法和我的 jsdom 设置的其余部分在这里:https://github.com/bee-hub/notjs/blob/master/test/testHelper.coffee
感谢您的帮助和建议。
最佳答案
在我能看到的任何地方都没有记录它,但是 jsdom 0.8.4 版本中的 test/jsdom/index.js 中的 testLocal() 函数为 jsdom.jsdom(…) 提供了一个“url”选项,这似乎实现与您期望从 documentRoot 选项获得的目标大致相同的目标。使用它,我能够从本地文件系统上的 HTML 文档解析相对脚本源路径。
就其值(value)而言,文档中也没有提及“documentRoot”。它的用途是anecdotally endorsed in a github issue ,所以也许它在早期版本中有效。
不要忘记 jsdom.jsdom(...) 会异步加载脚本,因此请务必将测试与文档就绪事件 Hook 。
祝你好运!
关于javascript - 具有相对路径的 jsdom 和 script 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15604883/