How to catch errors in component/rendering test?


#1

Version: ember@~3.1.0, ember-cli@~3.1.4

My component has a computed property that throws an error when the component is used without passing an attribute to replace the computed property.

How do I catch the error in my rendering test? Both assert.throws and assert.rejectsdo not catch the error.

My computed property looks like:

someProperty: computed(function() {
    throw new Error('some error');
})

My test looks like:

module('my-component test', function(hooks) {
    setupRenderingTest(hooks);

    test('throws', function(assert) {
        // Doesn't work, because render returns a promise.
        assert.throws(() => {
            render(hbs`{{my-component}}`);
        });

        // Doesn't work...
        assert.rejects(render(hbs`{{my-component}}`));
    ;})
});

#2

This is a bug we are trying to get fixed. The work around is something like this:

module('my-component test', function(hooks) {
    setupRenderingTest(hooks);

    test('throws', function(assert) {
      Ember.onerror = function(error) {
        // make your error assertion here
      };
      await render(hbs`{{my-component}}`) 
    });
});

This is obviously not ideal, follow along here:


#3

I personally believe that the assert.rejects example you gave above is what we want. I just need to figure out how to properly entangle the rendering related error with the promise returned from render