Skip to content

Better throws helper #1315

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

Merged
merged 5 commits into from
Mar 26, 2017
Merged

Better throws helper #1315

merged 5 commits into from
Mar 26, 2017

Conversation

novemberborn
Copy link
Member

@novemberborn novemberborn commented Mar 20, 2017

Integrate with avajs/babel-plugin-throws-helper#8. Again lead with some minor refactoring, though the repositioning of the error message is notable.

New output when improper usage of t.throws() is detected:

❯ node cli.js test/fixture/improper-t-throws/caught-and-leaked.js

  1 failed

  [anonymous]
  /Users/mark/GitHub/avajs/ava/test/fixture/improper-t-throws/caught-and-leaked.js:5

   4:   try {
   5:     t.throws(throwSync());
   6:   } catch (err) {

  Improper usage of `t.throws()` detected

  The following error was thrown, possibly before `t.throws()` could be called:

    [Error: should be detected]

  Try wrapping the first argument to `t.throws()` in a function:

    t.throws(() => { /* your code here */ })

  Visit the following URL for more details:

    https://github.com/avajs/ava#throwsfunctionpromise-error-message

  throwSync (test/fixture/improper-t-throws/caught-and-leaked.js:14:8)
  Test.t [as fn] (test/fixture/improper-t-throws/caught-and-leaked.js:5:12)

Copy link
Member

@sindresorhus sindresorhus left a comment

Choose a reason for hiding this comment

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

Looks great! I really like how extensive the t.throws detection is now.

lib/assert.js Outdated
@@ -17,6 +17,7 @@ class AssertionError extends Error {

this.assertion = opts.assertion;
this.fixedSource = opts.fixedSource;
this.improperUsage = opts.improperUsage || false;
Copy link
Member

Choose a reason for hiding this comment

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

I would have gone with incorrect instead of improper, but not important.

If the source is known through other means it doesn't have to be derived
from the assertion error's stack trace.
The formatted labels sometimes assume the context of the error message.
And, since formatted values can be quite long, it's odd to see the
message only at the very end.
@novemberborn novemberborn force-pushed the better-throws-helper branch from ac348ff to 132c887 Compare March 26, 2017 13:23
Show instructions on how to use `t.throws()` with the test results,
without writing them to stderr. Detect the improper usage even if user
code swallows the error, meaning that tests will definitely fail.

Assume that errors thrown, or emitted from an observable, or if the
returned promise was rejected, that that error is due to the improper
usage of `t.throws()`.

Assume that if a test has a pending throws assertion, and an error leaks
as an uncaught exception or an unhandled rejection, the error was
thrown due to the pending throws assertion. Attribute it to the test.
@novemberborn novemberborn force-pushed the better-throws-helper branch from 132c887 to 5ce8038 Compare March 26, 2017 13:44
@novemberborn novemberborn merged commit d924045 into master Mar 26, 2017
@novemberborn novemberborn deleted the better-throws-helper branch March 26, 2017 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants