block by samselikoff ad5e3695383b91599ee428bf9a2d22ca

How to use an `asyncThrows` custom helper.

This assert.asyncThrows custom assertion allows us to write tests against failing async code, usually as a result of a server error (4xx/5xx response).

test('If the index route errors, I see a message', async function(assert) {
  server.create('post');
  server.get('/posts/:id', { errors: ['The site is down'] }, 500); // force Mirage to error

  await assert.asyncThrows(() => {
    return visit('/posts/1');
  }, 'GET /posts/1 returned a 500');

  assert.ok(find(':contains(The site is down)').length > 1);
});

Without this helper, a failed XHR request would trigger an exception in QUnit’s test suite.

How to use

  1. Install Ember CLI Custom Assertions
  2. Add the file below under /tests/assertions/async-throws.js

Now you can use assert.asyncThrows in your test. The first argument is a function that’s expected to return a promise; the second is a string that must match the exception you expect to be thrown.

async-throws.js