michaelleeallen / mocha-junit-reporter

A JUnit XML reporter for mocha.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No report generated and failures are ignored if Date.now is stubed to return an invalid value

Flarna opened this issue · comments

If Date.now is stubbed to return an invalid value an exception happens in reporter causing that no report is generated and exit code from mocha is 0 even a test failed.

Reproducer:

const assert = require("assert");
const sinon = require("sinon");

describe("suite", () => {
  const stub = sinon.stub(Date, "now");
  stub.returns(9000000000000000);

  it("test", () => {
    assert.strictEqual(1, 2);
  });
});

I know that stubbing Date.now like this is not the best idea but I think an exception in reporter should not result in swallowing failed tests.

The exception happens at line _suiteAttr.timestamp = new Date(_suiteAttr.timestamp).toISOString().slice(0, -5); in MochaJUnitReporter.prototype.getXml() because _suiteAttr.timestamp is 9000000000000000 which is to high for new Date().

I'm experiencing a similar problem when stubbing Date.now(), but in my case the process just doesn't finish and no report is generated.

In my Case adding Date.now.restore(); before the test ends solved the issue.