-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
detect improper use of t.throws (#742)
* 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 * relative file path and colors * protect against null/undefined in _setAssertError * use babel-code-frame to do syntax highlighting on the Error * require `babel-code-frame` inline. It has a sizable dependency graph * remove middle section of message. It is redundant given code-frame * further tests and add documentation. * update readme.md
- Loading branch information
1 parent
195390e
commit 3201b1b
Showing
14 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var chalk = require('chalk'); | ||
var globals = require('./globals'); | ||
|
||
module.exports = function throwsHelper(error) { | ||
if (!error || !error._avaThrowsHelperData) { | ||
return; | ||
} | ||
var data = error._avaThrowsHelperData; | ||
var codeFrame = require('babel-code-frame'); | ||
var frame = ''; | ||
try { | ||
var rawLines = fs.readFileSync(data.filename, 'utf8'); | ||
frame = codeFrame(rawLines, data.line, data.column, {highlightCode: true}); | ||
} catch (e) { | ||
console.warn(e); | ||
console.warn(e); | ||
} | ||
console.error( | ||
[ | ||
'Improper usage of t.throws detected at ' + chalk.bold.yellow('%s (%d:%d)') + ':', | ||
frame, | ||
'The first argument to t.throws should be wrapped in a function:', | ||
chalk.cyan(' t.throws(function() {\n %s\n })'), | ||
'Visit the following URL for more details:', | ||
' ' + chalk.blue.underline('https://github.com/sindresorhus/ava#throwsfunctionpromise-error-message') | ||
].join('\n\n'), | ||
path.relative(globals.options.baseDir, data.filename), | ||
data.line, | ||
data.column, | ||
data.source | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import test from '../../'; | ||
|
||
test.cb(t => { | ||
setTimeout(() => { | ||
t.throws(throwSync()); | ||
}); | ||
}); | ||
|
||
function throwSync() { | ||
throw new Error('should be detected'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import test from '../../'; | ||
|
||
test(t => { | ||
return Promise.resolve().then(() => { | ||
t.throws(throwSync()); | ||
}); | ||
}); | ||
|
||
function throwSync() { | ||
throw new Error('should be detected'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import test from '../../'; | ||
|
||
test.cb(t => { | ||
Promise.resolve().then(() => { | ||
t.throws(throwSync()); | ||
}); | ||
|
||
setTimeout(t.end, 20); | ||
}); | ||
|
||
function throwSync() { | ||
throw new Error('should be detected'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
Why is babel-code-frame both a dependency and a devDependency?