【发布时间】:2017-06-14 01:03:55
【问题描述】:
我正在尝试找到一种方法来运行 npm test,使用 mocha 在 HTML DOM 之上。在这种情况下,我使用 global document 从DOM 中检索table。但是,当我运行 npm test 时,我会收到类似错误的信息:
ReferenceError: document is not defined
at /home/luiz/Projects/linguist-unknown/src/scripts/ling-loader.js:92:61
at extFunc (/home/luiz/Projects/linguist-unknown/src/scripts/ling-loader.js:49:11)
at Array.every (native)
at Utilities.tryMatchUrlExtension (/home/luiz/Projects/linguist-unknown/src/scripts/ling-loader.js:60:25)
at Utilities.<anonymous> (/home/luiz/Projects/linguist-unknown/src/scripts/ling-loader.js:90:16)
at xhr.onload (/home/luiz/Projects/linguist-unknown/src/scripts/ling-loader.js:24:11)
at dispatchEvent (/home/luiz/Projects/linguist-unknown/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:591:25)
at setState (/home/luiz/Projects/linguist-unknown/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:614:14)
at IncomingMessage.<anonymous> (/home/luiz/Projects/linguist-unknown/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:447:13)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
1) should refresh table
16 passing (3s)
1 failing
1) Loader Utilities should refresh table:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
我知道document 是undefined,我需要以某种方式自己创建一个,但是,我认为我的主要问题是:
- 我第一次使用
npm和mocha,但我在他们的文档中找不到任何相关内容。 - 大多数情况下,人们遇到的所有问题都与网络浏览器有关//我使用的是 CLI,它将在 Github 上使用 Travis 进行测试
- 在下面的代码中,您会看到我用
XMLHttpRequest解决了类似的问题。但是,我只是想不出将document变量正确包含到我的测试中的最佳方法。
因此,请原谅我问这个答案是否已经在 stackoverflow
上我的代码如下:
test-utilities.js
...
global.XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
global.jsyaml = require('../src/scripts-min/js-yaml.min.js');
global.LinguistHighlighter = require('../src/scripts/ling-highlighter.js').LinguistHighlighter;
var LinguistLoader = require('../src/scripts/ling-loader.js').LinguistLoader;
describe('Loader', function () {
var utilities = new LinguistLoader.Utilities();
it('should refresh table', function(done) {
var location = {
hostname: "github.com",
href: "https://github.com/github-aux/linguist-unknown/blob/chrome/examples/Brain/human_jump.brain",
pathname: "/github-aux/linguist-unknown/blob/chrome/examples/Brain/human_jump.brain"
};
// check if it is not breaking
utilities.refresh(location, function(langObj, table){
done();
});
});
});
...
utilities.js:
...
Utilities.prototype.refresh = function(location, callback) {
var new_url = location.href;
if (new_url === current_url || !this.isGithub(location)) {
return;
}
current_url = new_url;
if (linguistObj === null) {
linguistObj = {
path: this.getPossibleFilepath(location)
};
}
setTimeout(function() {
var downloadHelper = new DownloadHelper();
downloadHelper.load(linguistObj.path, function(objs){
this.tryMatchUrlExtension(current_url, objs, function(langObj){
var table = document.getElementsByClassName("blob-wrapper")[0]
.getElementsByTagName("table")[0];
new LinguistHighlighter.Highlighter(langObj).draw(table);
// callback for tests purposes only
if (callback) {
callback(langObj, table);
}
});
}.bind(this));
}.bind(this), 100);
};
...
感谢任何帮助。谢谢!
【问题讨论】:
-
CLI 没有 DOM,您必须在浏览器中运行 DOM 测试。您可以使用 selenium(seleniumhq.org) 和 mocha 进行测试。还有一些商业工具,如酱实验室和浏览器堆栈。更多详情请参考这篇文章developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/…和selenium的文档。
-
嗨@YiKai,感谢您的回答,Selenium 确实是帮助我解决这个问题的好工具,但是,我找到了一个 100% 使用 CLI 和 jsdom 的解决方案!
-
是的,这似乎是一个更简单的解决方案。
标签: javascript node.js dom npm mocha.js