Skip to content
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

Throw assertion does not catch class constructor errors #739

Closed
ejmurra opened this issue Apr 7, 2016 · 2 comments
Closed

Throw assertion does not catch class constructor errors #739

ejmurra opened this issue Apr 7, 2016 · 2 comments

Comments

@ejmurra
Copy link

ejmurra commented Apr 7, 2016

Use case: An abstract class which implements member checking in the constructor.

Example:

class FeedAggregator {
  constructor({feedName, feedType}) {
    this.feedName = feedName;
    this.feedType = feedType;

    // Check for methods that must be implemented
    if (this.getItems === undefined) {
      throw new TypeError("Class must define a getItems method");
    }
  }
}

class BadAggregator extends FeedAggregator {
  constructor({feedName, feedType}) {
    super({feedName, feedType});
  }
}

test("Expect FeedAggregator to throw error when subclass has no getItems method", t => {
  t.throws(new BadAggregator({feedName: 'test', feedType: 'test'}), TypeError("Class must define a getItems method"))
});

Expected output:

1 passed

Actual output:

1 failed

  1. Expect FeedAggregator to throw error when subclass has no getItems method
  failed with "Class must define a getItems method"
      BadAggregator.FeedAggregator (FeedAggregator.js:11:13)
    new BadAggregator (FeedAggregator.js:16:7)
    Test.fn (FeedAggregator.js:23:12)
@jamestalmage
Copy link
Contributor

You need to wrap any statement in a function. It's not possible for us to catch the Error otherwise.

For one liners like that, arrow functions are where it's at:

test("Expect FeedAggregator to throw error when subclass has no getItems method", t => {
  t.throws(
    () => new BadAggregator({feedName: 'test', feedType: 'test'}), 
    TypeError("Class must define a getItems method")
  );
});

@ejmurra
Copy link
Author

ejmurra commented Apr 8, 2016

Thanks.

jamestalmage added a commit to jamestalmage/ava that referenced this issue Apr 9, 2016
Protects against a common misuse of t.throws (Like that seen in avajs#739).

This required the creation of a custom babel plugin.

https://github.com/jamestalmage/babel-plugin-ava-throws-helper
jamestalmage added a commit that referenced this issue Apr 11, 2016
* 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
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

No branches or pull requests

2 participants