From aca41744600b5a7c1a76145a8130c6af07b066b9 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 7 Jun 2018 16:30:43 -0700 Subject: [PATCH] lib/test: Add diagnostic Test.context for Mochawesome 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 --- lib/test.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/test.js b/lib/test.js index 694285f..97dcd4b 100644 --- a/lib/test.js +++ b/lib/test.js @@ -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, } } }