Skip to content

Commit 79e440d

Browse files
committed
fix: throws a warning for detector (aws-amplify#12307)
* fix: throws a warning for detector * fix: address comments * chore: fix unit test header
1 parent 9e0097a commit 79e440d

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

packages/amplify-provider-awscloudformation/src/__tests__/print-cdk-migration-warning.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,42 @@ const mockContext = {
5252
describe('print migration warning tests', () => {
5353
beforeEach(() => jest.clearAllMocks());
5454

55+
it('no migration message when detector failed for some reason', async () => {
56+
const resourcesToBeCreated = [
57+
{
58+
category: 'function',
59+
resourceName: 'someResource',
60+
service: 'Lambda',
61+
},
62+
];
63+
const resourcesToBeDeleted = [
64+
{
65+
category: 'auth',
66+
resourceName: 'someResource1',
67+
service: 'Cognito',
68+
},
69+
];
70+
const resourcesToBeUpdated = [
71+
{
72+
category: 'storage',
73+
resourceName: 'someResource2',
74+
service: 'S3',
75+
},
76+
];
77+
78+
const allResources = [...resourcesToBeCreated, ...resourcesToBeDeleted, ...resourcesToBeUpdated];
79+
mockContext.amplify.getResourceStatus.mockResolvedValue({ allResources });
80+
// amplify-node-detector plug
81+
detectAffectedDirectDependenciesMock.mockImplementation(() => {
82+
throw new Error();
83+
});
84+
// override plug
85+
fsMock.existsSync.mockReturnValue(false);
86+
printerMock.warn.mockReturnValue(undefined);
87+
await printCdkMigrationWarning(mockContext as unknown as $TSContext);
88+
expect(printerMock.warn).not.toBeCalled();
89+
});
90+
5591
it('no migration message when there are no override and custom resources', async () => {
5692
const resourcesToBeCreated = [
5793
{

packages/amplify-provider-awscloudformation/src/print-cdk-migration-warning.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@ export type AmplifyWarning = {
2222
* print cdk migration warning if required
2323
*/
2424
export const printCdkMigrationWarning = async (context: $TSContext): Promise<void> => {
25-
const resourcesToBuild: IAmplifyResource[] = [];
26-
const { allResources } = await context.amplify.getResourceStatus();
27-
allResources.forEach((resource) => {
28-
resourcesToBuild.push({
29-
service: resource.service as string,
30-
category: resource.category as string,
31-
resourceName: resource.resourceName as string,
25+
try {
26+
const resourcesToBuild: IAmplifyResource[] = [];
27+
const { allResources } = await context.amplify.getResourceStatus();
28+
allResources.forEach((resource) => {
29+
resourcesToBuild.push({
30+
service: resource.service as string,
31+
category: resource.category as string,
32+
resourceName: resource.resourceName as string,
33+
});
3234
});
33-
});
34-
// check for override.ts file enabled
35-
const migrationString = getMigrationMessage(resourcesToBuild);
36-
if (!_.isEmpty(migrationString)) {
37-
printer.warn(migrationString);
35+
// check for override.ts file enabled
36+
const migrationString = getMigrationMessage(resourcesToBuild);
37+
if (!_.isEmpty(migrationString)) {
38+
printer.warn(migrationString);
39+
}
40+
} catch (error) {
41+
// suppress error if cdk detection fails for some reason
3842
}
3943
};
4044

0 commit comments

Comments
 (0)