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

detect improper use of t.throws #742

Merged
merged 8 commits into from
Apr 11, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/caching-precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ CachingPrecompiler.prototype._init = function () {
];

var transformRuntime = require('babel-plugin-transform-runtime');
var throwsHelper = require('babel-plugin-ava-throws-helper');
var rewriteBabelPaths = this._createRewritePlugin();
var powerAssert = this._createEspowerPlugin();

this.defaultPlugins = [
powerAssert,
throwsHelper,
rewriteBabelPaths,
transformRuntime
];
Expand Down
18 changes: 18 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ Test.prototype._assert = function (promise) {
};

Test.prototype._setAssertError = function (err) {
var data = err._avaThrowsHelperData;
Copy link
Member

Choose a reason for hiding this comment

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

Probably need to guard against err being null or undefined here?

if (data) {
console.error(
[
'Improper usage of t.throws detected at %s (%d:%d).',
'You should wrap the following expression in a function:',
' %s',
'Like this:',
' function() {\n %s\n }',
Copy link
Member

Choose a reason for hiding this comment

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

Promote arrow functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I considered that.

But if the user is green enough that that they don't fully understand how throw works, I was concerned about piling on another abstraction they need to understand.

Copy link
Member

Choose a reason for hiding this comment

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

Yea fair enough.

'See https://github.com/sindresorhus/ava#throwsfunctionpromise-error-message for more details.'
].join('\n\n'),
data.filename,
data.line,
data.column,
data.source,
data.source
);
}
if (this.assertError !== undefined) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"arrify": "^1.0.0",
"ava-init": "^0.1.0",
"babel-core": "^6.3.21",
"babel-plugin-ava-throws-helper": "0.0.4",
"babel-plugin-detective": "^1.0.2",
"babel-plugin-espower": "^2.1.0",
"babel-plugin-transform-runtime": "^6.3.13",
Expand Down
3 changes: 2 additions & 1 deletion test/caching-precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var uniqueTempDir = require('unique-temp-dir');
var sinon = require('sinon');
var babel = require('babel-core');
var transformRuntime = require('babel-plugin-transform-runtime');
var throwsHelper = require('babel-plugin-ava-throws-helper');
var fromMapFileSource = require('convert-source-map').fromMapFileSource;

var CachingPrecompiler = require('../lib/caching-precompiler');
Expand Down Expand Up @@ -145,7 +146,7 @@ test('uses babelConfig for babel options when babelConfig is an object', functio
t.true('inputSourceMap' in options);
t.false(options.babelrc);
t.same(options.presets, ['stage-2', 'es2015']);
t.same(options.plugins, [customPlugin, powerAssert, rewrite, transformRuntime]);
t.same(options.plugins, [customPlugin, powerAssert, throwsHelper, rewrite, transformRuntime]);
Copy link
Member

Choose a reason for hiding this comment

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

Without checking the power-assert transformations, are there cases the Babel plugin can no longer catch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shouldn't be. The babel-throws-helper wrapper is totally transparent to the code it wraps.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, but power-assert might rewrite the t.throws() argument expression such that it is computed outside of t.throws(). Again, I don't know if it does, but if it did then you wouldn't be able to catch it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh - Understood. Still not a problem though. power-assert wraps inline, just like ava-throws-helper.

t.true(_rec._expr(_rec._capt(_rec._capt(a, 'arguments/0/left') === 'bar', 'arguments/0'), {
    content: 't.true(a === \'bar\')',
    filepath: 'test/fixture/power-assert.js',
    line: 6
}));

Also, we don't enhance t.throws with power-assert.

Copy link
Contributor

Choose a reason for hiding this comment

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

we don't enhance t.throws with power-assert.

Right. t.throws is listed as NON_ENHANCED_PATTERNS here. power-assert doesn't touch its arguments.

t.end();
});

Expand Down
8 changes: 8 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ test('throwing a named function will report the to the console', function (t) {
});
});

test('improper use of t.throws will be reported to the console', function (t) {
execCli('fixture/improper-t-throws.js', function (err, stdout, stderr) {
t.ok(err);
t.match(stderr, /Improper usage of t\.throws detected at .*improper-t-throws.js \(4:10\)/);
t.end();
});
});

test('babel require hook only applies to the test file', function (t) {
t.plan(3);

Expand Down
9 changes: 9 additions & 0 deletions test/fixture/improper-t-throws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import test from '../../';

test(t => {
t.throws(throwSync());
});

function throwSync() {
throw new Error('should be detected');
}