From 85d4841dfcacc77a12689a4295054c0191b1d3df Mon Sep 17 00:00:00 2001 From: Takuto Wada Date: Sat, 3 Jun 2017 15:47:57 +0900 Subject: [PATCH] fix: recreate AssertionError under Node v8 if message is modified when `modifyMessageOnRethrow` option is truthy. since Node v8 (and v7) does not update message on rethrow. refs https://github.com/power-assert-js/power-assert/issues/85 --- index.js | 16 ++++++++++++++-- test/buster_assertions_test.js | 14 +++++++------- test/empower_test.js | 22 +++++++++++----------- test/not_espowered_case_test.js | 12 ++++++------ 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/index.js b/index.js index 6b4ea05..7f4c124 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,7 @@ var defaultOptions = require('./lib/default-options'); var capturable = require('./lib/capturable'); var assign = require('core-js/library/fn/object/assign'); var define = require('./lib/define-properties'); +var assert = require('assert'); /** * Enhance Power Assert feature to assert function/object. @@ -33,7 +34,7 @@ function empower (assert, formatter, options) { }, onError: function (errorEvent) { var e = errorEvent.error; - if (e.name !== 'AssertionError') { + if (!/^AssertionError/.test(e.name)) { throw e; } if (!errorEvent.powerAssertContext) { @@ -41,7 +42,18 @@ function empower (assert, formatter, options) { } // console.log(JSON.stringify(errorEvent, null, 2)); if (config.modifyMessageOnRethrow) { - e.message = buildPowerAssertText(formatter, errorEvent.originalMessage, errorEvent.powerAssertContext); + var poweredMessage = buildPowerAssertText(formatter, errorEvent.originalMessage, errorEvent.powerAssertContext); + if (e.code === 'ERR_ASSERTION') { + e = new assert.AssertionError({ + message: poweredMessage, + actual: e.actual, + expected: e.expected, + operator: e.operator, + stackStartFunction: e.stackStartFunction + }); + } else { + e.message = poweredMessage; + } } if (config.saveContextOnRethrow) { e.powerAssertContext = errorEvent.powerAssertContext; diff --git a/test/buster_assertions_test.js b/test/buster_assertions_test.js index 9902685..5bb0333 100644 --- a/test/buster_assertions_test.js +++ b/test/buster_assertions_test.js @@ -66,7 +66,7 @@ 'assert(falsy)', '[{"value":0,"espath":"arguments/0"}]' ].join('\n')); - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); @@ -83,7 +83,7 @@ 'assert.isNull(falsy)', '[{"value":0,"espath":"arguments/0"}]: Expected 0 to be null' ].join('\n')); - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); }); @@ -101,7 +101,7 @@ 'assert.same(foo, bar)', '[{"value":"foo","espath":"arguments/0"},{"value":"bar","espath":"arguments/1"}]: foo expected to be the same object as bar' ].join('\n')); - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); @@ -116,7 +116,7 @@ 'assert.same("foo", bar)', '[{"value":"bar","espath":"arguments/1"}]: foo expected to be the same object as bar' ].join('\n')); - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); @@ -131,7 +131,7 @@ 'assert.same(foo, "bar")', '[{"value":"foo","espath":"arguments/0"}]: foo expected to be the same object as bar' ].join('\n')); - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); }); @@ -149,7 +149,7 @@ 'assert.near(actualVal, expectedVal, delta)', '[{"value":10.6,"espath":"arguments/0"},{"value":10,"espath":"arguments/1"},{"value":0.5,"espath":"arguments/2"}]: Expected 10.6 to be equal to 10 +/- 0.5' ].join('\n')); - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); @@ -164,7 +164,7 @@ 'assert.near(actualVal, expectedVal, delta, messageStr)', '[{"value":10.6,"espath":"arguments/0"},{"value":10,"espath":"arguments/1"},{"value":0.5,"espath":"arguments/2"}]: Expected 10.6 to be equal to 10 +/- 0.5' ].join('\n')); - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); }); diff --git a/test/empower_test.js b/test/empower_test.js index 8484eb5..7e1d9df 100644 --- a/test/empower_test.js +++ b/test/empower_test.js @@ -68,7 +68,7 @@ test('default options behavior', function () { 'assert(falsy)', '[{"value":0,"espath":"arguments/0"}]' ].join('\n')); - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); @@ -105,7 +105,7 @@ test('Bug reproduction. should not fail if argument is null Literal. ' + JSON.st ] }); } - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); @@ -140,7 +140,7 @@ test('assertion with optional message argument. ' + JSON.stringify(option), func ] }); } - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); @@ -175,7 +175,7 @@ test(JSON.stringify(option) + ' empowered function also acts like an assert func ] }); } - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); @@ -211,7 +211,7 @@ suite(JSON.stringify(option) + ' assertion method with one argument', function ( ] }); } - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); }); @@ -250,7 +250,7 @@ suite(JSON.stringify(option) + ' assertion method with two arguments', function ] }); } - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); @@ -282,7 +282,7 @@ suite(JSON.stringify(option) + ' assertion method with two arguments', function ] }); } - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); @@ -314,7 +314,7 @@ suite(JSON.stringify(option) + ' assertion method with two arguments', function ] }); } - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); }); @@ -355,7 +355,7 @@ suite(JSON.stringify(option) + ' yield for assertion inside generator', function ] }); } - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } catch (e) { return done (e); } @@ -418,7 +418,7 @@ suite(JSON.stringify(option) + ' await assertion inside async async function', f ] }); } - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } catch (e) { return done (e); } @@ -486,7 +486,7 @@ test('the case when assertion function call is not listed in patterns (even if m baseAssert.ok(false, 'AssertionError should be thrown'); } catch (e) { baseAssert.equal(e.message, '0 == true', 'should not be empowered'); - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); } }); diff --git a/test/not_espowered_case_test.js b/test/not_espowered_case_test.js index 8e1e734..3c1ff17 100644 --- a/test/not_espowered_case_test.js +++ b/test/not_espowered_case_test.js @@ -27,7 +27,7 @@ test(JSON.stringify(option) + ' argument is null Literal.', function () { eval('assert.equal(foo, null);'); assert.ok(false, 'AssertionError should be thrown'); } catch (e) { - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); baseAssert((e.message === '\'foo\' == null' || e.message === '\"foo\" == null')); baseAssert(e.powerAssertContext === undefined); } @@ -40,7 +40,7 @@ test(JSON.stringify(option) + ' empowered function also acts like an assert func eval('assert(falsy);'); assert.ok(false, 'AssertionError should be thrown'); } catch (e) { - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); baseAssert.equal(e.message, '0 == true'); baseAssert(e.powerAssertContext === undefined); } @@ -54,7 +54,7 @@ suite(JSON.stringify(option) + ' assertion method with one argument', function ( eval('assert.ok(falsy);'); assert.ok(false, 'AssertionError should be thrown'); } catch (e) { - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); baseAssert.equal(e.message, '0 == true'); baseAssert(e.powerAssertContext === undefined); } @@ -69,7 +69,7 @@ suite(JSON.stringify(option) + ' assertion method with two arguments', function eval('assert.equal(foo, bar);'); assert.ok(false, 'AssertionError should be thrown'); } catch (e) { - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); baseAssert((e.message === '\'foo\' == \'bar\'' || e.message === '\"foo\" == \"bar\"')); baseAssert(e.powerAssertContext === undefined); } @@ -81,7 +81,7 @@ suite(JSON.stringify(option) + ' assertion method with two arguments', function eval('assert.equal("foo", bar);'); assert.ok(false, 'AssertionError should be thrown'); } catch (e) { - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); baseAssert((e.message === '\'foo\' == \'bar\'' || e.message === '\"foo\" == \"bar\"')); baseAssert(e.powerAssertContext === undefined); } @@ -93,7 +93,7 @@ suite(JSON.stringify(option) + ' assertion method with two arguments', function eval('assert.equal(foo, "bar");'); assert.ok(false, 'AssertionError should be thrown'); } catch (e) { - baseAssert.equal(e.name, 'AssertionError'); + baseAssert(/^AssertionError/.test(e.name)); baseAssert((e.message === '\'foo\' == \'bar\'' || e.message === '\"foo\" == \"bar\"')); baseAssert(e.powerAssertContext === undefined); }