Skip to content

Commit 6a4d423

Browse files
authored
chore(cli): diff tests should not depend on message order (#680)
This turns into an error every time we add a new message to the ioHost. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent f162c19 commit 6a4d423

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

packages/aws-cdk/test/commands/diff.test.ts

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ let tmpDir: string;
2020
let ioHost = CliIoHost.instance();
2121
let notifySpy: jest.SpyInstance<Promise<void>>;
2222

23+
function output() {
24+
return notifySpy.mock.calls.map(x => x[0].message).join('\n').replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
25+
}
26+
2327
beforeAll(() => {
2428
// The toolkit writes and checks for temporary files in the current directory,
2529
// so run these tests in a tempdir so they don't interfere with each other
@@ -96,7 +100,7 @@ describe('fixed template', () => {
96100
});
97101

98102
// THEN
99-
const plainTextOutput = notifySpy.mock.calls.map(x => x[0].message).join('\n').replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
103+
const plainTextOutput = output();
100104
expect(plainTextOutput).toContain(`Resources
101105
[~] AWS::SomeService::SomeResource SomeResource
102106
└─ [~] Something
@@ -191,8 +195,8 @@ describe('import existing resources', () => {
191195
expect(createDiffChangeSet).toHaveBeenCalled();
192196

193197
// THEN
194-
const plainTextOutput = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
195-
expect(plainTextOutput.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '')).toContain(`
198+
const plainTextOutput = output();
199+
expect(plainTextOutput).toContain(`
196200
Resources
197201
[-] AWS::DynamoDB::Table MyTable orphan
198202
[←] AWS::DynamoDB::GlobalTable MyGlobalTable import
@@ -229,8 +233,8 @@ Resources
229233
expect(createDiffChangeSet).toHaveBeenCalled();
230234

231235
// THEN
232-
const plainTextOutput = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
233-
expect(plainTextOutput.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '')).toContain(`
236+
const plainTextOutput = output();
237+
expect(plainTextOutput).toContain(`
234238
Resources
235239
[-] AWS::DynamoDB::Table MyTable orphan
236240
[+] AWS::DynamoDB::GlobalTable MyGlobalTable
@@ -267,8 +271,8 @@ Resources
267271
expect(createDiffChangeSet).not.toHaveBeenCalled();
268272

269273
// THEN
270-
const plainTextOutput = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
271-
expect(plainTextOutput.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '')).toContain(`
274+
const plainTextOutput = output();
275+
expect(plainTextOutput).toContain(`
272276
Resources
273277
[-] AWS::DynamoDB::Table MyTable orphan
274278
[+] AWS::DynamoDB::GlobalTable MyGlobalTable
@@ -406,7 +410,7 @@ describe('imports', () => {
406410
});
407411

408412
// THEN
409-
const plainTextOutput = notifySpy.mock.calls[1][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
413+
const plainTextOutput = output();
410414
expect(createDiffChangeSet).not.toHaveBeenCalled();
411415
expect(plainTextOutput).toContain(`Stack A
412416
Parameters and rules created during migration do not affect resource configuration.
@@ -433,7 +437,7 @@ Resources
433437
});
434438

435439
// THEN
436-
const plainTextOutput = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
440+
const plainTextOutput = output();
437441
expect(createDiffChangeSet).toHaveBeenCalled();
438442
expect(plainTextOutput).toContain(`Stack A
439443
Parameters and rules created during migration do not affect resource configuration.
@@ -525,10 +529,9 @@ describe('non-nested stacks', () => {
525529
});
526530

527531
// THEN
528-
const plainTextOutputA = notifySpy.mock.calls[1][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
529-
expect(plainTextOutputA).toContain('Stack A');
530-
const plainTextOutputB = notifySpy.mock.calls[2][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
531-
expect(plainTextOutputB).toContain('Stack B');
532+
const plainTextOutput = output();
533+
expect(plainTextOutput).toContain('Stack A');
534+
expect(plainTextOutput).toContain('Stack B');
532535

533536
expect(notifySpy).toHaveBeenCalledWith(expect.objectContaining({
534537
message: expect.stringContaining('✨ Number of stacks with differences: 2'),
@@ -564,10 +567,9 @@ describe('non-nested stacks', () => {
564567
});
565568

566569
// THEN
567-
const plainTextOutputA = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
568-
expect(plainTextOutputA).toContain('Stack A');
569-
const plainTextOutputB = notifySpy.mock.calls[1][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
570-
expect(plainTextOutputB).toContain('Stack B');
570+
const plainTextOutput = output();
571+
expect(plainTextOutput).toContain('Stack A');
572+
expect(plainTextOutput).toContain('Stack B');
571573

572574
expect(notifySpy).toHaveBeenCalledWith(expect.objectContaining({
573575
message: expect.stringContaining('✨ Number of stacks with differences: 2'),
@@ -630,7 +632,7 @@ describe('non-nested stacks', () => {
630632
});
631633

632634
// THEN
633-
const plainTextOutput = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
635+
const plainTextOutput = output();
634636
expect(plainTextOutput).not.toContain('Stack D');
635637
expect(plainTextOutput).not.toContain('There were no differences');
636638
expect(notifySpy).toHaveBeenCalledWith(expect.objectContaining({
@@ -648,7 +650,7 @@ describe('non-nested stacks', () => {
648650
});
649651

650652
// THEN
651-
const plainTextOutput = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
653+
const plainTextOutput = output();
652654
expect(plainTextOutput).toContain('Stack A');
653655
expect(plainTextOutput).not.toContain('There were no differences');
654656
expect(notifySpy).toHaveBeenCalledWith(expect.objectContaining({
@@ -1056,8 +1058,8 @@ describe('nested stacks', () => {
10561058
});
10571059

10581060
// THEN
1059-
const plainTextOutput = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '').replace(/[ \t]+$/gm, '');
1060-
expect(plainTextOutput.trim()).toEqual(`Stack Parent
1061+
const plainTextOutput = output().replace(/[ \t]+$/gm, '');
1062+
expect(plainTextOutput.trim()).toContain(`Stack Parent
10611063
Resources
10621064
[~] AWS::CloudFormation::Stack AdditionChild
10631065
└─ [~] TemplateURL
@@ -1120,8 +1122,8 @@ There were no differences`);
11201122
});
11211123

11221124
// THEN
1123-
const plainTextOutput = notifySpy.mock.calls[1][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '').replace(/[ \t]+$/gm, '');
1124-
expect(plainTextOutput.trim()).toEqual(`Stack Parent
1125+
const plainTextOutput = output().replace(/[ \t]+$/gm, '');
1126+
expect(plainTextOutput.trim()).toContain(`Stack Parent
11251127
Resources
11261128
[~] AWS::CloudFormation::Stack AdditionChild
11271129
└─ [~] TemplateURL
@@ -1183,7 +1185,7 @@ There were no differences`);
11831185
});
11841186

11851187
// THEN
1186-
const plainTextOutput = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '').replace(/[ \t]+$/gm, '');
1188+
const plainTextOutput = output().replace(/[ \t]+$/gm, '');
11871189
expect(plainTextOutput).not.toContain('Stack UnchangedParent');
11881190
expect(plainTextOutput).not.toContain('There were no differences');
11891191
expect(notifySpy).toHaveBeenCalledWith(expect.objectContaining({
@@ -1201,7 +1203,7 @@ There were no differences`);
12011203
});
12021204

12031205
// THEN
1204-
const plainTextOutput = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '').replace(/[ \t]+$/gm, '');
1206+
const plainTextOutput = output().replace(/[ \t]+$/gm, '');
12051207
expect(plainTextOutput).toContain(`Stack Parent
12061208
Resources
12071209
[~] AWS::CloudFormation::Stack AdditionChild
@@ -1311,8 +1313,8 @@ describe('--strict', () => {
13111313
});
13121314

13131315
// THEN
1314-
const plainTextOutput = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
1315-
expect(plainTextOutput.trim()).toEqual(`Stack A
1316+
const plainTextOutput = output();
1317+
expect(plainTextOutput.trim()).toContain(`Stack A
13161318
Resources
13171319
[+] AWS::CDK::Metadata MetadataResource
13181320
[+] AWS::Something::Amazing SomeOtherResource
@@ -1333,8 +1335,8 @@ Other Changes
13331335
});
13341336

13351337
// THEN
1336-
const plainTextOutput = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
1337-
expect(plainTextOutput.trim()).toEqual(`Stack A
1338+
const plainTextOutput = output();
1339+
expect(plainTextOutput.trim()).toContain(`Stack A
13381340
Resources
13391341
[+] AWS::Something::Amazing SomeOtherResource`);
13401342

@@ -1384,15 +1386,14 @@ describe('stack display names', () => {
13841386
});
13851387

13861388
// THEN
1387-
const parentOutput = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
1388-
const childOutput = notifySpy.mock.calls[1][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
1389+
const plainTextOutput = output();
13891390

13901391
// Verify that the display name (path) is shown instead of the logical ID
1391-
expect(parentOutput).toContain('Stack Parent/NestedStack/MyChild');
1392-
expect(parentOutput).not.toContain('Stack MyChild');
1392+
expect(plainTextOutput).toContain('Stack Parent/NestedStack/MyChild');
1393+
expect(plainTextOutput).not.toContain('Stack MyChild');
13931394

1394-
expect(childOutput).toContain('Stack Parent/NestedStack');
1395-
expect(childOutput).not.toContain('Stack MyParent');
1395+
expect(plainTextOutput).toContain('Stack Parent/NestedStack');
1396+
expect(plainTextOutput).not.toContain('Stack MyParent');
13961397

13971398
expect(notifySpy).toHaveBeenCalledWith(expect.objectContaining({
13981399
message: expect.stringContaining('✨ Number of stacks with differences: 2'),
@@ -1425,10 +1426,10 @@ describe('stack display names', () => {
14251426
});
14261427

14271428
// THEN
1428-
const output = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
1429+
const plainTextOutput = output();
14291430

14301431
// Verify that the logical ID is shown when display name is not available
1431-
expect(output).toContain('Stack NoDisplayNameStack');
1432+
expect(plainTextOutput).toContain('Stack NoDisplayNameStack');
14321433

14331434
expect(exitCode).toBe(0);
14341435
});

0 commit comments

Comments
 (0)