Skip to content

Commit 6bcd4db

Browse files
Copilotkategengler
andcommitted
Fix isFeatureEnabled to warn instead of throw for unknown features
Co-authored-by: kategengler <444218+kategengler@users.noreply.github.com>
1 parent 3d75c52 commit 6bcd4db

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,19 @@ module.exports = {
6868

6969
isFeatureEnabled(name) {
7070
let value = this._features[name];
71-
return value !== undefined ? value : FEATURES[name].default;
71+
if (value !== undefined) {
72+
return value;
73+
}
74+
let feature = FEATURES[name];
75+
if (feature === undefined) {
76+
console.warn(
77+
chalk.yellow(
78+
`Warning: Unknown feature "${name}" passed to isFeatureEnabled`
79+
)
80+
);
81+
return null;
82+
}
83+
return feature.default;
7284
},
7385

7486
isFeatureExplicitlySet(name) {

tests/optional-features-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,29 @@ QUnit.module('@ember/optional-features', (hooks) => {
165165
);
166166
});
167167

168+
QUnit.test(
169+
'it warns and returns null for unknown feature in isFeatureEnabled',
170+
(assert) => {
171+
let addon = buildAddon({});
172+
let originalWarn = console.warn;
173+
let warningMessage = null;
174+
console.warn = (msg) => {
175+
warningMessage = msg;
176+
};
177+
try {
178+
let result = addon.isFeatureEnabled('unknown-feature');
179+
assert.strictEqual(result, null, 'Expecting null for unknown feature');
180+
assert.ok(
181+
warningMessage &&
182+
warningMessage.includes('Unknown feature "unknown-feature"'),
183+
'Should warn about unknown feature'
184+
);
185+
} finally {
186+
console.warn = originalWarn;
187+
}
188+
}
189+
);
190+
168191
QUnit.test(
169192
'it can query the features with `isFeatureExplicitlySet`',
170193
(assert) => {

0 commit comments

Comments
 (0)