Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON reporter doesn't work (no output) #2433

Closed
sakovias opened this issue Aug 12, 2016 · 4 comments · Fixed by Urigo/tortilla#64
Closed

JSON reporter doesn't work (no output) #2433

sakovias opened this issue Aug 12, 2016 · 4 comments · Fixed by Urigo/tortilla#64
Labels
area: reporters involving a specific reporter status: accepting prs Mocha can use your help with this one! type: bug a defect, confirmed by a maintainer

Comments

@sakovias
Copy link

sakovias commented Aug 12, 2016

The issue occur to me on mocha v3.0.2 and node v5.8.0 on Mac OS X. Here's my test file

'use strict';
var app = require('./app');

describe('Converting strings.', function() {

  it('Can convert a string with one space', function() {
    var result = app.convertString('hello world');
    result.should.eql('hello-world');
  });

  it('Replaces two spaces with one dash', function() {
    app.convertString('hello  world').should.eql('hello-world');
  });
});

and the code I'm testing

function convertString(str) {
  return str.split(' ').join('-');
}

module.exports = {
  convertString: convertString
};

With json-stream reporter everything works fine:

$ mocha --require should --reporter json-stream test.js
["start",{"total":2}]
["pass",{"title":"Can convert a string with one space","fullTitle":"Converting strings. Can convert a string with one space","duration":2,"currentRetry":0}]
["fail",{"title":"Replaces two spaces with one dash","fullTitle":"Converting strings. Replaces two spaces with one dash","duration":0,"currentRetry":0,"err":"expected 'hello--world' to equal 'hello-world'","stack":"AssertionError: expected 'hello--world' to equal 'hello-world'\n    at Assertion.fail (node_modules/should/cjs/should.js:205:17)\n    at Assertion.Object.defineProperty.value (node_modules/should/cjs/should.js:277:19)\n    at Context.<anonymous> (test.js:12:46)"}]
["end",{"suites":1,"tests":2,"passes":1,"pending":0,"failures":1,"start":"2016-08-12T15:34:45.144Z","end":"2016-08-12T15:34:45.155Z","duration":11}]

but with the json reporter nothing comes out:

$ mocha --require should --reporter json test.js  # No output!

Am I doing something wrong?

@gurdiga
Copy link

gurdiga commented Aug 13, 2016

It looks like should.js has a circular reference in its assertion object, and this gets Mocha’s json reporter in trouble when it tries to serialize it. 😶

Mocha (by default) catches and collects the uncaught exceptions, but at the time when this one is thrown, the reporter’s output is already prepared, and so this particular error is lost. 😑

One way to fix this is I guess to watch and cut the circular references in the json reporter. Maybe there are better ways, I’m open to advice. 🤓

@sakovias
Copy link
Author

Good catch! Thank you for your work @gurdiga. I don't see a reason not to merge you fix 😄

That's exactly what I was noticing on my Linux distro (with slightly older mocha and node) where JSON reporter was blowing up on circular references. With newer mocha and node on Mac OS, the reporter was just going silent.

As a workaround on old Mocha folks can use json-stream reporter. It doesn't have this issue for some reason.

@boneskull
Copy link
Member

The ./lib/utils module has a method jsonStringify() which is circular-reference safe. Would accept PR to JSON reporter to use it.

@boneskull boneskull added type: bug a defect, confirmed by a maintainer area: reporters involving a specific reporter status: accepting prs Mocha can use your help with this one! labels Aug 23, 2016
@boneskull
Copy link
Member

as noted in #2559, I was wrong about the function (my bad)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: reporters involving a specific reporter status: accepting prs Mocha can use your help with this one! type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants