Skip to content
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

Less API using power-assert #46

Closed
wants to merge 13 commits into from
2 changes: 2 additions & 0 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,5 @@ x.regexTest = function (regex, contents, msg) {
x.ifError = x.error = function (err, msg) {
test(!err, create(err, 'Error', '!==', msg, x.ifError));
};

require('./enhance-assert')(x);
8 changes: 7 additions & 1 deletion lib/babel.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
'use strict';

var resolveFrom = require('resolve-from');
var createEspowerPlugin = require('babel-plugin-espower/create');

var hasGenerators = parseInt(process.version.slice(1), 10) > 0;

var options = {
only: /(test|test\-.+|test\/.+)\.js$/,
blacklist: hasGenerators ? ['regenerator'] : [],
optional: hasGenerators ? ['asyncToGenerator'] : []
optional: hasGenerators ? ['asyncToGenerator'] : [],
plugins: [
createEspowerPlugin(require('babel-core'), {
patterns: require('./enhance-assert').PATTERNS
})
]
};

try {
Expand Down
34 changes: 34 additions & 0 deletions lib/enhance-assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = enhanceAssert;

module.exports.PATTERNS = [
't.ok(value, [message])',
't.notOk(value, [message])',
't.true(value, [message])',
't.false(value, [message])',
't.is(value, expected, [message])',
't.not(value, expected, [message])',
't.same(value, expected, [message])',
't.notSame(value, expected, [message])',
't.regexTest(regex, contents, [message])'
];

function enhanceAssert(assert) {
var empower = require('empower');
var powerAssertFormatter = require('power-assert-formatter');
var powerAssertRenderers = require('power-assert-renderers');

empower(assert,
powerAssertFormatter({
renderers: [
powerAssertRenderers.AssertionRenderer,
powerAssertRenderers.SuccinctRenderer
]
}),
{
destructive: true,
modifyMessageOnRethrow: true,
saveContextOnRethrow: false,
patterns: module.exports.PATTERNS
}
);
}
5 changes: 5 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ Object.keys(assert).forEach(function (el) {
};
});

// Workaround for power-assert
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@twada Is there anything you can do in power-assert to make this unnecessary?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this workaround is necessary for now (_capt and _expr methods are needed at the top level).

To make them look less messy, I'll introduce small helper method like

empower.makeCapturable(Test.prototype);

(looking for a better name though)

// `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');
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,21 @@
"arr-flatten": "^1.0.1",
"ava-init": "^0.1.0",
"babel-core": "^5.8.23",
"babel-plugin-espower": "^1.0.0",
"bluebird": "^2.9.34",
"chalk": "^1.0.0",
"co": "^4.6.0",
"core-assert": "^0.1.0",
"empower": "^1.0.2",
"figures": "^1.4.0",
"fn-name": "^2.0.0",
"globby": "^3.0.1",
"is-generator": "^1.0.2",
"meow": "^3.3.0",
"object-assign": "^4.0.1",
"plur": "^2.0.0",
"power-assert-formatter": "^1.1.0",
"power-assert-renderers": "^0.1.0",
"pretty-ms": "^2.0.0",
"resolve-from": "^1.0.0",
"set-immediate-shim": "^1.0.1",
Expand Down
8 changes: 8 additions & 0 deletions test/fixture/power-assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import test from '../../';

test(t => {
const a = 'foo';

t.ok(a === 'bar');
t.end();
});
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -837,3 +837,16 @@ test('fail-fast mode', function (t) {
t.end();
});
});

test('power-assert support', function (t) {
t.plan(2);

execCli('fixture/power-assert.js', function (err, stdout, stderr) {
t.ok(err);

// t.ok(a === 'bar')
// |
// "foo"
t.true((/t\.ok\(a === 'bar'\)\s*\n\s+\|\s*\n\s+"foo"/m).test(stderr));
});
});