Skip to content

Commit

Permalink
lib/test: Add diagnostic Test.context for Mochawesome
Browse files Browse the repository at this point in the history
This is, as far as I know, Mochawesome-specific, which makes it a bit
of a hack.  The Mochawesome side of this is
adamgruber/mochawesome@65e90621 (implement addContext method for
adding report context to tests, 2016-12-20,
adamgruber/mochawesome#106).  Ideally we'd want a way to trigger this
as part of requesting Mochawesome as the reporter, but the addition
seems harmless enough so I'm just including it every time.

In the diag.source case, we might want to expose diagnostics besides
.fn.  But we surely don't want to expose the test source via both .fn
and .context.  For this commit, I've left the diag.source case alone,
but in future work we might want something closer to :

  if (result.diag) {
    var context = Object.assign({}, result.diag)
    if (context.source) {
      var source = context.source
      delete context.source
      this.fn = {
        toString: function () {
          return 'function(){' + source + '\n}'
        }
      }
    }
    if (Object.keys(context).length) {
      this.context = {
        title: 'diagnostic',
        value: context,
      }
    }
  }

I haven't done that here, because Object.assign is not supported on
Internet Exporer, Android webview, or Opera for Android [1], despite
being part of ECMAScript 2015 [2].  Object.keys seems to be supported
everywhere [3].  We may only care about Node for this package, but I'm
being conservative for this commit.

[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Browser_compatibility
[2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Specifications
[3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Browser_compatibility
  • Loading branch information
wking committed Jun 8, 2018
1 parent 8b73357 commit aca4174
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ function Test (result, suite) {
} else {
this.state = 'failed'
}
if (result.diag && result.diag.source) {
var source = result.diag.source
this.fn = {
toString: function () {
return 'function(){' + source + '\n}'
if (result.diag) {
if (result.diag.source) {
var source = result.diag.source
this.fn = {
toString: function () {
return 'function(){' + source + '\n}'
}
}
} else {
this.context = {
title: 'diagnostic',
value: result.diag,
}
}
}
Expand Down

0 comments on commit aca4174

Please sign in to comment.