-
Notifications
You must be signed in to change notification settings - Fork 1
🧪 test: Add unit tests for initObservability #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
3d07ade
efaef61
b31ace2
9049dbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
|
|
||
| // Mock the open telemetry modules | ||
| vi.mock('@opentelemetry/sdk-node', () => { | ||
| return { | ||
| NodeSDK: vi.fn().mockImplementation(() => ({ | ||
| start: vi.fn(), | ||
| shutdown: vi.fn(), | ||
| })), | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock('@opentelemetry/api', async () => { | ||
| const actual = await vi.importActual('@opentelemetry/api'); | ||
| return { | ||
| ...actual, | ||
| trace: { | ||
| getTracer: vi.fn().mockReturnValue({ | ||
| startActiveSpan: vi.fn((name, callback) => { | ||
| const mockSpan = { | ||
| setAttribute: vi.fn(), | ||
| setStatus: vi.fn(), | ||
| recordException: vi.fn(), | ||
| end: vi.fn(), | ||
| }; | ||
| return callback(mockSpan); | ||
| }), | ||
| }), | ||
| }, | ||
| metrics: { | ||
| getMeter: vi.fn().mockReturnValue({ | ||
| createCounter: vi.fn().mockReturnValue({ add: vi.fn() }), | ||
| createHistogram: vi.fn().mockReturnValue({ record: vi.fn() }), | ||
| }), | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| describe('observability module exports', () => { | ||
| const testConfig = { | ||
| serviceName: 'test-service', | ||
| enabled: false // disabled to prevent actual connections | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| // Reset vi modules so that each test gets a fresh internal observabilityInstance variable | ||
| vi.resetModules(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('initObservability', () => { | ||
| it('should initialize and return an Observability instance', async () => { | ||
| // Import freshly to get a new instance | ||
| const mod = await import('./observability'); | ||
| const instance = mod.initObservability(testConfig); | ||
|
|
||
| expect(instance).toBeInstanceOf(mod.Observability); | ||
| // @ts-ignore - accessing private member for verification | ||
| expect(instance.serviceName).toBe('test-service'); | ||
| // @ts-ignore - accessing private member for verification | ||
| expect(instance.enabled).toBe(false); | ||
|
Comment on lines
+60
to
+63
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using A better approach is to test the observable behavior. For example, to test the References
Comment on lines
+60
to
+63
|
||
| }); | ||
|
|
||
| it('should return the same instance when called multiple times', async () => { | ||
| const mod = await import('./observability'); | ||
| const instance1 = mod.initObservability(testConfig); | ||
| const instance2 = mod.initObservability({ serviceName: 'other-service' }); // Should ignore this config | ||
|
|
||
| expect(instance1).toBe(instance2); | ||
| // @ts-ignore | ||
| expect(instance2.serviceName).toBe('test-service'); | ||
|
Comment on lines
+72
to
+73
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As with the previous comment, it's best to avoid To verify that the singleton instance is not reconfigured on subsequent calls, you could check that the initialization logic (e.g., calls to References
|
||
| }); | ||
| }); | ||
|
|
||
| describe('getObservability', () => { | ||
| it('should throw an error if called before initialization', async () => { | ||
| const mod = await import('./observability'); | ||
| expect(() => mod.getObservability()).toThrow('Observability not initialized. Call initObservability() first.'); | ||
| }); | ||
|
|
||
| it('should return the instance if already initialized', async () => { | ||
| const mod = await import('./observability'); | ||
| const initialized = mod.initObservability(testConfig); | ||
| const retrieved = mod.getObservability(); | ||
|
|
||
| expect(retrieved).toBe(initialized); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test file is located under
src/, andpackages/observability/tsconfig.jsoncurrently includes all ofsrcwithout excluding*.test.ts. That meanstscwill emit this test intodist/duringnpm run build, which is usually undesirable for a library package. Consider moving tests outsidesrc(e.g.,tests/), or updating the tsconfigexclude/includepatterns to omit test files.