简体   繁体   English

从phantomjs运行摩卡

[英]Run mocha from phantomjs

I'd like to do some BDD using Mocha and PhantomJS only . 我只想使用Mocha和PhantomJS做一些BDD。
Define a few functional paths and test 'em. 定义一些功能路径并测试它们。

In short, I'd love to run the command phantomjs test.js while test.js contains something like this: 简而言之,我很想运行phantomjs test.js命令,而test.js包含以下内容:

var mocha = require("mocha"); // This one fails with "ReferenceError: Can't find variable: process"..... "/node_modules/mocha/index.js:2"

var page = require('webpage').create();

describe('Empty', function(){ // Obviously this one fails too with "ReferenceError: Can't find variable: describe"
  describe('tests are', function(){
    it('very nice', function(done){
      page.open("http://www.phantomjs.org/", function (status) {
          console.log('Loading a web page');
          console.log("Page is loaded");
          phantom.exit(); // Or, just call `done()`, doesn't matter right now
      });
    })
  })
})

Can you recognize the code pieces of the two? 你能认出两者的代码吗?

How can I make this work? 我该如何进行这项工作? I've read on some page that I could require() "mocha.js" of the browser version, but it doesn't seem to work very well. 我在某个页面上读到了我可能require()浏览器版本的“ mocha.js”,但它似乎不能很好地工作。

------------------More details ------------------------------------ - - - - - - - - - 更多细节 - - - - - - - - - - - - - - - ------

I've tried CasperJS but besides providing some really nice stuff, I miss Mocha's appealing test design ( server-side ). 我已经尝试过CasperJS,但是除了提供一些非常好的东西之外,我还想念Mocha吸引人的测试设计( 服务器端 )。

All the examples I've seen explain how to run your Mocha tests on the webpage (browser-side) (with a script tag), rather than the NodeJS test script. 我所看到的所有示例都说明了如何在网页上(浏览器端)(带有script标签)而不是NodeJS测试脚本运行Mocha测试。
I don't like it, I want to keep the test pure-console, in addition to the fact that this approach encourages me to add testing code to my webpages. 我不喜欢它,除了这种方法鼓励我将测试代码添加到网页之外,我还想保持测试纯控制台。

I've seen a few projects trying to ease that approach yet I'm not content with it - it seems like they've done 95% of the requirement by bringing Mocha and PhantomJS, but the last 5% are still undone, the stuff that really connect it together. 我看到一些项目试图简化这种方法,但我并不满意-看来他们通过引入Mocha和PhantomJS已经完成了95%的要求,但是最后5%的工作仍未完成,真正将其连接在一起。

You may want to run your tests still in a Node context ( node test.js ), but controlling phantomJS from node with something like the node-phantom library. 您可能希望仍在Node上下文( node test.js )中运行测试,但是要使用node-phantom库之类的东西从node控制phantomJS。

Something TJ Holowaychuk does in all of his libraries is this process.env.COV check (which I personally don't understand), but it's likely that PhantomJS won't support the process variable. TJ Holowaychuk在他所有的库中所做的事情就是这个process.env.COV检查 (我个人不理解),但是PhantomJS可能不支持该过程变量。

You might be able to trick Mocha by setting window.process and window.process.env to empty objects, though I'd guess you'll run into more PhantomJS/Node issues later on. 您可能可以通过将window.processwindow.process.env设置为空对象来欺骗Mocha,尽管我猜您稍后会遇到更多的PhantomJS / Node问题。

Running phantomjs via the bridge from node may not be the best option. 通过node的网桥运行phantomjs可能不是最佳选择。

There is phantomjs-nodeify module (I forked from http://github.com/jgonera ) that makes phantomjs environment more like node environment - it adds modules process, events, etc. to phantomjs. phantomjs-nodeify模块(我从http://github.com/jgonera分叉)使phantomjs环境更像节点环境-它向phantomjs添加模块进程,事件等。

There is webspecter framework that achieved exactly that, with old phantomjs and old mocha though. 有一个webspecter框架正是用旧的phantomjs和旧的摩卡实现了这一目标。

It seems possible to make mocha tests run in phantom 2.0. 进行摩卡测试似乎可以在phantom 2.0中运行。

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

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