diff --git a/packages/container/lib/registry.js b/packages/container/lib/registry.js index fd9237d347b..1e510961c29 100644 --- a/packages/container/lib/registry.js +++ b/packages/container/lib/registry.js @@ -3,8 +3,10 @@ import { assert, deprecate } from 'ember-debug'; import { EMBER_MODULE_UNIFICATION } from 'ember/features'; import Container from './container'; import { DEBUG } from 'ember-env-flags'; +import { ENV } from 'ember-environment'; const VALID_FULL_NAME_REGEXP = /^[^:]+:[^:]+$/; +let missingResolverFunctionsDeprecation = 'Passing a `resolver` function into a Registry is deprecated. Please pass in a Resolver object with a `resolve` method.'; /** A registry used to store factory and option information keyed @@ -24,7 +26,14 @@ export default class Registry { this.fallback = options.fallback || null; this.resolver = options.resolver || null; - if (typeof this.resolver === 'function') { + if (ENV._ENABLE_RESOLVER_FUNCTION_SUPPORT !== true) { + assert( + missingResolverFunctionsDeprecation, + typeof this.resolver !== 'function' + ); + } + + if (typeof this.resolver === 'function' && ENV._ENABLE_RESOLVER_FUNCTION_SUPPORT === true) { deprecateResolverFunction(this); } @@ -604,7 +613,7 @@ export default class Registry { function deprecateResolverFunction(registry) { deprecate( - 'Passing a `resolver` function into a Registry is deprecated. Please pass in a Resolver object with a `resolve` method.', + missingResolverFunctionsDeprecation, false, { id: 'ember-application.registry-resolver-as-function', until: '3.0.0', url: 'https://emberjs.com/deprecations/v2.x#toc_registry-resolver-as-function' } ); diff --git a/packages/container/tests/registry_test.js b/packages/container/tests/registry_test.js index 2b579f8a57c..29e6eaf9276 100644 --- a/packages/container/tests/registry_test.js +++ b/packages/container/tests/registry_test.js @@ -1,8 +1,20 @@ import { Registry, privatize } from '..'; import { factory } from 'internal-test-helpers'; import { EMBER_MODULE_UNIFICATION } from 'ember/features'; +import { ENV } from 'ember-environment'; -QUnit.module('Registry'); +let originalResolverFunctionSupport; + +QUnit.module('Registry', { + setup() { + originalResolverFunctionSupport = ENV._ENABLE_RESOLVER_FUNCTION_SUPPORT; + ENV._ENABLE_RESOLVER_FUNCTION_SUPPORT = true; + }, + + teardown() { + ENV._ENABLE_RESOLVER_FUNCTION_SUPPORT = originalResolverFunctionSupport; + } +}); QUnit.test('A registered factory is returned from resolve', function() { let registry = new Registry(); @@ -473,8 +485,10 @@ QUnit.test('`knownForType` is called on the resolver if present', function() { }); }); -QUnit.test('A registry can be created with a deprecated `resolver` function instead of an object', function() { - expect(2); +QUnit.test('A registry created with `resolver` function instead of an object throws deprecation', (assert) => { + assert.expect(2); + + ENV._ENABLE_RESOLVER_FUNCTION_SUPPORT = true; let registry; @@ -486,7 +500,23 @@ QUnit.test('A registry can be created with a deprecated `resolver` function inst }); }, 'Passing a `resolver` function into a Registry is deprecated. Please pass in a Resolver object with a `resolve` method.'); - equal(registry.resolve('foo:bar'), 'foo:bar-resolved', '`resolve` still calls the deprecated function'); + assert.equal(registry.resolve('foo:bar'), 'foo:bar-resolved', '`resolve` still calls the deprecated function'); +}); + +QUnit.test('A registry created with `resolver` function instead of an object throws assertion', (assert) => { + assert.expect(1); + + ENV._ENABLE_RESOLVER_FUNCTION_SUPPORT = false; + + let registry; + + expectAssertion(() => { + registry = new Registry({ + resolver(fullName) { + return `${fullName}-resolved`; + } + }); + }, /Passing a \`resolver\` function into a Registry is deprecated\. Please pass in a Resolver object with a \`resolve\` method\./); }); QUnit.test('resolver.expandLocalLookup is not required', function(assert) {