Error: "jQuery requires a window with a document" with mocha and typescript?
Error: "jQuery requires a window with a document" with mocha and typescript?
我有一些 typescript mocha 单元测试 运行;我已成功导入 jquery,但出现错误:
import * as $ from 'jquery'; // or maybe this should be in beforeEach() ?
...
it('should append a div', function() {
$("body").append("div"); // ERROR: Causes "jQuery requires a window with a document"
});
我读了一篇 similar question,其中 node.js 中的解决方案是:
var $ = require('jquery')(require("jsdom").jsdom().parentWindow);
但我尝试使用 typings
安装 jsdom
,但我收到了一个不同的错误,表明我没有使用正确的源...
$ typings search jsdom
NAME SOURCE HOMEPAGE DESCRIPTION VERSIONS UPDATED
jsdom dt https://github.com/tmpvar/jsdom 2 2017-02-13T06:16:22.000Z
$ typings install jsdom
typings ERR! message Unable to find "jsdom" ("npm") in the registry.
typings ERR! message However, we found "jsdom" for 1 other source: "dt"
typings ERR! message You can install these using the "source" option.
typings ERR! message We could use your help adding these typings to the registry: https://github.com/typings/registry
typings ERR! caused by https://api.typings.org/entries/npm/jsdom/versions/latest responded with 404, expected it to equal 200
typings ERR!
typings ERR! cwd /home/accounting/Documents/dev/anki-accounting-unit-tests
typings ERR! system Linux 4.4.0-78-generic
typings ERR! command "/usr/bin/nodejs" "/usr/local/bin/typings" "install" "jsdom"
typings ERR! node -v v4.2.6
typings ERR! typings -v 2.1.1
typings ERR!
typings ERR! If you need help, you may report this error at:
typings ERR! <https://github.com/typings/typings/issues>
而且作者似乎已经放弃了node.js in favor of io.js
我应该使用 jsdom 以外的其他东西吗?
此问题与有关。
未能在我的问题中说明我使用的是以下版本:
- node.js v4.2.6
- 打字 2.1.1
npm 3.5.2
jquery^3.2.1
jdom ^3.1.2
@types/jsdom^2.0.30
是否做了以下事情(有不雅之处请告知)
// ...chai and mocha imports...
import * as jsdom from 'jsdom';
import * as jq from 'jquery';
// Import code to test...
import MeCode from './pirates/MeCode';
// ..
// testing inits
chai.use(chaiAsPromised);
const expect = chai.expect;
const assert = chai.assert;
const should = chai.should();
let mc,$ = null;
let someId = "yarId";
describe('Main Hooks', () => {
// ..
beforeEach(function() {
// Initialize jQuery with a DOM it can work on....
$ = jq(jsdom.jsdom().parentWindow);
// Initialize the object I'm testing...
mc = new MeCode($, someId);
});
afterEach(function() {
// clean up
// ...
});
// ..
it('should generate html', function() {
($("#" + someId).length).should.be.equal(1);
});
});
我有一些 typescript mocha 单元测试 运行;我已成功导入 jquery,但出现错误:
import * as $ from 'jquery'; // or maybe this should be in beforeEach() ?
...
it('should append a div', function() {
$("body").append("div"); // ERROR: Causes "jQuery requires a window with a document"
});
我读了一篇 similar question,其中 node.js 中的解决方案是:
var $ = require('jquery')(require("jsdom").jsdom().parentWindow);
但我尝试使用 typings
安装 jsdom
,但我收到了一个不同的错误,表明我没有使用正确的源...
$ typings search jsdom
NAME SOURCE HOMEPAGE DESCRIPTION VERSIONS UPDATED
jsdom dt https://github.com/tmpvar/jsdom 2 2017-02-13T06:16:22.000Z
$ typings install jsdom
typings ERR! message Unable to find "jsdom" ("npm") in the registry.
typings ERR! message However, we found "jsdom" for 1 other source: "dt"
typings ERR! message You can install these using the "source" option.
typings ERR! message We could use your help adding these typings to the registry: https://github.com/typings/registry
typings ERR! caused by https://api.typings.org/entries/npm/jsdom/versions/latest responded with 404, expected it to equal 200
typings ERR!
typings ERR! cwd /home/accounting/Documents/dev/anki-accounting-unit-tests
typings ERR! system Linux 4.4.0-78-generic
typings ERR! command "/usr/bin/nodejs" "/usr/local/bin/typings" "install" "jsdom"
typings ERR! node -v v4.2.6
typings ERR! typings -v 2.1.1
typings ERR!
typings ERR! If you need help, you may report this error at:
typings ERR! <https://github.com/typings/typings/issues>
而且作者似乎已经放弃了node.js in favor of io.js
我应该使用 jsdom 以外的其他东西吗?
此问题与
未能在我的问题中说明我使用的是以下版本:
- node.js v4.2.6
- 打字 2.1.1
npm 3.5.2
jquery^3.2.1
jdom ^3.1.2
@types/jsdom^2.0.30
是否做了以下事情(有不雅之处请告知)
// ...chai and mocha imports...
import * as jsdom from 'jsdom';
import * as jq from 'jquery';
// Import code to test...
import MeCode from './pirates/MeCode';
// ..
// testing inits
chai.use(chaiAsPromised);
const expect = chai.expect;
const assert = chai.assert;
const should = chai.should();
let mc,$ = null;
let someId = "yarId";
describe('Main Hooks', () => {
// ..
beforeEach(function() {
// Initialize jQuery with a DOM it can work on....
$ = jq(jsdom.jsdom().parentWindow);
// Initialize the object I'm testing...
mc = new MeCode($, someId);
});
afterEach(function() {
// clean up
// ...
});
// ..
it('should generate html', function() {
($("#" + someId).length).should.be.equal(1);
});
});