Skip to content

Commit

Permalink
fix: throws a warning for detector (aws-amplify#12307)
Browse files Browse the repository at this point in the history
* fix: throws a warning for detector

* fix: address comments

* chore: fix unit test header
  • Loading branch information
akshbhu committed Apr 17, 2023
1 parent 9e0097a commit 79e440d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,42 @@ const mockContext = {
describe('print migration warning tests', () => {
beforeEach(() => jest.clearAllMocks());

it('no migration message when detector failed for some reason', async () => {
const resourcesToBeCreated = [
{
category: 'function',
resourceName: 'someResource',
service: 'Lambda',
},
];
const resourcesToBeDeleted = [
{
category: 'auth',
resourceName: 'someResource1',
service: 'Cognito',
},
];
const resourcesToBeUpdated = [
{
category: 'storage',
resourceName: 'someResource2',
service: 'S3',
},
];

const allResources = [...resourcesToBeCreated, ...resourcesToBeDeleted, ...resourcesToBeUpdated];
mockContext.amplify.getResourceStatus.mockResolvedValue({ allResources });
// amplify-node-detector plug
detectAffectedDirectDependenciesMock.mockImplementation(() => {
throw new Error();
});
// override plug
fsMock.existsSync.mockReturnValue(false);
printerMock.warn.mockReturnValue(undefined);
await printCdkMigrationWarning(mockContext as unknown as $TSContext);
expect(printerMock.warn).not.toBeCalled();
});

it('no migration message when there are no override and custom resources', async () => {
const resourcesToBeCreated = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@ export type AmplifyWarning = {
* print cdk migration warning if required
*/
export const printCdkMigrationWarning = async (context: $TSContext): Promise<void> => {
const resourcesToBuild: IAmplifyResource[] = [];
const { allResources } = await context.amplify.getResourceStatus();
allResources.forEach((resource) => {
resourcesToBuild.push({
service: resource.service as string,
category: resource.category as string,
resourceName: resource.resourceName as string,
try {
const resourcesToBuild: IAmplifyResource[] = [];
const { allResources } = await context.amplify.getResourceStatus();
allResources.forEach((resource) => {
resourcesToBuild.push({
service: resource.service as string,
category: resource.category as string,
resourceName: resource.resourceName as string,
});
});
});
// check for override.ts file enabled
const migrationString = getMigrationMessage(resourcesToBuild);
if (!_.isEmpty(migrationString)) {
printer.warn(migrationString);
// check for override.ts file enabled
const migrationString = getMigrationMessage(resourcesToBuild);
if (!_.isEmpty(migrationString)) {
printer.warn(migrationString);
}
} catch (error) {
// suppress error if cdk detection fails for some reason
}
};

Expand Down

0 comments on commit 79e440d

Please sign in to comment.