Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const SilentError = require('silent-error');
const chalk = require('chalk');

const FEATURES = require('./features');
const getConfigPath = require('./utils').getConfigPath;
Expand Down Expand Up @@ -65,7 +66,19 @@ module.exports = {

isFeatureEnabled(name) {
let value = this._features[name];
return value !== undefined ? value : FEATURES[name].default;
if (value !== undefined) {
return value;
}
let feature = FEATURES[name];
if (feature === undefined) {
console.warn(
chalk.yellow(
`Warning: Unknown feature "${name}" passed to isFeatureEnabled`
)
);
return null;
}
return feature.default;
},

isFeatureExplicitlySet(name) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions tests/optional-features-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,29 @@ QUnit.module('@ember/optional-features', (hooks) => {
);
});

QUnit.test(
'it warns and returns null for unknown feature in isFeatureEnabled',
(assert) => {
let addon = buildAddon({});
let originalWarn = console.warn;
let warningMessage = null;
console.warn = (msg) => {
warningMessage = msg;
};
try {
let result = addon.isFeatureEnabled('unknown-feature');
assert.strictEqual(result, null, 'Expecting null for unknown feature');
assert.ok(
warningMessage &&
warningMessage.includes('Unknown feature "unknown-feature"'),
'Should warn about unknown feature'
);
} finally {
console.warn = originalWarn;
}
}
);

QUnit.test(
'it can query the features with `isFeatureExplicitlySet`',
(assert) => {
Expand Down