-
Notifications
You must be signed in to change notification settings - Fork 1.4k
use empower-core for better assertion tracking and performance
#330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ var observableToPromise = require('observable-to-promise'); | |
| var isPromise = require('is-promise'); | ||
| var isObservable = require('is-observable'); | ||
| var assert = require('./assert'); | ||
| var enhanceAssert = require('./enhance-assert'); | ||
| var globals = require('./globals'); | ||
|
|
||
| function Test(title, fn) { | ||
|
|
@@ -61,11 +62,6 @@ Test.prototype._setAssertError = function (err) { | |
| this.assertError = err; | ||
| }; | ||
|
|
||
| // Workaround for power-assert | ||
| // `t` must be capturable for decorated assert output | ||
| Test.prototype._capt = assert._capt; | ||
| Test.prototype._expr = assert._expr; | ||
|
|
||
| Test.prototype.plan = function (count) { | ||
| if (typeof count !== 'number') { | ||
| throw new TypeError('Expected a number'); | ||
|
|
@@ -208,9 +204,7 @@ Test.prototype._publicApi = function () { | |
| [ | ||
| 'assertCount', | ||
| 'title', | ||
| 'end', | ||
| '_capt', | ||
| '_expr' | ||
| 'end' | ||
| ] | ||
| .forEach(function (name) { | ||
| Object.defineProperty(api, name, { | ||
|
|
@@ -239,31 +233,46 @@ Test.prototype._publicApi = function () { | |
| self._assert(); | ||
| } | ||
|
|
||
| function onAssertionEvent(event) { | ||
| if (event.assertionThrew) { | ||
| event.error.powerAssertContext = event.powerAssertContext; | ||
| event.error.originalMessage = event.originalMessage; | ||
| self._setAssertError(event.error); | ||
| self._assert(); | ||
| return null; | ||
| } | ||
|
|
||
| var fn = observableToPromise(event.returnValue); | ||
|
|
||
| if (isPromise(fn)) { | ||
| return Promise.resolve(fn) | ||
| .catch(function (err) { | ||
| err.originalMessage = event.originalMessage; | ||
| self._setAssertError(err); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will errors here also have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given your answer below I understood what is Here, if assertion returns promise and that promise rejects, we just Or (correct me if I'm wrong) if "in that promise" there is a failed assertion, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure - it might. I need to test that. My initial thought is no, sense the only assertions that return promises is Definitely worth me double checking though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, then we are good. Those will never have |
||
| }) | ||
| .finally(function () { | ||
| self._assert(); | ||
| }); | ||
| } | ||
|
|
||
| self._assert(); | ||
| return null; | ||
| } | ||
|
|
||
| var enhanced = enhanceAssert({ | ||
| assert: assert, | ||
| onSuccess: onAssertionEvent, | ||
| onError: onAssertionEvent | ||
| }); | ||
|
|
||
| // Patched assert methods: increase assert count and store errors. | ||
| Object.keys(assert).forEach(function (el) { | ||
| api.skip[el] = skipFn; | ||
| api[el] = function () { | ||
| try { | ||
| var fn = assert[el].apply(assert, arguments); | ||
|
|
||
| fn = observableToPromise(fn); | ||
|
|
||
| if (isPromise(fn)) { | ||
| return Promise.resolve(fn) | ||
| .catch(function (err) { | ||
| self._setAssertError(err); | ||
| }) | ||
| .finally(function () { | ||
| self._assert(); | ||
| }); | ||
| } | ||
| } catch (err) { | ||
| self._setAssertError(err); | ||
| } | ||
|
|
||
| self._assert(); | ||
| }; | ||
| api[el] = enhanced[el].bind(enhanced); | ||
| }); | ||
|
|
||
| api._capt = enhanced._capt.bind(enhanced); | ||
| api._expr = enhanced._expr.bind(enhanced); | ||
|
|
||
| return api; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| import test from '../../'; | ||
|
|
||
| test(t => { | ||
| test.serial(t => { | ||
| const a = 'foo'; | ||
|
|
||
| t.ok(a === 'bar'); | ||
| }); | ||
|
|
||
| test.serial(t => { | ||
| const a = 'bar'; | ||
|
|
||
| t.ok(a === 'foo', 'with message'); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The non-enhanced patterns are a new feature we are adding to power-assert. There will be no source code transform, but we do wrap the function and automatically extract the optional message parameter for you and pass it as
event.originalMessage. It will beundefinedif the user does not pass it in.