nathanboktae / mocha-phantomjs

:coffee: :ghost: Run client-side mocha tests in the command line through phantomjs
MIT License
954 stars 112 forks source link

Failing tests with the mocha events API #118

Closed hoisie closed 10 years ago

hoisie commented 10 years ago

I've been trying to get mocha-phantomjs to work with mocha events, but I haven't been able to do so. I have a module that runs after each mocha test ends and checks if certain conditions are met. It's similar to the global leaks detector, and it's used to detect event handler memory leaks. I constructed a test case that may explain the issue better:

The HTML for this test is here:

<html>
  <head>
    <title>Tests Failing</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
  </head>
  <body>
    <div id="mocha"></div>
    <script src="../node_modules/mocha/mocha.js"></script>
    <script src="../node_modules/chai/chai.js"></script>
    <script>
      mocha.ui('bdd');
      mocha.reporter('html');
      expect = chai.expect;
    </script>
    <script src="lib/events.js"></script>
    <script>
      var runner;
      if (window.mochaPhantomJS) {
        runner = mochaPhantomJS.run();
      } else {
        runner = mocha.run();
      }
      // because reporters also bind to the 'test end' event when reporting their status,
      // we need to call 'runner.fail' to mark the test as failed before the runner
      // gets the test status. Here, we prepend our 'test end' handler to the beginning
      // of the list.
      runner.$events['test end'].unshift(function(test) {
        runner.fail(test, new Error("Failed via an event"));
      });
    </script>
  </body>
</html>

And events.js is here:

expect = (chai && chai.expect) || require('chai').expect;

describe('Event Tests', function() {
  it('Fails tests', function() {
    expect(1).to.be.ok;
  });
});

This test fails as expected in Chrome and phantomjs, but it passes in mocha-phantomjs.

nathanboktae commented 10 years ago

Thanks for the good bug report - repro'd it right away and used it for the unit test. Reference the git repo directly until we get a new npm package up.

hoisie commented 10 years ago

Wow, thanks for the quick fix! Mocha-phantomjs has been an incredible tool for us.