Skip to content

assert: async parameter for assert.throws() and assert.doesNotThrow()

Hello community! It's my first PR, I hope I didn't missed important stuff. Documentation isn't included yet as I'd like to get feedback on this new feature first.

The first parameter of assert.throws() and assert.doesNotThrow() can be an async function.

Benefit is to make tests way more easier to read. Validating error behaviour with promise-based code used to look like this:

it('should report database option errors', () => {
  transferData({}, '')
    .then(() => {
      throw new Error('it should have failed');
    }, err => {
      assert(err.message.includes('"host" is required'));
    });
);

Now, thanks to async/await support, it looks like this:

it('should report database option errors', async () => {
  try {
    await transferData({}, '');
    throw new Error('it should have failed');
  } catch (err) {
    assert(err.message.includes('"host" is required'));
  }
});

With native async support, it can now be written like this:

it('should report database option errors', async () =>
  assert.throws(async () => transferData({}, ''), /^Error: "host" is required/)
);

However, it may be considered as a breaking change: assert.throws() used to return nothing, while now, due to its async nature, it returns a Promise.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

assert

Merge request reports

Loading