<p dir="auto"><strong>I'm submitting a ...</strong>  (check one with "x")</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[x] bug report =&gt; search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request =&gt; Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question"><pre class="notranslate"><code class="notranslate">[x] bug report =&gt; search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request =&gt; Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
</code></pre></div>
<p dir="auto"><strong>Current behavior</strong><br>
We are injecting ComponentFactoryResolver into a modal service to dynamically create modal components. When testing this service, we discovered that when we create a second test with the modal service (with a different module created with entry components), the instance of the ComponentFactoryResolver from the previous test is still used. Because it only recognizes the entry components from the previous test, it throws errors with a "No component factory found for ModalTest2Component" Error. Note that using beforeEach to inject the ComponentFactoryResolver manually into the test and calling <code class="notranslate">resolveComponentFactory</code> instantiates the correct instance of ComponentFactoryResolver.</p>
<p dir="auto"><strong>Expected behavior</strong><br>
The updated ComponentFactoryResolver will be injected into our modal service on subsequent tests.</p>
<p dir="auto"><strong>Minimal reproduction of the problem with instructions</strong><br>
I pared down our repository to the files necessary to run the modal tests here: <a href="https://github.com/Blackbaud-PatrickOFriel/skyux2/tree/modal-ut-issue">https://github.com/Blackbaud-PatrickOFriel/skyux2/tree/modal-ut-issue</a>. The second test file which displays the strange behavior is at <a href="https://github.com/Blackbaud-PatrickOFriel/skyux2/blob/modal-ut-issue/src/modules/modal/modal.component2.spec.ts">https://github.com/Blackbaud-PatrickOFriel/skyux2/blob/modal-ut-issue/src/modules/modal/modal.component2.spec.ts</a>. I inserted a couple console logs to show the injected ComponentFactoryResolver working in the test and not working in the Modal service call immediately after.</p>
<ul dir="auto">
<li><strong>Browser:</strong> [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]</li>
</ul>

<ul dir="auto">
<li>
<p dir="auto"><strong>Language:</strong> [all | TypeScript X.X | ES6/7 | ES5]<br>
TypeScript</p>
</li>
<li>
<p dir="auto"><strong>Node (for AoT issues):</strong> <code class="notranslate">node --version</code> =</p>
</li>
</ul>
<p dir="auto">Thanks for your help and let me know if I can offer any more information</p>
<p dir="auto">The <code class="notranslate">ComponentFactoryResolver</code> <a href="https://github.com/angular/angular/blob/b96869afd2bdd3ad8c5b7477da647af628e383aa/modules/%40angular/core/src/linker/component_factory_resolver.ts#L40">has an internal map</a> containing factories for components that have been compiled.</p>
<p dir="auto">However, when TestBed compiles components declared in the <code class="notranslate">DynamicTestModule</code>, the factories are stored in the <a href="https://github.com/angular/angular/blob/master/modules/@angular/core/testing/test_bed.ts#L141">TestBed's internal <code class="notranslate">_moduleWithComponentFactories</code></a>. These factories are never made known to the <code class="notranslate">ComponentFactoryResolver</code>, so any tests that use the resolver to create a component declared in the test module will fail with <code class="notranslate">Error: No component factory found for YourTestComponent</code>.</p>
<p dir="auto">As a workaround, you can simply create a real <code class="notranslate">@NgModule</code> in which to declare your test components and import that into the test module.</p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/tbosch/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/tbosch">@tbosch</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/IgorMinar/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/IgorMinar">@IgorMinar</a></p>