Skip to content

Commit 1f431c4

Browse files
committed
detect improper use of t.throws
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
1 parent 2295118 commit 1f431c4

File tree

6 files changed

+40
-1
lines changed

6 files changed

+40
-1
lines changed

lib/caching-precompiler.js

+2
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@ CachingPrecompiler.prototype._init = function () {
5252
];
5353

5454
var transformRuntime = require('babel-plugin-transform-runtime');
55+
var throwsHelper = require('babel-plugin-ava-throws-helper');
5556
var rewriteBabelPaths = this._createRewritePlugin();
5657
var powerAssert = this._createEspowerPlugin();
5758

5859
this.defaultPlugins = [
5960
powerAssert,
61+
throwsHelper,
6062
rewriteBabelPaths,
6163
transformRuntime
6264
];

lib/test.js

+18
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ Test.prototype._assert = function (promise) {
6868
};
6969

7070
Test.prototype._setAssertError = function (err) {
71+
var data = err._avaThrowsHelperData;
72+
if (data) {
73+
console.error(
74+
[
75+
'Improper usage of t.throws detected at %s (%d:%d).',
76+
'You should wrap the following expression in a function:',
77+
' %s',
78+
'Like this:',
79+
' function() {\n %s\n }',
80+
'See https://github.com/sindresorhus/ava#throwsfunctionpromise-error-message for more details.'
81+
].join('\n\n'),
82+
data.filename,
83+
data.line,
84+
data.column,
85+
data.source,
86+
data.source
87+
);
88+
}
7189
if (this.assertError !== undefined) {
7290
return;
7391
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"arrify": "^1.0.0",
8080
"ava-init": "^0.1.0",
8181
"babel-core": "^6.3.21",
82+
"babel-plugin-ava-throws-helper": "0.0.4",
8283
"babel-plugin-detective": "^1.0.2",
8384
"babel-plugin-espower": "^2.1.0",
8485
"babel-plugin-transform-runtime": "^6.3.13",

test/caching-precompiler.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var uniqueTempDir = require('unique-temp-dir');
66
var sinon = require('sinon');
77
var babel = require('babel-core');
88
var transformRuntime = require('babel-plugin-transform-runtime');
9+
var throwsHelper = require('babel-plugin-ava-throws-helper');
910
var fromMapFileSource = require('convert-source-map').fromMapFileSource;
1011

1112
var CachingPrecompiler = require('../lib/caching-precompiler');
@@ -145,7 +146,7 @@ test('uses babelConfig for babel options when babelConfig is an object', functio
145146
t.true('inputSourceMap' in options);
146147
t.false(options.babelrc);
147148
t.same(options.presets, ['stage-2', 'es2015']);
148-
t.same(options.plugins, [customPlugin, powerAssert, rewrite, transformRuntime]);
149+
t.same(options.plugins, [customPlugin, powerAssert, throwsHelper, rewrite, transformRuntime]);
149150
t.end();
150151
});
151152

test/cli.js

+8
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ test('throwing a named function will report the to the console', function (t) {
9696
});
9797
});
9898

99+
test('improper use of t.throws will be reported to the console', function (t) {
100+
execCli('fixture/improper-t-throws.js', function (err, stdout, stderr) {
101+
t.ok(err);
102+
t.match(stderr, /Improper usage of t\.throws detected at .*improper-t-throws.js \(4:10\)/);
103+
t.end();
104+
});
105+
});
106+
99107
test('babel require hook only applies to the test file', function (t) {
100108
t.plan(3);
101109

test/fixture/improper-t-throws.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import test from '../../';
2+
3+
test(t => {
4+
t.throws(throwSync());
5+
});
6+
7+
function throwSync() {
8+
throw new Error('should be detected');
9+
}

0 commit comments

Comments
 (0)