refactor(service-worker): make `SwTestHarness.envIsSupported()` a standalone function (#42736)

This commit makes the `SwTestHarness.envIsSupported()` static method a
standalone function. This function is used to determine whether the
current environment provides the necessary APIs to run the SW tests and
is independent of `SwTestHarness`, so is no need for it to be a static
method of `SwTestHarness`.

This is in preparation of switching from our custom typings to the
official TypeScript typings (`lib.webworker.d.ts`).

PR Close #42736
This commit is contained in:
George Kalpakas 2021-07-08 16:25:15 +03:00 committed by atscott
parent fe135e1198
commit 7df1fa5411
7 changed files with 32 additions and 22 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -132,22 +132,6 @@ export class SwTestHarness extends Adapter<MockCacheStorage> 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;
}

View File

@ -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.
*