michaelleeallen / mocha-junit-reporter

A JUnit XML reporter for mocha.
MIT License
144 stars 153 forks source link

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

Open Flarna opened 2 years ago

Flarna commented 2 years ago

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().

calipee commented 2 years ago

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.