Override computed() property in test


After updating to Ember 3.9, we now get a bunch of these deprecation warnings: https://deprecations.emberjs.com/v3.x/#toc_computed-property-override

For places in our application where we use this, the steps to “fix” the deprecation make sense and we’ll gladly follow them.

However, there are some instances where we override a computed() property in a unit test, e.g. to mock certain things. These places now also trigger this deprecation. For example:

myProperty: computed(function() {
  // do some complex stuff that depends on a bunch of other properties...
  return myProperty;

In the test:

let component = this.owner.factoryFor('component:my-component').create({
  myProperty: mockedProperty

It doesn’t feel nice to add a setter and an internal private property etc. just so this can be mocked in a test… Are there any other patterns or ideas on how to tackle this in a nice, non-deprecation-triggering way?



I believe the deprecation is only triggered when you “clobber” the computed property, in your case you can override it by extending.

Something like this:

import FooService from 'my-app/services/foo';

test('asdf', function(assert) {
  this.owner.register('service:foo', FooService.extend({
    myProperty: mockedProperty

  let service = this.owner.lookup('service:foo');
  // do stuff....

Note: I specifically changed the example to use a service instead of a component (though the technique I am describing is unrelated to the object type). In general, its not a good idea to manually instantiate components as it leads to brittle tests across Ember upgrades.



Ah, extending it in the test is a neat idea. I’ll try that out - thanks!