Reputation: 8189
I'm using Ember.Logger.error:
if (isInvalid) {
Ember.Logger.error('this is invalid');
}
I want to test it in qunit:
assert.throws(() => myFunction(), /this is invalid/, 'error was thrown');
But assert.throws
doesn't catch the error. It does if I replace Ember.Logger.error
with a simple throw
statement, but surely there's a way to test for logged Ember errors. Anyone know the way?
UPDATE:
I made a little addon that adds this ability to QUnit. You can get it here.
Upvotes: 3
Views: 597
Reputation: 18682
Okay, so I've done a research how it's done in Ember and I've seen what's the practice to test it:
Here's example test function you could use to test calling Ember.Logger.error
in helper unit test:
/* global Ember */
import { demo } from '../../../helpers/demo';
import { module, test } from 'qunit';
module('Unit | Helper | demo');
test('catching log', function(assert) {
assert.expect(1); // define how many assertions we expect
const oldError = Ember.Logger.error; // store original error function in variable
Ember.Logger.error = function(message) { // monkey patch with our custom function
assert.equal(message, 'this is invalid', 'error was thrown'); // our assertion
};
demo(); // actually call function
Ember.Logger.error = oldError; // restore original error function
});
Upvotes: 3