npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mocha-phantomjs-reporter

v0.0.6

Published

a mocha reporter which we can use in phantomjs

Downloads

5

Readme

Mocha-Phantomjs-Reporter

如果想在phantomjs中使用mocha,比较方便的我们可以使用mocha-phantomjs,用例代码如下:

<html>
  <head>
    <meta charset="utf-8">
    <!-- encoding must be set for mocha's special characters to render properly -->
    <link rel="stylesheet" href="mocha.css" />
  </head>
  <body>
    <div id="mocha"></div>
    <script src="mocha.js"></script>
    <script src="chai.js"></script>
    <script>
      mocha.ui('bdd')
      expect = chai.expect
    </script>
    <script src="src/mycode.js"></script>
    <script src="test/mycode.js"></script>
    <script>
      mocha.run()
    </script>
  </body>
</html>

他的优势在于其方便的集成,使用简单,支持绝大部分reporter,但是依然满足不了我目前的需求;使用phantomjs,我们最大的目的是进行自动化测试,对于已有的静态页面或者服务器页面,我们想对其进行自动化测试,最好的办法是测试单独隔离开,不和他们有任何耦合的地方,这样才是最理想的方案。但是由上面的例子我们可以发现,mocha及相关设置必须事先写入页面当中,当然使用gulp可以完成这类任务,但是因为测试而去改变已有的模板,这是没法容忍的。我们能不能在测试时,动态注入mocha了?

翻看phantomjs的文档,最终找到了解决方案,本项目采用了phantomjs的injectJs,以及onCallback配合沙盒里面的window.callPhantom(这种通信方式类似html5中的postMessage),最终完美的将mocha的测试信息返回到控制台!

...
page.onCallback = function(data) {
  if(!data){
    phantom.exit();
  }
  data = data.map(function(n){
    if(/^\{(.+)\|(.+)\}$/.test(n)){
      return RegExp.$1[RegExp.$2]
    }else{
      return n;
    }
  })
  console.log.apply(console, data);
}
page.open('index.html', function(status){
  if(status){
    page.injectJs('../node_modules/mocha/mocha.js');
    page.injectJs('../index.js'); //reporter
    page.injectJs('../node_modules/chai/chai.js');
    page.injectJs('index.js'); //test
    page.evaluateJavaScript('function(){ mocha.run() }');
  }else{
    console.error('open index.html failed');
  }
})

从此测试与html模板成功分离。