Skip to content

Commit 22012b8

Browse files
committed
chore: refactor out for loop in tests
1 parent a4eead1 commit 22012b8

File tree

4 files changed

+210
-196
lines changed

4 files changed

+210
-196
lines changed

packages/tracer/tests/e2e/decorator.test.ts

Lines changed: 85 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('Tracer E2E tests, decorator instrumentation', () => {
6262
);
6363
testTable.grantWriteData(fnDecorator);
6464

65-
const invocationCount = 3;
65+
const invocationCount = 2;
6666
let traceData: EnrichedXRayTraceDocumentParsed[] = [];
6767

6868
beforeAll(async () => {
@@ -99,89 +99,92 @@ describe('Tracer E2E tests, decorator instrumentation', () => {
9999
'should generate all trace data correctly',
100100
async () => {
101101
// Assess
102-
for (let i = 0; i < invocationCount; i++) {
103-
const isColdStart = i === 0; // First invocation is a cold start
104-
const shouldThrowAnError = i === invocationCount - 1; // Last invocation should throw - we are testing error capture
105-
const mainSubsegment = traceData[i];
106-
const { subsegments, annotations, metadata } = mainSubsegment;
107-
108-
// Check the main segment name
109-
expect(mainSubsegment.name).toBe('## index.handler');
110-
111-
// Check the subsegments of the main segment
112-
expect(subsegments.size).toBe(3);
113-
114-
// Check remote call subsegment
115-
expect(subsegments.has('docs.powertools.aws.dev')).toBe(true);
116-
const httpSubsegment = subsegments.get('docs.powertools.aws.dev');
117-
expect(httpSubsegment?.namespace).toBe('remote');
118-
expect(httpSubsegment?.http?.request?.url).toEqual(
119-
'https://docs.powertools.aws.dev/lambda/typescript/latest/'
120-
);
121-
expect(httpSubsegment?.http?.request?.method).toBe('GET');
122-
expect(httpSubsegment?.http?.response?.status).toEqual(
123-
expect.any(Number)
124-
);
125-
expect(httpSubsegment?.http?.response?.status).toEqual(
126-
expect.any(Number)
127-
);
128-
129-
// Check the custom subsegment name & metadata
130-
expect(subsegments.has(expectedCustomSubSegmentName)).toBe(true);
131-
expect(
132-
subsegments.get(expectedCustomSubSegmentName)?.metadata
133-
).toStrictEqual({
134-
Decorator: {
135-
'myMethod response': expectedCustomResponseValue,
136-
},
137-
});
138-
139-
// Check the other custom subsegment and its subsegments
140-
expect(subsegments.has('### methodNoResponse')).toBe(true);
141-
expect(
142-
subsegments.get('### methodNoResponse')?.metadata
143-
).toBeUndefined();
144-
expect(
145-
subsegments.get('### methodNoResponse')?.subsegments?.length
146-
).toBe(1);
147-
expect(
148-
subsegments.get('### methodNoResponse')?.subsegments?.[0]?.name ===
149-
'DynamoDB'
150-
).toBe(true);
151-
152-
// Check the annotations of the main segment
153-
if (!annotations) {
154-
throw new Error('No annotations found on the main segment');
155-
}
156-
expect(annotations.ColdStart).toEqual(isColdStart);
157-
expect(annotations.Service).toEqual('Decorator');
158-
expect(annotations[expectedCustomAnnotationKey]).toEqual(
159-
expectedCustomAnnotationValue
160-
);
161-
162-
// Check the metadata of the main segment
163-
if (!metadata) {
164-
throw new Error('No metadata found on the main segment');
165-
}
166-
expect(metadata.Decorator[expectedCustomMetadataKey]).toEqual(
167-
expectedCustomMetadataValue
168-
);
169-
170-
// Check the error recording (only on invocations that should throw)
171-
if (shouldThrowAnError) {
172-
expect(mainSubsegment.fault).toBe(true);
173-
expect(Object.hasOwn(mainSubsegment, 'cause')).toBe(true);
174-
expect(mainSubsegment.cause?.exceptions[0].message).toBe(
175-
expectedCustomErrorMessage
176-
);
177-
// Check the response in the metadata (only on invocations that DON'T throw)
178-
} else {
179-
expect(metadata.Decorator['index.handler response']).toEqual(
180-
expectedCustomResponseValue
181-
);
182-
}
102+
const mainSubsegment = traceData[0];
103+
const { subsegments, annotations, metadata } = mainSubsegment;
104+
105+
// Check the main segment name
106+
expect(mainSubsegment.name).toBe('## index.handler');
107+
108+
// Check the subsegments of the main segment
109+
expect(subsegments.size).toBe(3);
110+
111+
// Check remote call subsegment
112+
expect(subsegments.has('docs.powertools.aws.dev')).toBe(true);
113+
const httpSubsegment = subsegments.get('docs.powertools.aws.dev');
114+
expect(httpSubsegment?.namespace).toBe('remote');
115+
expect(httpSubsegment?.http?.request?.url).toEqual(
116+
'https://docs.powertools.aws.dev/lambda/typescript/latest/'
117+
);
118+
expect(httpSubsegment?.http?.request?.method).toBe('GET');
119+
expect(httpSubsegment?.http?.response?.status).toEqual(
120+
expect.any(Number)
121+
);
122+
expect(httpSubsegment?.http?.response?.status).toEqual(
123+
expect.any(Number)
124+
);
125+
126+
// Check the custom subsegment name & metadata
127+
expect(subsegments.has(expectedCustomSubSegmentName)).toBe(true);
128+
expect(
129+
subsegments.get(expectedCustomSubSegmentName)?.metadata
130+
).toStrictEqual({
131+
Decorator: {
132+
'myMethod response': expectedCustomResponseValue,
133+
},
134+
});
135+
136+
// Check the other custom subsegment and its subsegments
137+
expect(subsegments.has('### methodNoResponse')).toBe(true);
138+
expect(subsegments.get('### methodNoResponse')?.metadata).toBeUndefined();
139+
expect(subsegments.get('### methodNoResponse')?.subsegments?.length).toBe(
140+
1
141+
);
142+
expect(
143+
subsegments.get('### methodNoResponse')?.subsegments?.[0]?.name ===
144+
'DynamoDB'
145+
).toBe(true);
146+
147+
// Check the annotations of the main segment
148+
if (!annotations) {
149+
throw new Error('No annotations found on the main segment');
183150
}
151+
expect(annotations.ColdStart).toEqual(true);
152+
expect(annotations.Service).toEqual('Decorator');
153+
expect(annotations[expectedCustomAnnotationKey]).toEqual(
154+
expectedCustomAnnotationValue
155+
);
156+
157+
// Check the metadata of the main segment
158+
if (!metadata) {
159+
throw new Error('No metadata found on the main segment');
160+
}
161+
expect(metadata.Decorator[expectedCustomMetadataKey]).toEqual(
162+
expectedCustomMetadataValue
163+
);
164+
165+
// Check the response is present in the metadata
166+
expect(metadata.Decorator['index.handler response']).toEqual(
167+
expectedCustomResponseValue
168+
);
184169
},
185170
TEST_CASE_TIMEOUT
186171
);
172+
173+
it('should annotate the trace with error data correctly', () => {
174+
const mainSubsegment = traceData[1];
175+
const { annotations } = mainSubsegment;
176+
177+
// Check the annotations of the main segment
178+
if (!annotations) {
179+
throw new Error('No annotations found on the main segment');
180+
}
181+
expect(annotations.ColdStart).toEqual(false);
182+
183+
// Check that the main segment has error data
184+
expect(mainSubsegment.fault).toBe(true);
185+
expect(Object.hasOwn(mainSubsegment, 'cause')).toBe(true);
186+
expect(mainSubsegment.cause?.exceptions[0].message).toBe(
187+
expectedCustomErrorMessage
188+
);
189+
});
187190
});

packages/tracer/tests/e2e/manual.test.ts

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('Tracer E2E tests, manual instantiation', () => {
6161
);
6262
testTable.grantWriteData(fnManual);
6363

64-
const invocationCount = 3;
64+
const invocationCount = 2;
6565
let traceData: EnrichedXRayTraceDocumentParsed[] = [];
6666

6767
beforeAll(async () => {
@@ -96,51 +96,56 @@ describe('Tracer E2E tests, manual instantiation', () => {
9696
'should generate all trace data correctly',
9797
async () => {
9898
// Assess
99-
for (let i = 0; i < invocationCount; i++) {
100-
const isColdStart = i === 0; // First invocation is a cold start
101-
const shouldThrowAnError = i === invocationCount - 1; // Last invocation should throw - we are testing error capture
102-
const mainSubsegment = traceData[i];
103-
const { subsegments, annotations, metadata } = mainSubsegment;
104-
105-
// Check the main segment name
106-
expect(mainSubsegment.name).toBe('## index.handler');
107-
108-
// Since CaptureHTTPsRequests is disabled, we should not have any subsegments
109-
expect(subsegments.size).toBe(0);
110-
111-
// Check the annotations of the main segment
112-
if (!annotations) {
113-
throw new Error('No annotations found on the main segment');
114-
}
115-
expect(annotations.ColdStart).toEqual(isColdStart);
116-
expect(annotations.Service).toEqual('Manual');
117-
expect(annotations[expectedCustomAnnotationKey]).toEqual(
118-
expectedCustomAnnotationValue
119-
);
120-
121-
// Check the metadata of the main segment
122-
if (!metadata) {
123-
throw new Error('No metadata found on the main segment');
124-
}
125-
expect(metadata.Manual?.[expectedCustomMetadataKey]).toEqual(
126-
expectedCustomMetadataValue
127-
);
128-
129-
// Check the error recording (only on invocations that should throw)
130-
if (shouldThrowAnError) {
131-
expect(mainSubsegment.fault).toBe(true);
132-
expect(Object.hasOwn(mainSubsegment, 'cause')).toBe(true);
133-
expect(mainSubsegment.cause?.exceptions[0].message).toBe(
134-
expectedCustomErrorMessage
135-
);
136-
// Check the response in the metadata (only on invocations that DON'T throw)
137-
} else {
138-
expect(metadata.Manual?.['index.handler response']).toEqual(
139-
expectedCustomResponseValue
140-
);
141-
}
99+
const mainSubsegment = traceData[0];
100+
const { subsegments, annotations, metadata } = mainSubsegment;
101+
102+
// Check the main segment name
103+
expect(mainSubsegment.name).toBe('## index.handler');
104+
105+
// Since CaptureHTTPsRequests is disabled, we should not have any subsegments
106+
expect(subsegments.size).toBe(0);
107+
108+
// Check the annotations of the main segment
109+
if (!annotations) {
110+
throw new Error('No annotations found on the main segment');
142111
}
112+
expect(annotations.ColdStart).toEqual(true);
113+
expect(annotations.Service).toEqual('Manual');
114+
expect(annotations[expectedCustomAnnotationKey]).toEqual(
115+
expectedCustomAnnotationValue
116+
);
117+
118+
// Check the metadata of the main segment
119+
if (!metadata) {
120+
throw new Error('No metadata found on the main segment');
121+
}
122+
expect(metadata.Manual?.[expectedCustomMetadataKey]).toEqual(
123+
expectedCustomMetadataValue
124+
);
125+
126+
// Check the response is present in the metadata
127+
expect(metadata.Manual?.['index.handler response']).toEqual(
128+
expectedCustomResponseValue
129+
);
143130
},
144131
TEST_CASE_TIMEOUT
145132
);
133+
134+
it('should annotate the trace with error data correctly', () => {
135+
const mainSubsegment = traceData[1];
136+
const { annotations } = mainSubsegment;
137+
138+
// Check the annotations of the main segment
139+
if (!annotations) {
140+
throw new Error('No annotations found on the main segment');
141+
}
142+
expect(annotations.ColdStart).toEqual(false);
143+
144+
// Check that the main segment has error data
145+
expect(mainSubsegment.fault).toBe(true);
146+
expect(Object.hasOwn(mainSubsegment, 'cause')).toBe(true);
147+
expect(mainSubsegment.cause?.exceptions[0].message).toBe(
148+
expectedCustomErrorMessage
149+
);
150+
});
146151
});

0 commit comments

Comments
 (0)