Skip to content

Commit

Permalink
detect improper use of t.throws
Browse files Browse the repository at this point in the history
Protects against a common misuse of t.throws (Like that seen in #739).

This required the creation of a custom babel plugin.

https://github.com/jamestalmage/babel-plugin-ava-throws-helper
  • Loading branch information
jamestalmage committed Apr 9, 2016
1 parent 2295118 commit 1f431c4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
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;
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 }',
'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]);
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');
}

0 comments on commit 1f431c4

Please sign in to comment.