Skip to content

Commit 78b1a3d

Browse files
committed
refactor tests
1 parent fb692c1 commit 78b1a3d

File tree

8 files changed

+130
-274
lines changed

8 files changed

+130
-274
lines changed

sdk/highlight-run/src/client/otel/index.test.ts renamed to sdk/highlight-run/src/__tests__/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCorsUrlsPattern } from './index'
1+
import { getCorsUrlsPattern } from '../client/otel'
22

33
describe('getCorsUrlsPattern', () => {
44
it('handles `tracingOrigins: false` correctly', () => {
Lines changed: 125 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,174 @@
1-
import { H } from 'highlight.run'
2-
import { LDClientMin, HighlightPublicInterface } from '../client'
3-
4-
// Don't run tests for now. Need to move code from firstload to client for backend errors.
5-
describe.skip('should work outside of the browser in unit test', () => {
6-
let highlight: HighlightPublicInterface
1+
import { LDClientMin } from '../client'
2+
import { expect, vi } from 'vitest'
3+
import {
4+
setSessionData,
5+
setSessionSecureID,
6+
} from '../client/utils/sessionStorage/highlightSession'
7+
import * as otel from '../client/otel'
8+
import { Highlight } from '../client'
9+
import { Observe } from '../api/observe'
10+
import { ObserveSDK } from '../sdk/observe'
11+
12+
const sessionData = {
13+
sessionSecureID: 'foo',
14+
projectID: 1,
15+
payloadID: 1,
16+
lastPushTime: new Date().getTime(),
17+
sessionStartTime: new Date().getTime(),
18+
}
19+
20+
describe('should work outside of the browser in unit test', () => {
21+
let highlight: Highlight
22+
let observe: Observe
723

824
beforeEach(() => {
9-
jest.useFakeTimers()
10-
highlight = H
25+
vi.useFakeTimers()
26+
highlight = new Highlight({
27+
organizationID: undefined,
28+
sessionSecureID: '',
29+
})
30+
observe = new ObserveSDK({
31+
otlpEndpoint: '',
32+
projectId: undefined,
33+
sessionSecureId: '',
34+
})
35+
36+
setSessionSecureID('foo')
37+
setSessionData(sessionData)
1138
})
1239

1340
afterEach(() => {
14-
jest.useRealTimers()
41+
vi.useRealTimers()
1542
})
1643

1744
it('should handle init', () => {
18-
highlight.init('test')
45+
highlight.initialize({})
1946
})
2047

2148
it('should handle consumeError', () => {
2249
const error = new Error('test error')
23-
highlight.consumeError(error)
24-
})
25-
26-
it('should handle error', () => {
27-
highlight.error('test message')
50+
highlight.consumeError(error, {})
2851
})
2952

3053
it('should handle track', () => {
31-
highlight.track('test message', {})
54+
highlight.addProperties('test message', {})
3255
})
3356

3457
it('should handle start', () => {
35-
highlight.init('test', { manualStart: true })
36-
37-
highlight.start()
58+
highlight.initialize({ forceNew: true })
3859
})
3960

4061
it('should handle stop', () => {
41-
highlight.stop()
62+
highlight.stopRecording()
4263
})
4364

4465
it('should handle identify', () => {
4566
highlight.identify('123', {})
4667
})
4768

48-
it('should handle getSessionURL', () => {
49-
highlight.getSessionURL()
69+
it('should handle getSessionURL', async () => {
70+
setSessionData(sessionData)
71+
highlight.initialize()
72+
73+
expect(await highlight.getCurrentSessionURL()).toBe(
74+
'https://app.highlight.io/1/sessions/foo',
75+
)
76+
})
77+
78+
describe('startSpan', () => {
79+
it('it returns the value of the callback', () =>
80+
new Promise(async (done) => {
81+
let tracer: any
82+
await vi.waitFor(() => {
83+
tracer = otel.getTracer()
84+
expect(tracer).toBeDefined()
85+
})
86+
87+
vi.spyOn(tracer, 'startActiveSpan')
88+
89+
const value = observe.startSpan('test', () => 'test')
90+
expect(value).toBe('test')
91+
92+
expect(tracer.startActiveSpan).toHaveBeenCalledWith(
93+
'test',
94+
expect.any(Function),
95+
)
96+
97+
done(true)
98+
}))
5099
})
51100

52-
it('should handle getSessionDetails', () => {
53-
highlight.getSessionDetails()
101+
describe('startManualSpan', () => {
102+
it('it returns the value of the callback', () =>
103+
new Promise(async (done) => {
104+
let tracer: any
105+
await vi.waitFor(() => {
106+
tracer = otel.getTracer()
107+
expect(tracer).toBeDefined()
108+
})
109+
110+
vi.spyOn(tracer, 'startActiveSpan')
111+
112+
const value = observe.startManualSpan('test', (span) => {
113+
span.end()
114+
return 'test'
115+
})
116+
expect(value).toBe('test')
117+
118+
expect(tracer.startActiveSpan).toHaveBeenCalledWith(
119+
'test',
120+
expect.any(Function),
121+
)
122+
123+
done(true)
124+
}))
54125
})
55126
})
56127

128+
const sleep = (ms: number) => {
129+
const promise = new Promise((resolve) => setTimeout(resolve, ms))
130+
vi.advanceTimersByTime(ms)
131+
return promise
132+
}
133+
57134
describe('LD integration', () => {
58-
let highlight: HighlightPublicInterface
135+
let highlight: Highlight
59136

60137
beforeEach(() => {
61-
jest.useFakeTimers()
62-
highlight = H
138+
vi.useFakeTimers()
139+
highlight = new Highlight({
140+
organizationID: '',
141+
sessionSecureID: '',
142+
})
63143
})
64144

65145
afterEach(() => {
66-
jest.useRealTimers()
146+
vi.useRealTimers()
67147
})
68148

69149
it('should handle register', () => {
150+
const worker = (globalThis.Worker as unknown as typeof Worker).prototype
151+
worker.postMessage = vi.fn(
152+
(_message: unknown, _options?: unknown) => null,
153+
)
154+
70155
const client: LDClientMin = {
71-
track: jest.fn(),
72-
identify: jest.fn(),
73-
addHook: jest.fn(),
156+
track: vi.fn(),
157+
identify: vi.fn(),
158+
addHook: vi.fn(),
74159
}
75160
highlight.registerLD(client)
76161

77-
expect(client.addHook).toHaveBeenCalled()
162+
expect(client.addHook).not.toHaveBeenCalled()
78163
expect(client.identify).not.toHaveBeenCalled()
79164
expect(client.track).not.toHaveBeenCalled()
165+
expect(worker.postMessage).not.toHaveBeenCalled()
166+
167+
highlight.identify('123', {})
168+
highlight.addProperties('test', {})
169+
// noop for launchdarkly
170+
expect(client.identify).not.toHaveBeenCalled()
171+
expect(client.track).toHaveBeenCalled()
172+
expect(worker.postMessage).toHaveBeenCalled()
80173
})
81174
})

sdk/highlight-run/src/client/listeners/network-listener/utils/utils.test.ts renamed to sdk/highlight-run/src/__tests__/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
normalizeUrl,
33
shouldNetworkRequestBeRecorded,
44
shouldNetworkRequestBeTraced,
5-
} from './utils'
5+
} from '../client/listeners/network-listener/utils/utils'
66

77
describe('normalizeUrl', () => {
88
vi.stubGlobal('location', {

sdk/highlight-run/src/client/__tests__/index.test.tsx

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)