{"record":{"id":"12dc3350fcfe565d","repo":"angular/angular.js","slug":"injector-already-created-can-not-modify-strict-an","errorCode":null,"errorMessage":"Injector already created, can not modify strict annotations","messagePattern":"Injector already created, can not modify strict annotations","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/ngMock/angular-mocks.js","lineNumber":3426,"sourceCode":"            throw new ErrorAddingDeclarationLocationStack(e, errorForStack);\n          }\n          throw e;\n        } finally {\n          errorForStack = null;\n        }\n      }\n    }\n  };\n\n\n  angular.mock.inject.strictDi = function(value) {\n    value = arguments.length ? !!value : true;\n    return wasInjectorCreated() ? workFn() : workFn;\n\n    function workFn() {\n      if (value !== currentSpec.$injectorStrict) {\n        if (currentSpec.$injector) {\n          throw new Error('Injector already created, can not modify strict annotations');\n        } else {\n          currentSpec.$injectorStrict = value;\n        }\n      }\n    }\n  };\n\n  function InjectorState() {\n    this.shared = false;\n    this.sharedError = null;\n\n    this.cleanupAfterEach = function() {\n      return !this.shared || this.sharedError;\n    };\n  }\n})(window.jasmine || window.mocha);\n","sourceCodeStart":3408,"sourceCodeEnd":3443,"githubUrl":"https://github.com/angular/angular.js/blob/d8f77817eb5c98dec5317bc3756d1ea1812bcfbe/src/ngMock/angular-mocks.js#L3408-L3443","documentation":"angular.mock.inject.strictDi(value) toggles strict dependency-injection annotation for injectors created afterwards (catching services relying on implicit annotations). The setting is baked into the injector at creation time, so attempting to change it once currentSpec.$injector exists throws 'Injector already created, can not modify strict annotations'. Note it only throws when the value actually differs from the current setting.","triggerScenarios":"Calling inject.strictDi(true) inside an it() or beforeEach that runs after another beforeEach already called inject(); enabling strictDi lazily to check one service after fixtures were injected; disabling (strictDi(false)) in a nested describe after the parent setup injected with strict mode on.","commonSituations":"Adding strict-di verification to an existing suite whose beforeEach already creates injectables; calling inject.strictDi after module() setup was consumed by an inject() in the same file's earlier hook; migration checks for minification safety (implicit annotations) run too late in the lifecycle.","solutions":["Set it globally and early: angular.mock.inject.strictDi(true) in a top-level/global beforeEach in a shared karma setup file, before any module/inject runs.","Within a describe, ensure the strictDi call sits in a beforeEach that is declared before (and therefore runs before) any beforeEach that calls inject().","Keep it consistent across the suite — flipping it per-test requires isolated injector creation ordering that Jasmine hooks make fragile."],"exampleFix":"// before\ndescribe('svc', function() {\n  beforeEach(inject(function(mySvc) { /* injector created here */ }));\n  beforeEach(function() {\n    angular.mock.inject.strictDi(true); // throws: injector already created\n  });\n});\n\n// after\n// karma-global setup (runs first)\nbeforeEach(function() {\n  angular.mock.inject.strictDi(true);\n});\ndescribe('svc', function() {\n  beforeEach(inject(function(mySvc) { /* now strict-di applies */ }));\n});","handlingStrategy":"validation","validationCode":"// Set strict-di once, globally, before anything injects\n// (e.g., in a karma-shared setup file)\nbeforeEach(function() {\n  angular.mock.inject.strictDi(true);\n});\n// ...only then, in suites:\nbeforeEach(module('app'));\nbeforeEach(inject(function(mySvc) { /* strict-di applies here */ }));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set strictDi in the outermost (globally first) beforeEach, never per-test.","Keep the strictDi value uniform across the suite; flipping it mid-lifecycle is what throws.","Annotate all services (ng-annotate / explicit $inject) so strict mode passes."],"tags":["angular-mocks","injector","strict-di","lifecycle","test-setup"],"backgroundTag":"injector-already-created","analyzedSha":"d8f77817eb5c98dec5317bc3756d1ea1812bcfbe","analyzedAt":"2026-08-21T20:36:31.651Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}