0

I am writing an Ember.js Addon, and I need to deprecate a feature. Can I use the same deprecation system that Ember uses? I want my users to be able to silence the warnings.

Ideally, I also need to be able to test that the deprecation message works under the right conditions, from within my test suite.

I'm using Ember 3.x.

handlebears
  • 2,168
  • 8
  • 18

1 Answers1

2

An addon can use the same style of deprecation warnings as Ember uses, through the deprecate method:

import { deprecate } from '@ember/application/deprecations';

deprecate takes three arguments: the string as a message, a boolean test where falsey means the deprecation will be shown, and an options object with more information to be displayed.

For example:

import { deprecate } from '@ember/application/deprecations';
// ...
deprecate(
   "message about your addon that mentions the addon by name", 
   false,
   {
      id: 'send-component-hash',
      until: '2.0.0',
      url: 'some url goes here'
    }
);
// ...

The message in the console will look something like this:

a yellow warning box with an alert icon and instructions

Testing a deprecation can be done with a library like ember-qunit-assert. Once it is installed, expectDeprecation will be available on assert if you are already using qunit in your tests:

assert.expectDeprecation(/expected deprecation message/);
handlebears
  • 2,168
  • 8
  • 18
  • If it's only about renaming an attribute, event or function [`deprecatingAlias` macro](https://api.emberjs.com/ember/3.10/functions/@ember%2Fobject%2Fcomputed/deprecatingAlias) is also very helpful. – jelhan Jun 06 '19 at 08:44
  • Thanks @jelhan, I'll add that suggestion to the documentation I just wrote! – handlebears Jun 07 '19 at 19:22