From 978eaf9bfa9d9eac86d06d3d07d89718d219e08b Mon Sep 17 00:00:00 2001 From: blam Date: Fri, 19 Aug 2022 17:21:52 +0200 Subject: [PATCH] chore: updating some changeset and api-reports Signed-off-by: blam --- .changeset/nervous-rivers-sneeze.md | 24 +++++++++++++++++-- packages/backend-common/api-report.md | 4 ++-- .../src/reading/AwsS3UrlReader.ts | 2 ++ .../AwsS3DiscoveryProcessor.test.ts | 16 ++++++------- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/.changeset/nervous-rivers-sneeze.md b/.changeset/nervous-rivers-sneeze.md index f3a5550ead..f0abb16700 100644 --- a/.changeset/nervous-rivers-sneeze.md +++ b/.changeset/nervous-rivers-sneeze.md @@ -1,6 +1,26 @@ --- -'@backstage/backend-common': patch '@backstage/cli': patch --- -Switch out `sucrase` for `swc` for transpilation +Switch out `sucrase` for `swc` for transpilation. + +Sucrase is a little more relaxed when it comes to supporting the ways of mocking in Jest. You might have to make some changes to your tests to meet the Jest standard and spec if your tests seems to start failing. + +Mocks that look like this are invalid, and they will throw a reference error in line with the Jest documentation [here on example 3](https://jestjs.io/docs/es6-class-mocks#calling-jestmock-with-the-module-factory-parameter) + +```ts +const mockCommandExists = jest.fn(); +jest.mock('command-exists', () => mockCommandExists); +``` + +You might need to update these mocks to look a little like the following to defer the call to the `jest.fn()` spy until the mock is called. + +```ts +const mockCommandExists = jest.fn(); +jest.mock( + 'command-exists', + () => + (...args: any[]) => + commandExists(...args), +); +``` diff --git a/packages/backend-common/api-report.md b/packages/backend-common/api-report.md index 8362c3ab2f..af62238925 100644 --- a/packages/backend-common/api-report.md +++ b/packages/backend-common/api-report.md @@ -8,6 +8,7 @@ import { AbortController as AbortController_2 } from 'node-abort-controller'; import { AbortSignal as AbortSignal_2 } from 'node-abort-controller'; +import aws from 'aws-sdk'; import { AwsS3Integration } from '@backstage/integration'; import { AzureIntegration } from '@backstage/integration'; import { BitbucketCloudIntegration } from '@backstage/integration'; @@ -34,7 +35,6 @@ import { Readable } from 'stream'; import { ReadCommitResult } from 'isomorphic-git'; import { RequestHandler } from 'express'; import { Router } from 'express'; -import { S3 } from 'aws-sdk'; import { Server } from 'http'; import * as winston from 'winston'; import { Writable } from 'stream'; @@ -44,7 +44,7 @@ export class AwsS3UrlReader implements UrlReader { constructor( integration: AwsS3Integration, deps: { - s3: S3; + s3: aws.S3; treeResponseFactory: ReadTreeResponseFactory; }, ); diff --git a/packages/backend-common/src/reading/AwsS3UrlReader.ts b/packages/backend-common/src/reading/AwsS3UrlReader.ts index 5d471dfae7..b9fc58ef5c 100644 --- a/packages/backend-common/src/reading/AwsS3UrlReader.ts +++ b/packages/backend-common/src/reading/AwsS3UrlReader.ts @@ -14,6 +14,8 @@ * limitations under the License. */ +// note: We do the import like this so that we don't get issues destructuring +// and it not being mocked by the aws-sdk-mock package which is unfortunate. import aws from 'aws-sdk'; import { CredentialsOptions } from 'aws-sdk/lib/credentials'; import { diff --git a/plugins/catalog-backend-module-aws/src/processors/AwsS3DiscoveryProcessor.test.ts b/plugins/catalog-backend-module-aws/src/processors/AwsS3DiscoveryProcessor.test.ts index 49dc70ae5e..6aab7b2bec 100644 --- a/plugins/catalog-backend-module-aws/src/processors/AwsS3DiscoveryProcessor.test.ts +++ b/plugins/catalog-backend-module-aws/src/processors/AwsS3DiscoveryProcessor.test.ts @@ -13,9 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import mockAws from 'aws-sdk-mock'; -import aws from 'aws-sdk'; -import path from 'path'; + import { getVoidLogger, UrlReaders } from '@backstage/backend-common'; import { ConfigReader } from '@backstage/config'; import { AwsS3DiscoveryProcessor } from './AwsS3DiscoveryProcessor'; @@ -24,8 +22,12 @@ import { CatalogProcessorResult, processingResult, } from '@backstage/plugin-catalog-backend'; +import AWSMock from 'aws-sdk-mock'; +import aws from 'aws-sdk'; +import path from 'path'; import YAML from 'yaml'; +AWSMock.setSDKInstance(aws); const object: aws.S3.Types.Object = { Key: 'awsS3-mock-object.txt', }; @@ -33,11 +35,8 @@ const objectList: aws.S3.ObjectList = [object]; const output: aws.S3.Types.ListObjectsV2Output = { Contents: objectList, }; - -mockAws.setSDKInstance(require('aws-sdk')); - -mockAws.mock('S3', 'listObjectsV2', output); -mockAws.mock( +AWSMock.mock('S3', 'listObjectsV2', output); +AWSMock.mock( 'S3', 'getObject', Buffer.from( @@ -76,7 +75,6 @@ describe('readLocation', () => { }, ), )) as CatalogProcessorEntityResult; - console.log(generated); expect(generated.type).toBe('entity'); expect(generated.location).toEqual({ target: 'awsS3-mock-object.txt',