简体   繁体   English

如何使用jasmine和node从命令行测试jQuery插件?

[英]How to test jQuery plugin from command line using jasmine and node?

I have jQuery plugin that I'm testing. 我有jQuery插件,我正在测试。 I found this question: How to run Jasmine tests on Node.js from command line? 我发现了这个问题: 如何从命令行对Node.js运行Jasmine测试? but when I run: 但是当我跑步时:

node_modules/jasmine-node/bin/jasmine-node --verbose --junitreport --noColor spec

I got errors that $ is not defined. 我得到了$ $未定义的错误。 How can I include jQuery? 我怎么能包含jQuery? (Right now I only test utilities that don't interact with the dom). (现在我只测试不与dom交互的实用程序)。

You will first need to create a DOM that jQuery can do its thing on. 您首先需要创建一个jQuery可以执行其操作的DOM。 You should set this up as a global variable, as you are probably accessing jQuery (or $) on the window element. 您应该将其设置为全局变量,因为您可能正在访问窗口元素上的jQuery(或$)。

First, add jquery and jsdom to your package.json file, eg: 首先,将jquery和jsdom添加到package.json文件中,例如:

  "dependencies": {
    "jquery": "*"
  },
  "devDependencies": {
    "jsdom": "*"
  }

Run npm install to install the new dependencies you just added. 运行npm install以安装刚刚添加的新依赖项。

Then, at the top of your spec file, do something like this: 然后,在spec文件的顶部,执行以下操作:

var jsdom = require("jsdom").jsdom;
global.window = jsdom().parentWindow;
global.jQuery = global.$ = require("jquery")(window);

This creates a window element using jsdom, and lets jquery know what to act on. 这将使用jsdom创建一个窗口元素,并让jquery知道要操作的内容。 You should now be able to access both jQuery and $ in your specs. 您现在应该能够访问规范中的jQuery$

I've had some difficulty getting this to work too. 我也遇到了一些困难。 What I've found works (by going to the jsdom github page: https://github.com/tmpvar/jsdom ) is to do the following. 我发现的工作(通过访问jsdom github页面: https//github.com/tmpvar/jsdom )将执行以下操作。

Install the following two packages (as suggested by Farid): 安装以下两个软件包(如Farid所建议):

npm install jquery --save
npm install jsdom --save-dev

Then at the top of your spec (test) file type the following: 然后在spec(测试)文件的顶部键入以下内容:

const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM(`<!DOCTYPE html>`);

global.jQuery = global.$ = require("jquery")(window); 
global.window = window;
global.document = window.window;

Now you have set $ , window and document globally. 现在,您已全局设置$windowdocument This is my first time using javascript, so if I have done something wrong, please let me know! 这是我第一次使用javascript,所以如果我做错了,请告诉我!

PS There is a neat way to run your tests using npm (I learnt it from here: https://www.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/ ). PS有一种利用npm运行测试的简洁方法(我从这里学到了它: https//www.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/ )。 Inside your package.json file add a "scripts" section, eg package.json文件中添加“脚本”部分,例如

{
  "dependencies": {
    "jquery": "^3.2.1"
  },
  "devDependencies": {
    "jasmine-node": "^1.14.5",
    "jsdom": "^10.1.0"
  },
  "scripts": {
    "test": "jasmine-node --verbose --color spec/*"
  }
}

Now you can run your tests by simply typing: 现在您可以通过输入以下命令来运行测试:

npm run test

or just npm test for short. 或者只是npm test

By try and error I came up with this code that work with jQuery and jsdom inside jasmine-node spec file: 通过尝试和错误,我想出了这个代码,它可以在jasmine-node规范文件中使用jQuery和jsdom:

if (typeof window === 'undefined') {
    var jsdom = require("jsdom");
    global.document = jsdom.jsdom();
    global.window = global.document.parentWindow;
    var navigator = {userAgent: "node-js", platform: "Linux i686"};
    global.window.navigator = global.navigator = navigator;
    navigator.platform = "Linux i686";
    global.jQuery = global.$ = require("jquery");
}

Using jsdom v7.2.2 and node v4.2.4. 使用jsdom v7.2.2和node v4.2.4。 I got it to work with the following code: 我让它使用以下代码:

var jsdom = require("jsdom").jsdom;
global.window = jsdom().defaultView;
global.jQuery = global.$ = require("jquery");
var myModuleThatReliesOnJquery = require("./myModuleThatReliesOnJquery.js");

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

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