Alternate "slim" version of empower.
jamestalmage opened this issue · 6 comments
Poking through the code, I think there is room for a "slim" version of empower.
If my understanding is correct, then the following:
saveContextOnRethrow: true,
modifyMessageOnRethrow: false
Does not require a formatter at all. It just attaches the context and re-throws a copy.
In the "slim" version I am picturing, it would only take a patterns argument.
Also, instead of re-throwing an error (which messes with the existing stack trace needlessly). You could just use serialize-error
attach the context, and throw.
We would format and print the message on the main thread using powerAssertContext
.
Is what I am saying making sense?
Let me explain a history behind the code.
Also, instead of re-throwing an error (which messes with the existing stack trace needlessly). You could just use serialize-error attach the context, and throw.
I've implemented the same strategy, but some of old browsers (including PhantomJS) have strange behavior. When I rethrow Errors after attaching some props, the props disappear.
try {
try {
...
} catch (e) {
e.foo = 'bar';
throw e;
}
} catch (e) {
assert(e.foo === 'bar'); // Error on old PhantomJS
}
refactor(empower): always re-instantiate AssertionError on rethrow · power-assert-js/empower@997da24
However, I found that the attached props are remaining on current PhantomJS.
So I can just attach and rethrow the original Error now.
It's a delightful discovery. Thanks!
@twada - serialize-error
actually does not create an error. It creates a plain old javascript object with deep copies of the Errors properties. An Error object does not survive JSON.stringify()
very well. Especially assertion errors that have circular references.
Check the unit tests for all it does.
For use outside AVA, throwing non-errors is probably a bad idea. Could you maybe split most of the functionality into another module? empower-core
or something. It should take a copyAssertion
callback. empower
can continue to copy the Assertion the way you've been doing it here, and we can make a custom version that uses serialize-error
to make the copy.
Could you maybe split most of the functionality into another module? empower-core or something. It should take a copyAssertion callback.
Yeah I'm working on that !
Update: Now in a beta stage
Close this issue since twada/empower-core is going on.