简体   繁体   English

错误:“ jQuery需要带有文档的窗口”以及Mocha和Typescript?

[英]Error: “jQuery requires a window with a document” with mocha and typescript?

I have some typescript mocha unit tests that I'm running; 我有一些正在运行的打字稿摩卡单元测试; I have successfully imported jquery, but I get an error: 我已经成功导入了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"

});

I read a similar question where the solution in node.js was: 我读到一个类似的问题 ,node.js中的解决方案在哪里:

var $ = require('jquery')(require("jsdom").jsdom().parentWindow);

but I tried to use typings to install jsdom and I get a different error which indicates that I'm not using the correct source... 但我试图用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>

Also it appears that the author has dropped node.js in favor of io.js 此外,似乎作者已放弃了node.js,转而使用io.js

Should I be using something else, other than jsdom? 我是否应该使用除jsdom以外的其他东西?

This question is related to another question . 这个问题与另一个问题有关

Failed to state in my question that I am using the following versions: 无法在我的问题中声明我正在使用以下版本:

  • node.js v4.2.6 node.js v4.2.6
  • typings 2.1.1 打字2.1.1
  • npm 3.5.2 npm 3.5.2

  • jquery ^3.2.1 jQuery ^ 3.2.1

  • jdom ^3.1.2 jdom ^ 3.1.2

  • @types/jsdom ^2.0.30 @ types / jsdom ^ 2.0.30

Did the following (please let me know if it is not elegant) 是否进行了以下操作(请告诉我它是否不美观)

// ...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);
    });
});

暂无
暂无

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

相关问题 错误:jQuery 需要一个带有文档的窗口 - Error: jQuery requires a window with a document 如何解决“jQuery需要一个带文档的窗口”错误? - How to solve “jQuery requires a window with a document” error? “ jQuery需要带有文档的窗口” Webpack 2错误 - “jQuery requires a window with a document” webpack 2 error 操作 jQuery 得到一个错误:“jQuery 需要一个窗口和一个文档” - Manipulating jQuery to get an error: "jQuery requires a window and a document" 带有Jquery集成的Nightwatch失败,并显示以下错误:jQuery需要带有文档的窗口 - Nightwatch with Jquery integration fails with the error jQuery requires a window with a document Webpack添加jQuery插件:错误:jQuery需要带有文档的窗口 - Webpack adding jQuery plugin: Error: jQuery requires a window with a document Browserify with jQuery> = 2产生“jQuery需要一个带文档的窗口” - Browserify with jQuery >= 2 produces “jQuery requires a window with a document” jQuery 需要一个 window 和 vue native 中的文档 - jQuery requires a window with a document in vue native 错误:jQuery需要一个window带证件。 使用ejs和javascript创建刽子手web app - Error : jQuery requires a window with a document. Using ejs and javascript to create a hangman web app 使用 Mocha 使用 Typescript 对 Jquery 进行单元测试时,如何修复“$ 未定义”错误? - How to fix “$ is not defined” error when unit testing Jquery with Typescript using Mocha?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM