diff --git a/packages/techdocs-common/__mocks__/@azure/storage-blob.ts b/packages/techdocs-common/__mocks__/@azure/storage-blob.ts index 80c60acce7..b901f5f5a7 100644 --- a/packages/techdocs-common/__mocks__/@azure/storage-blob.ts +++ b/packages/techdocs-common/__mocks__/@azure/storage-blob.ts @@ -13,13 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import type { +import { BlobUploadCommonResponse, ContainerGetPropertiesResponse, } from '@azure/storage-blob'; import { EventEmitter } from 'events'; -const storage = new (global as any).StorageFilesMock(); +const storage = global.storageFilesMock; export class BlockBlobClient { private readonly blobName; diff --git a/packages/techdocs-common/__mocks__/@google-cloud/storage.ts b/packages/techdocs-common/__mocks__/@google-cloud/storage.ts index efc2454d08..856f09c4df 100644 --- a/packages/techdocs-common/__mocks__/@google-cloud/storage.ts +++ b/packages/techdocs-common/__mocks__/@google-cloud/storage.ts @@ -15,7 +15,7 @@ */ import { Readable } from 'stream'; -const storage = new (global as any).StorageFilesMock(); +const storage = global.storageFilesMock; class GCSFile { private readonly path: string; diff --git a/packages/techdocs-common/__mocks__/aws-sdk.ts b/packages/techdocs-common/__mocks__/aws-sdk.ts index 36c864bfe7..6251fe8357 100644 --- a/packages/techdocs-common/__mocks__/aws-sdk.ts +++ b/packages/techdocs-common/__mocks__/aws-sdk.ts @@ -18,7 +18,7 @@ import { ReadStream } from 'fs'; export { Credentials } from 'aws-sdk'; -const storage = new (global as any).StorageFilesMock(); +const storage = global.storageFilesMock; export class S3 { constructor() { diff --git a/packages/techdocs-common/__mocks__/globals.d.ts b/packages/techdocs-common/__mocks__/globals.d.ts new file mode 100644 index 0000000000..ad293d2ebe --- /dev/null +++ b/packages/techdocs-common/__mocks__/globals.d.ts @@ -0,0 +1,22 @@ +/* + * Copyright 2020 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare module NodeJS { + interface Global { + rootDir: string; + storageFilesMock: IStorageFilesMock; + } +} diff --git a/packages/techdocs-common/__mocks__/types.d.ts b/packages/techdocs-common/__mocks__/types.d.ts new file mode 100644 index 0000000000..e085b95851 --- /dev/null +++ b/packages/techdocs-common/__mocks__/types.d.ts @@ -0,0 +1,24 @@ +/* + * Copyright 2020 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +interface IStorageFilesMock { + emptyFiles(): void; + fileExists(targetPath: string): boolean; + readFile(targetPath: string): Buffer; + writeFile(targetPath: string, sourcePath: string): void; + writeFile(targetPath: string, sourceBuffer: Buffer): void; + writeFile(targetPath: string, source: string | Buffer): void; +} diff --git a/packages/techdocs-common/src/globals.d.ts b/packages/techdocs-common/src/globals.d.ts new file mode 100644 index 0000000000..ad293d2ebe --- /dev/null +++ b/packages/techdocs-common/src/globals.d.ts @@ -0,0 +1,22 @@ +/* + * Copyright 2020 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare module NodeJS { + interface Global { + rootDir: string; + storageFilesMock: IStorageFilesMock; + } +} diff --git a/packages/techdocs-common/src/setupTests.ts b/packages/techdocs-common/src/setupTests.ts index 996a8d35dd..9a13e57633 100644 --- a/packages/techdocs-common/src/setupTests.ts +++ b/packages/techdocs-common/src/setupTests.ts @@ -18,18 +18,18 @@ import os from 'os'; import path from 'path'; import fs from 'fs-extra'; -const rootDir = os.platform() === 'win32' ? 'C:\\rootDir' : '/rootDir'; +const rootDir: string = os.platform() === 'win32' ? 'C:\\rootDir' : '/rootDir'; const encoding = 'utf8'; -export class StorageFilesMock { +class StorageFilesMock implements IStorageFilesMock { private files: Record; constructor() { this.files = {}; } - public emptyFiles() { + public emptyFiles(): void { this.files = {}; } @@ -56,6 +56,5 @@ export class StorageFilesMock { } } -const _global = global as any; -_global.rootDir = rootDir; -_global.StorageFilesMock = StorageFilesMock; +global.rootDir = rootDir; +global.storageFilesMock = new StorageFilesMock(); diff --git a/packages/techdocs-common/src/stages/publish/awsS3.test.ts b/packages/techdocs-common/src/stages/publish/awsS3.test.ts index d77d643291..62ec80d634 100644 --- a/packages/techdocs-common/src/stages/publish/awsS3.test.ts +++ b/packages/techdocs-common/src/stages/publish/awsS3.test.ts @@ -26,7 +26,7 @@ import { AwsS3Publish } from './awsS3'; // NOTE: /packages/techdocs-common/__mocks__ is being used to mock aws-sdk client library -const rootDir = (global as any).rootDir; +const rootDir = global.rootDir; const getEntityRootDir = (entity: Entity) => { const { diff --git a/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts b/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts index dd658440d6..c32ec310d0 100644 --- a/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts +++ b/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts @@ -26,7 +26,7 @@ import { AzureBlobStoragePublish } from './azureBlobStorage'; // NOTE: /packages/techdocs-common/__mocks__ is being used to mock Azure client library -const rootDir = (global as any).rootDir; +const rootDir = global.rootDir; const getEntityRootDir = (entity: Entity) => { const { diff --git a/packages/techdocs-common/src/stages/publish/googleStorage.test.ts b/packages/techdocs-common/src/stages/publish/googleStorage.test.ts index 57fde767ba..fe93ef909b 100644 --- a/packages/techdocs-common/src/stages/publish/googleStorage.test.ts +++ b/packages/techdocs-common/src/stages/publish/googleStorage.test.ts @@ -26,7 +26,7 @@ import { GoogleGCSPublish } from './googleStorage'; // NOTE: /packages/techdocs-common/__mocks__ is being used to mock Google Cloud Storage client library -const rootDir = (global as any).rootDir; +const rootDir = global.rootDir; const getEntityRootDir = (entity: Entity) => { const { diff --git a/packages/techdocs-common/src/types.d.ts b/packages/techdocs-common/src/types.d.ts new file mode 100644 index 0000000000..e085b95851 --- /dev/null +++ b/packages/techdocs-common/src/types.d.ts @@ -0,0 +1,24 @@ +/* + * Copyright 2020 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +interface IStorageFilesMock { + emptyFiles(): void; + fileExists(targetPath: string): boolean; + readFile(targetPath: string): Buffer; + writeFile(targetPath: string, sourcePath: string): void; + writeFile(targetPath: string, sourceBuffer: Buffer): void; + writeFile(targetPath: string, source: string | Buffer): void; +}