-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
jamestalmage
merged 8 commits into
avajs:master
from
jamestalmage:use-ava-throws-helper
Apr 11, 2016
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f431c4
detect improper use of t.throws
jamestalmage dbed3d4
relative file path and colors
jamestalmage 53b38a8
protect against null/undefined in _setAssertError
jamestalmage 56c59ce
use babel-code-frame to do syntax highlighting on the Error
jamestalmage 3f6a01f
require `babel-code-frame` inline. It has a sizable dependency graph
jamestalmage 4a0bbd0
remove middle section of message. It is redundant given code-frame
jamestalmage b12b4d7
further tests and add documentation.
jamestalmage df79d3c
update readme.md
jamestalmage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 thet.throws()
argument expression such that it is computed outside oft.throws()
. Again, I don't know if it does, but if it did then you wouldn't be able to catch it.There was a problem hiding this comment.
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 likeava-throws-helper
.Also, we don't enhance
t.throws
withpower-assert
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right.
t.throws
is listed as NON_ENHANCED_PATTERNS here. power-assert doesn't touch its arguments.