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

Handle errors once #386

Open
evanp opened this issue Apr 27, 2018 · 0 comments
Open

Handle errors once #386

evanp opened this issue Apr 27, 2018 · 0 comments
Assignees
Labels
enhancement The software would be more useful if it did more

Comments

@evanp
Copy link
Contributor

evanp commented Apr 27, 2018

We should have a way to pass errors from a topic to a single error handler per batch, or a single global handler.

The way we're handling errors in 1.x, you get a lot of duplication if you have multiple tests:

vows.describe("handle error once")
  .addBatch({
    "When we read a file": {
      topic() {
        fs.readFile("/tmp/somefile.txt", "utf8", this.callback)
      },
      "it is the right data type": (err, data) => {
        assert.ifError(err)
        assert.isString(data)
      },
      "it is the right size": (err, data) => {
        assert.ifError(err)
        assert.lengthOf(data, 42)
      },
      "it has the right contents": (err, data) => {
        assert.ifError(err)
        assert.match(data, /some contents/)
      }
    }
  })
  .export(module)

It would be nice if, when you define an error handler for the batch, that would get called for any errors, and tests would only be run if there is no error.

vows.describe("handle error once")
  .addBatch({
    "When we read a file": {
      topic() {
        fs.readFile("/tmp/somefile.txt", "utf8", this.callback)
      },
      error(err) {
        console.error(err)
      },
      "it is the right data type": (data) => {
        assert.isString(data)
      },
      "it is the right size": (data) => {
        assert.lengthOf(data, 42)
      },
      "it has the right contents": (data) => {
        assert.match(data, /some contents/)
      }
    }
  })
  .export(module)

We could even have a mechanism for declaring a global error handler.

vows.options.errorHandler = (err) => {
  console.error(err)
}

vows.describe("default error handler")
  .addBatch({
    "When we read a file": {
      topic() {
        fs.readFile("/tmp/somefile.txt", "utf8", this.callback)
      },
      "it is the right data type": (data) => {
        assert.isString(data)
      },
      "it is the right size": (data) => {
        assert.lengthOf(data, 42)
      },
      "it has the right contents": (data) => {
        assert.match(data, /some contents/)
      }
    }
  })
  .export(module)

This would only handle errors from the topic:

  1. If the topic function throws an Error
  2. If this.callback is called with a first argument that's truthy
  3. If the topic returns a Promise that reject()s

If any of the tests throw an error, that's treated as a test failure.

@evanp evanp added the enhancement The software would be more useful if it did more label Apr 27, 2018
@evanp evanp self-assigned this Apr 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement The software would be more useful if it did more
Projects
None yet
Development

No branches or pull requests

1 participant