Skip to content

Commit

Permalink
fix: recreate AssertionError under Node v8 if message is modified
Browse files Browse the repository at this point in the history
when `modifyMessageOnRethrow` option is truthy.
since Node v8 (and v7) does not update message on rethrow.

refs power-assert-js/power-assert#85
  • Loading branch information
twada committed Jun 3, 2017
1 parent 06f8851 commit 85d4841
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -33,15 +34,26 @@ 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) {
throw e;
}
// 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;
Expand Down
14 changes: 7 additions & 7 deletions test/buster_assertions_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
'assert(falsy)',
'[{"value":0,"espath":"arguments/0"}]'
].join('\n'));
baseAssert.equal(e.name, 'AssertionError');
baseAssert(/^AssertionError/.test(e.name));
}
});

Expand All @@ -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));
}
});
});
Expand All @@ -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));
}
});

Expand All @@ -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));
}
});

Expand All @@ -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));
}
});
});
Expand All @@ -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));
}
});

Expand All @@ -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));
}
});
});
Expand Down
22 changes: 11 additions & 11 deletions test/empower_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});

Expand Down Expand Up @@ -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));
}
});

Expand Down Expand Up @@ -140,7 +140,7 @@ test('assertion with optional message argument. ' + JSON.stringify(option), func
]
});
}
baseAssert.equal(e.name, 'AssertionError');
baseAssert(/^AssertionError/.test(e.name));
}
});

Expand Down Expand Up @@ -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));
}
});

Expand Down Expand Up @@ -211,7 +211,7 @@ suite(JSON.stringify(option) + ' assertion method with one argument', function (
]
});
}
baseAssert.equal(e.name, 'AssertionError');
baseAssert(/^AssertionError/.test(e.name));
}
});
});
Expand Down Expand Up @@ -250,7 +250,7 @@ suite(JSON.stringify(option) + ' assertion method with two arguments', function
]
});
}
baseAssert.equal(e.name, 'AssertionError');
baseAssert(/^AssertionError/.test(e.name));
}
});

Expand Down Expand Up @@ -282,7 +282,7 @@ suite(JSON.stringify(option) + ' assertion method with two arguments', function
]
});
}
baseAssert.equal(e.name, 'AssertionError');
baseAssert(/^AssertionError/.test(e.name));
}
});

Expand Down Expand Up @@ -314,7 +314,7 @@ suite(JSON.stringify(option) + ' assertion method with two arguments', function
]
});
}
baseAssert.equal(e.name, 'AssertionError');
baseAssert(/^AssertionError/.test(e.name));
}
});
});
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
}
});

Expand Down
12 changes: 6 additions & 6 deletions test/not_espowered_case_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 85d4841

Please sign in to comment.