Skip to content

Commit 5341c47

Browse files
committed
Improved error catching in tests
1 parent 6addea6 commit 5341c47

File tree

2 files changed

+45
-61
lines changed

2 files changed

+45
-61
lines changed

packages/tracing/tests/unit/Tracer.test.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -451,19 +451,16 @@ describe('Class: Tracer', () => {
451451

452452
}
453453

454-
// Act
455-
try {
456-
await new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
457-
} catch (error) {
458-
// Assess
459-
expect(captureAsyncFuncSpy).toHaveBeenCalledTimes(1);
460-
expect(newSubsegment).toEqual(expect.objectContaining({
461-
name: '## foo-bar-function',
462-
}));
463-
expect('cause' in newSubsegment).toBe(false);
464-
expect(addErrorFlagSpy).toHaveBeenCalledTimes(1);
465-
expect(addErrorSpy).toHaveBeenCalledTimes(0);
466-
}
454+
// Act & Assess
455+
await expect(new Lambda().handler({}, dummyContext, () => console.log('Lambda invoked!'))).rejects.toThrowError(Error);
456+
expect(captureAsyncFuncSpy).toHaveBeenCalledTimes(1);
457+
expect(newSubsegment).toEqual(expect.objectContaining({
458+
name: '## foo-bar-function',
459+
}));
460+
expect('cause' in newSubsegment).toBe(false);
461+
expect(addErrorFlagSpy).toHaveBeenCalledTimes(1);
462+
expect(addErrorSpy).toHaveBeenCalledTimes(0);
463+
expect.assertions(6);
467464

468465
delete process.env.POWERTOOLS_TRACER_CAPTURE_ERROR;
469466

@@ -490,19 +487,16 @@ describe('Class: Tracer', () => {
490487

491488
}
492489

493-
// Act
494-
try {
495-
await new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
496-
} catch (error) {
497-
// Assess
498-
expect(captureAsyncFuncSpy).toHaveBeenCalledTimes(1);
499-
expect(newSubsegment).toEqual(expect.objectContaining({
500-
name: '## foo-bar-function',
501-
}));
502-
expect('cause' in newSubsegment).toBe(true);
503-
expect(addErrorSpy).toHaveBeenCalledTimes(1);
504-
expect(addErrorSpy).toHaveBeenCalledWith(new Error('Exception thrown!'), false);
505-
}
490+
// Act & Assess
491+
await expect(new Lambda().handler({}, dummyContext, () => console.log('Lambda invoked!'))).rejects.toThrowError(Error);
492+
expect(captureAsyncFuncSpy).toHaveBeenCalledTimes(1);
493+
expect(newSubsegment).toEqual(expect.objectContaining({
494+
name: '## foo-bar-function',
495+
}));
496+
expect('cause' in newSubsegment).toBe(true);
497+
expect(addErrorSpy).toHaveBeenCalledTimes(1);
498+
expect(addErrorSpy).toHaveBeenCalledWith(new Error('Exception thrown!'), false);
499+
expect.assertions(6);
506500

507501
});
508502

packages/tracing/tests/unit/middy.test.ts

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { captureLambdaHandler } from '../../src/middleware/middy';
22
import middy from '@middy/core';
3-
// import { EnvironmentVariablesService } from '../../../src/config';
43
import { Tracer } from './../../src';
54
import type { Context, Handler } from 'aws-lambda/handler';
65
import { Segment, setContextMissingStrategy, Subsegment } from 'aws-xray-sdk-core';
@@ -76,14 +75,11 @@ describe('Middy middlewares', () => {
7675
const handler = middy(lambdaHandler).use(captureLambdaHandler(tracer));
7776
const context = Object.assign({}, mockContext);
7877

79-
// Act
80-
try {
81-
await handler({}, context, () => console.log('Lambda invoked!'));
82-
} catch (error) {
83-
// Assess
84-
expect(setSegmentSpy).toHaveBeenCalledTimes(0);
85-
expect(getSegmentSpy).toHaveBeenCalledTimes(0);
86-
}
78+
// Act & Assess
79+
await expect(handler({}, context, () => console.log('Lambda invoked!'))).rejects.toThrowError(Error);
80+
expect(setSegmentSpy).toHaveBeenCalledTimes(0);
81+
expect(getSegmentSpy).toHaveBeenCalledTimes(0);
82+
expect.assertions(3);
8783

8884
});
8985

@@ -164,19 +160,16 @@ describe('Middy middlewares', () => {
164160
const handler = middy(lambdaHandler).use(captureLambdaHandler(tracer));
165161
const context = Object.assign({}, mockContext);
166162

167-
// Act
168-
try {
169-
await handler({}, context, () => console.log('Lambda invoked!'));
170-
} catch (error) {
171-
// Assess
172-
expect(setSegmentSpy).toHaveBeenCalledTimes(1);
173-
expect(setSegmentSpy).toHaveBeenCalledWith(expect.objectContaining({
174-
name: '## foo-bar-function',
175-
}));
176-
expect('cause' in newSubsegment).toBe(false);
177-
expect(addErrorFlagSpy).toHaveBeenCalledTimes(1);
178-
expect(addErrorSpy).toHaveBeenCalledTimes(0);
179-
}
163+
// Act & Assess
164+
await expect(handler({}, context, () => console.log('Lambda invoked!'))).rejects.toThrowError(Error);
165+
expect(setSegmentSpy).toHaveBeenCalledTimes(1);
166+
expect(setSegmentSpy).toHaveBeenCalledWith(expect.objectContaining({
167+
name: '## foo-bar-function',
168+
}));
169+
expect('cause' in newSubsegment).toBe(false);
170+
expect(addErrorFlagSpy).toHaveBeenCalledTimes(1);
171+
expect(addErrorSpy).toHaveBeenCalledTimes(0);
172+
expect.assertions(6);
180173

181174
delete process.env.POWERTOOLS_TRACER_CAPTURE_ERROR;
182175

@@ -199,19 +192,16 @@ describe('Middy middlewares', () => {
199192
const handler = middy(lambdaHandler).use(captureLambdaHandler(tracer));
200193
const context = Object.assign({}, mockContext);
201194

202-
// Act
203-
try {
204-
await handler({}, context, () => console.log('Lambda invoked!'));
205-
} catch (error) {
206-
// Assess
207-
expect(setSegmentSpy).toHaveBeenCalledTimes(1);
208-
expect(setSegmentSpy).toHaveBeenCalledWith(expect.objectContaining({
209-
name: '## foo-bar-function',
210-
}));
211-
expect('cause' in newSubsegment).toBe(true);
212-
expect(addErrorSpy).toHaveBeenCalledTimes(1);
213-
expect(addErrorSpy).toHaveBeenCalledWith(new Error('Exception thrown!'), false);
214-
}
195+
// Act & Assess
196+
await expect(handler({}, context, () => console.log('Lambda invoked!'))).rejects.toThrowError(Error);
197+
expect(setSegmentSpy).toHaveBeenCalledTimes(1);
198+
expect(setSegmentSpy).toHaveBeenCalledWith(expect.objectContaining({
199+
name: '## foo-bar-function',
200+
}));
201+
expect('cause' in newSubsegment).toBe(true);
202+
expect(addErrorSpy).toHaveBeenCalledTimes(1);
203+
expect(addErrorSpy).toHaveBeenCalledWith(new Error('Exception thrown!'), false);
204+
expect.assertions(6);
215205

216206
});
217207

0 commit comments

Comments
 (0)