From 6bf8b1007c40640c18ad880e9baa1d2d87e86d38 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Thu, 16 May 2019 18:14:41 +0300 Subject: [PATCH] test(platform-browser-dynamic): make `CachedResourceLoader` tests more reliable (#30515) Previously, [this test][1] would occasionally fail (e.g. on CI) with "Template cache was not found in $templateCache". This was due to a combination of: 1. [That test][2] (which removes the cache) being run right before the failing test. 2. The async `TestBed.compileComponents()` operation run in the `beforeEach()` block (which sets the cache) not having completed before the `it()` block. This commit fixes the issue by ensuring the cache is always set, before instantiating `CachedResourceLoader`. This commit also moves some operations that are only needed in one test from the `beforeEach()` block to that test's `it()` block. [1]: https://github.com/angular/angular/blob/79903b184248a57d7e9e10d69c94098f4fe34fa6/packages/platform-browser-dynamic/test/resource_loader/resource_loader_cache_spec.ts#L50 [2]: https://github.com/angular/angular/blob/79903b184248a57d7e9e10d69c94098f4fe34fa6/packages/platform-browser-dynamic/test/resource_loader/resource_loader_cache_spec.ts#L37 Fixes #30499 PR Close #30515 --- .../resource_loader_cache_spec.ts | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/packages/platform-browser-dynamic/test/resource_loader/resource_loader_cache_spec.ts b/packages/platform-browser-dynamic/test/resource_loader/resource_loader_cache_spec.ts index ead5fa17b2..f5b42e1b6e 100644 --- a/packages/platform-browser-dynamic/test/resource_loader/resource_loader_cache_spec.ts +++ b/packages/platform-browser-dynamic/test/resource_loader/resource_loader_cache_spec.ts @@ -21,17 +21,6 @@ if (isBrowser) { setTemplateCache({'test.html': '
Hello
'}); return new CachedResourceLoader(); } - beforeEach(fakeAsync(() => { - TestBed.configureCompiler({ - providers: [ - {provide: UrlResolver, useClass: TestUrlResolver, deps: []}, - {provide: ResourceLoader, useFactory: createCachedResourceLoader, deps: []} - ] - }); - - TestBed.configureTestingModule({declarations: [TestComponent]}); - TestBed.compileComponents(); - })); it('should throw exception if $templateCache is not found', () => { setTemplateCache(null); @@ -41,13 +30,12 @@ if (isBrowser) { }); it('should resolve the Promise with the cached file content on success', async(() => { - setTemplateCache({'test.html': '
Hello
'}); - resourceLoader = new CachedResourceLoader(); + resourceLoader = createCachedResourceLoader(); resourceLoader.get('test.html').then((text) => { expect(text).toBe('
Hello
'); }); })); it('should reject the Promise on failure', async(() => { - resourceLoader = new CachedResourceLoader(); + resourceLoader = createCachedResourceLoader(); resourceLoader.get('unknown.html') .then((text) => { throw new Error('Not expected to succeed.'); }) .catch((error) => {/** success */}); @@ -55,6 +43,12 @@ if (isBrowser) { it('should allow fakeAsync Tests to load components with templateUrl synchronously', fakeAsync(() => { + TestBed.configureCompiler({ + providers: [ + {provide: UrlResolver, useClass: TestUrlResolver, deps: []}, + {provide: ResourceLoader, useFactory: createCachedResourceLoader, deps: []} + ] + }); TestBed.configureTestingModule({declarations: [TestComponent]}); TestBed.compileComponents(); tick();