diff --git a/packages/service-worker/test/integration_spec.ts b/packages/service-worker/test/integration_spec.ts index 7b0de6b4d5..505a0e855d 100644 --- a/packages/service-worker/test/integration_spec.ts +++ b/packages/service-worker/test/integration_spec.ts @@ -16,12 +16,13 @@ import {Manifest} from '@angular/service-worker/worker/src/manifest'; import {MockRequest} from '@angular/service-worker/worker/testing/fetch'; import {MockFileSystemBuilder, MockServerStateBuilder, tmpHashTableForFs} from '@angular/service-worker/worker/testing/mock'; import {SwTestHarness, SwTestHarnessBuilder} from '@angular/service-worker/worker/testing/scope'; +import {envIsSupported} from '@angular/service-worker/worker/testing/utils'; import {Observable} from 'rxjs'; import {take} from 'rxjs/operators'; (function() { // Skip environments that don't support the minimum APIs needed to run the SW tests. -if (!SwTestHarness.envIsSupported()) { +if (!envIsSupported()) { return; } diff --git a/packages/service-worker/worker/test/data_spec.ts b/packages/service-worker/worker/test/data_spec.ts index 507bbdf963..fe5b04a8b6 100644 --- a/packages/service-worker/worker/test/data_spec.ts +++ b/packages/service-worker/worker/test/data_spec.ts @@ -13,10 +13,11 @@ import {MockCache} from '../testing/cache'; import {MockRequest} from '../testing/fetch'; import {MockFileSystemBuilder, MockServerStateBuilder, tmpHashTableForFs} from '../testing/mock'; import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope'; +import {envIsSupported} from '../testing/utils'; (function() { // Skip environments that don't support the minimum APIs needed to run the SW tests. -if (!SwTestHarness.envIsSupported()) { +if (!envIsSupported()) { return; } diff --git a/packages/service-worker/worker/test/happy_spec.ts b/packages/service-worker/worker/test/happy_spec.ts index a231095160..0a9561dcf2 100644 --- a/packages/service-worker/worker/test/happy_spec.ts +++ b/packages/service-worker/worker/test/happy_spec.ts @@ -15,10 +15,11 @@ import {clearAllCaches, MockCache} from '../testing/cache'; import {MockRequest, MockResponse} from '../testing/fetch'; import {MockFileSystem, MockFileSystemBuilder, MockServerState, MockServerStateBuilder, tmpHashTableForFs} from '../testing/mock'; import {MockClient, SwTestHarness, SwTestHarnessBuilder, WindowClientImpl} from '../testing/scope'; +import {envIsSupported} from '../testing/utils'; (function() { // Skip environments that don't support the minimum APIs needed to run the SW tests. -if (!SwTestHarness.envIsSupported()) { +if (!envIsSupported()) { return; } diff --git a/packages/service-worker/worker/test/idle_spec.ts b/packages/service-worker/worker/test/idle_spec.ts index 3d223c6ce3..8575e20f3e 100644 --- a/packages/service-worker/worker/test/idle_spec.ts +++ b/packages/service-worker/worker/test/idle_spec.ts @@ -8,10 +8,11 @@ import {IdleScheduler} from '../src/idle'; import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope'; +import {envIsSupported} from '../testing/utils'; (function() { // Skip environments that don't support the minimum APIs needed to run the SW tests. -if (!SwTestHarness.envIsSupported()) { +if (!envIsSupported()) { return; } diff --git a/packages/service-worker/worker/test/prefetch_spec.ts b/packages/service-worker/worker/test/prefetch_spec.ts index 9531efe7d4..4f3dc11be9 100644 --- a/packages/service-worker/worker/test/prefetch_spec.ts +++ b/packages/service-worker/worker/test/prefetch_spec.ts @@ -12,11 +12,12 @@ import {IdleScheduler} from '../src/idle'; import {MockCache} from '../testing/cache'; import {MockRequest} from '../testing/fetch'; import {MockFileSystemBuilder, MockServerStateBuilder, tmpHashTable, tmpManifestSingleAssetGroup} from '../testing/mock'; -import {MockExtendableEvent, SwTestHarness, SwTestHarnessBuilder} from '../testing/scope'; +import {MockExtendableEvent, SwTestHarnessBuilder} from '../testing/scope'; +import {envIsSupported} from '../testing/utils'; (function() { // Skip environments that don't support the minimum APIs needed to run the SW tests. -if (!SwTestHarness.envIsSupported()) { +if (!envIsSupported()) { return; } diff --git a/packages/service-worker/worker/testing/scope.ts b/packages/service-worker/worker/testing/scope.ts index 0b507d59e7..8c8b9602d3 100644 --- a/packages/service-worker/worker/testing/scope.ts +++ b/packages/service-worker/worker/testing/scope.ts @@ -132,22 +132,6 @@ export class SwTestHarness extends Adapter implements ServiceW }, } as any; - static envIsSupported(): boolean { - if (typeof URL === 'function') { - return true; - } - - // If we're in a browser that doesn't support URL at this point, don't go any further - // since browser builds use requirejs which will fail on the `require` call below. - if (typeof window !== 'undefined' && window) { - return false; - } - - // In older Node.js versions, the `URL` global does not exist. We can use `url` instead. - const url = (typeof require === 'function') && require('url'); - return url && (typeof url.parse === 'function') && (typeof url.resolve === 'function'); - } - get time() { return this.mockTime; } diff --git a/packages/service-worker/worker/testing/utils.ts b/packages/service-worker/worker/testing/utils.ts index 761907eecf..787ab712ec 100644 --- a/packages/service-worker/worker/testing/utils.ts +++ b/packages/service-worker/worker/testing/utils.ts @@ -9,6 +9,27 @@ import {NormalizedUrl} from '../src/api'; +/** + * Determine whether the current environment provides all necessary APIs to run ServiceWorker tests. + * + * @return Whether ServiceWorker tests can be run in the current environment. + */ +export function envIsSupported(): boolean { + if (typeof URL === 'function') { + return true; + } + + // If we're in a browser that doesn't support URL at this point, don't go any further + // since browser builds use requirejs which will fail on the `require` call below. + if (typeof window !== 'undefined' && window) { + return false; + } + + // In older Node.js versions, the `URL` global does not exist. We can use `url` instead. + const url = (typeof require === 'function') && require('url'); + return url && (typeof url.parse === 'function') && (typeof url.resolve === 'function'); +} + /** * Get a normalized representation of a URL relative to a provided base URL. *