Skip to content

Commit 3e4e23c

Browse files
committed
tests: moved unit tests
1 parent 55cdbb9 commit 3e4e23c

File tree

1 file changed

+301
-0
lines changed

1 file changed

+301
-0
lines changed

Diff for: packages/tracer/tests/unit/Tracer.test.ts

+301
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
Subsegment,
1717
} from 'aws-xray-sdk-core';
1818
import { ProviderServiceInterface } from '../../src/provider';
19+
import { ConfigServiceInterface } from 'packages/tracer/src/config';
1920

2021
type CaptureAsyncFuncMock = jest.SpyInstance<
2122
unknown,
@@ -60,6 +61,306 @@ describe('Class: Tracer', () => {
6061
process.env = ENVIRONMENT_VARIABLES;
6162
});
6263

64+
describe('Method: constructor', () => {
65+
it('instantiates with default settings when no option is passed', () => {
66+
// Prepare & Act
67+
const tracer = new Tracer(undefined);
68+
69+
// Assess
70+
expect(tracer).toBeInstanceOf(Tracer);
71+
expect(tracer).toEqual(
72+
expect.objectContaining({
73+
tracingEnabled: true,
74+
serviceName: 'hello-world',
75+
captureHTTPsRequests: true,
76+
})
77+
);
78+
});
79+
80+
it('uses the provided options when passed ', () => {
81+
// Prepare
82+
const tracerOptions = {
83+
enabled: false,
84+
serviceName: 'my-lambda-service',
85+
captureHTTPsRequests: false,
86+
};
87+
88+
// Act
89+
const tracer = new Tracer(tracerOptions);
90+
91+
// Assess
92+
expect(tracer).toBeInstanceOf(Tracer);
93+
expect(tracer).toEqual(
94+
expect.objectContaining({
95+
tracingEnabled: false,
96+
serviceName: 'my-lambda-service',
97+
captureHTTPsRequests: false,
98+
})
99+
);
100+
});
101+
102+
it('uses the default service name when an invalid one is passed', () => {
103+
// Prepare
104+
const tracerOptions = {
105+
serviceName: '',
106+
};
107+
108+
// Act
109+
const tracer = new Tracer(tracerOptions);
110+
111+
// Assess
112+
expect(tracer).toBeInstanceOf(Tracer);
113+
expect(tracer).toEqual(
114+
expect.objectContaining({
115+
tracingEnabled: true,
116+
serviceName: 'hello-world',
117+
captureHTTPsRequests: true,
118+
})
119+
);
120+
});
121+
122+
it('uses the custom config service when one is passed', () => {
123+
// Prepare
124+
const configService: ConfigServiceInterface = {
125+
get(name: string): string {
126+
return `a-string-from-${name}`;
127+
},
128+
getCaptureHTTPsRequests(): string {
129+
return 'false';
130+
},
131+
getTracingEnabled(): string {
132+
return 'false';
133+
},
134+
getTracingCaptureResponse(): string {
135+
return 'false';
136+
},
137+
getTracingCaptureError(): string {
138+
return 'false';
139+
},
140+
getServiceName(): string {
141+
return 'my-backend-service';
142+
},
143+
};
144+
145+
// Act
146+
const tracer = new Tracer({
147+
customConfigService: configService,
148+
});
149+
150+
// Assess
151+
expect(tracer).toBeInstanceOf(Tracer);
152+
expect(tracer).toEqual(
153+
expect.objectContaining({
154+
customConfigService: configService,
155+
tracingEnabled: false,
156+
serviceName: 'my-backend-service',
157+
captureHTTPsRequests: false,
158+
})
159+
);
160+
});
161+
162+
it('sets captureHTTPsGlobal to true by default when tracing is enabled', () => {
163+
// Prepare
164+
const tracerOptions = {
165+
enabled: true,
166+
};
167+
168+
// Act
169+
const tracer = new Tracer(tracerOptions);
170+
171+
// Assess
172+
expect(tracer).toBeInstanceOf(Tracer);
173+
expect(tracer).toEqual(
174+
expect.objectContaining({
175+
tracingEnabled: true,
176+
captureHTTPsRequests: true,
177+
})
178+
);
179+
});
180+
});
181+
182+
describe('Environment Variables configs', () => {
183+
test('when AWS_EXECUTION_ENV environment variable is equal to AWS_Lambda_amplify-mock, tracing is disabled', () => {
184+
// Prepare
185+
process.env.AWS_EXECUTION_ENV = 'AWS_Lambda_amplify-mock';
186+
187+
// Act
188+
const tracer = new Tracer();
189+
190+
// Assess
191+
expect(tracer).toEqual(
192+
expect.objectContaining({
193+
tracingEnabled: false,
194+
})
195+
);
196+
});
197+
198+
test('when AWS_SAM_LOCAL environment variable is set, tracing is disabled', () => {
199+
// Prepare
200+
process.env.AWS_SAM_LOCAL = 'true';
201+
202+
// Act
203+
const tracer = new Tracer();
204+
205+
// Assess
206+
expect(tracer).toEqual(
207+
expect.objectContaining({
208+
tracingEnabled: false,
209+
})
210+
);
211+
});
212+
213+
test('when AWS_EXECUTION_ENV environment variable is set, tracing is enabled', () => {
214+
// Prepare
215+
process.env.AWS_EXECUTION_ENV = 'nodejs16.x';
216+
217+
// Act
218+
const tracer = new Tracer();
219+
220+
// Assess
221+
expect(tracer).toEqual(
222+
expect.objectContaining({
223+
tracingEnabled: true,
224+
})
225+
);
226+
});
227+
228+
test('when AWS_EXECUTION_ENV environment variable is NOT set, tracing is disabled', () => {
229+
// Prepare
230+
delete process.env.AWS_EXECUTION_ENV;
231+
232+
// Act
233+
const tracer = new Tracer();
234+
235+
// Assess
236+
expect(tracer).toEqual(
237+
expect.objectContaining({
238+
tracingEnabled: false,
239+
})
240+
);
241+
});
242+
243+
test('when POWERTOOLS_TRACE_ENABLED environment variable is set, a tracer with tracing disabled is returned', () => {
244+
// Prepare
245+
process.env.POWERTOOLS_TRACE_ENABLED = 'false';
246+
247+
// Act
248+
const tracer = new Tracer();
249+
250+
// Assess
251+
expect(tracer).toEqual(
252+
expect.objectContaining({
253+
tracingEnabled: false,
254+
})
255+
);
256+
});
257+
258+
test('when POWERTOOLS_SERVICE_NAME environment variable is set, a tracer with the correct serviceName is returned', () => {
259+
// Prepare
260+
process.env.POWERTOOLS_SERVICE_NAME = 'my-backend-service';
261+
262+
// Act
263+
const tracer = new Tracer();
264+
265+
// Assess
266+
expect(tracer).toEqual(
267+
expect.objectContaining({
268+
serviceName: 'my-backend-service',
269+
})
270+
);
271+
});
272+
273+
test('when POWERTOOLS_TRACER_CAPTURE_RESPONSE environment variable is set, a tracer with captureResponse disabled is returned', () => {
274+
// Prepare
275+
process.env.POWERTOOLS_TRACER_CAPTURE_RESPONSE = 'false';
276+
277+
// Act
278+
const tracer = new Tracer();
279+
280+
// Assess
281+
expect(tracer).toEqual(
282+
expect.objectContaining({
283+
captureResponse: false,
284+
})
285+
);
286+
});
287+
288+
test('when POWERTOOLS_TRACER_CAPTURE_RESPONSE environment variable is set to invalid value, a tracer with captureResponse enabled is returned', () => {
289+
// Prepare
290+
process.env.POWERTOOLS_TRACER_CAPTURE_RESPONSE = '';
291+
292+
// Act
293+
const tracer = new Tracer();
294+
295+
// Assess
296+
expect(tracer).toEqual(
297+
expect.objectContaining({
298+
captureResponse: true,
299+
})
300+
);
301+
});
302+
303+
test('when POWERTOOLS_TRACER_CAPTURE_ERROR environment variable is set, a tracer with captureError disabled is returned', () => {
304+
// Prepare
305+
process.env.POWERTOOLS_TRACER_CAPTURE_ERROR = 'false';
306+
307+
// Act
308+
const tracer = new Tracer();
309+
310+
// Assess
311+
expect(tracer).toEqual(
312+
expect.objectContaining({
313+
captureError: false,
314+
})
315+
);
316+
});
317+
318+
test('when POWERTOOLS_TRACER_CAPTURE_ERROR environment variable is set to invalid value, a tracer with captureError enabled is returned', () => {
319+
// Prepare
320+
process.env.POWERTOOLS_TRACER_CAPTURE_ERROR = '';
321+
322+
// Act
323+
const tracer = new Tracer();
324+
325+
// Assess
326+
expect(tracer).toEqual(
327+
expect.objectContaining({
328+
captureError: true,
329+
})
330+
);
331+
});
332+
333+
test('when POWERTOOLS_TRACER_CAPTURE_HTTPS_REQUESTS environment variable is set, captureHTTPsGlobal is called', () => {
334+
// Prepare
335+
process.env.POWERTOOLS_TRACER_CAPTURE_HTTPS_REQUESTS = 'false';
336+
337+
// Act
338+
const tracer = new Tracer();
339+
340+
// Assess
341+
expect(tracer).toEqual(
342+
expect.objectContaining({
343+
captureHTTPsRequests: false,
344+
})
345+
);
346+
});
347+
348+
test('when POWERTOOLS_TRACER_CAPTURE_HTTPS_REQUESTS environment variable is set to invalid value, captureHTTPsGlobal is called', () => {
349+
// Prepare
350+
process.env.POWERTOOLS_TRACER_CAPTURE_HTTPS_REQUESTS = '';
351+
352+
// Act
353+
const tracer = new Tracer();
354+
355+
// Assess
356+
expect(tracer).toEqual(
357+
expect.objectContaining({
358+
captureHTTPsRequests: true,
359+
})
360+
);
361+
});
362+
});
363+
63364
describe('Method: annotateColdStart', () => {
64365
test('when called while tracing is disabled, it does nothing', () => {
65366
// Prepare

0 commit comments

Comments
 (0)