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

Test function body in a reporter #1842

Open
iliakan opened this issue Aug 3, 2020 · 3 comments
Open

Test function body in a reporter #1842

iliakan opened this issue Aug 3, 2020 · 3 comments

Comments

@iliakan
Copy link

iliakan commented Aug 3, 2020

In Mocha, another test framework, reporters get the test body - function code.

That's the part of the result. As the spec is created, fn.toString() is also stored in it, and then passed to the reporter.

It's very handy - to get the test code in the report. So that if something goes wrong, HTML reporter shows the failed test code on click.

Can Jasmine be tuned to do the same, without much hacking?

@iliakan iliakan changed the title Test code in reporter Test function body in a reporter Aug 3, 2020
@iliakan
Copy link
Author

iliakan commented Aug 3, 2020

P.S. This is what I've done (approximately), is it ok?

let Spec = jasmineRequire.Spec;
jasmineRequire.Spec = function(j$) {
  let SpecOriginal = Spec(j$);
  return class Spec extends SpecOriginal {
    constructor(attrs) {
      super(attrs);
      this.result.body = (attrs.queueableFn && attrs.queueableFn.fn || '').toString(); 
    }
  }
}

@slackersoft
Copy link
Member

This is an interesting concept. I'm not sure I understand the circumstances in which it would be useful for debugging a test failure though. I'm especially thinking of situations around transpilers (e.g. TypeScript, babel, etc.) and what output would be useful here.

If the original spec is written in TypeScript, Jasmine won't have any way to get the "real" source of the function, just what the compiler output as base JavaScript. I'm not sure how useful that would be; unless of course that is the actual goal here, to be able to see what the code actually running looks like.

If this is added to Jasmine, we would also need to figure out whether (and how) that content would be exposed in the current reporters (both HTML and console), or if we just want to expose the functionality to 3rd party reporters.

@iliakan
Copy link
Author

iliakan commented Sep 3, 2020

If a test fails, HTML reporter becomes able to show the test code.

Reporters in another framework, Mocha, have access to function body, so I just ported the functionality to Jasmine, with the code above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants