Skip to content

Commit cef5820

Browse files
authored
chore: convert telemetry tests from mocha to vitest (#32577)
* chore: start vitest conversion * convert githubActionsDetectorSync to vitest * chore: convert on start span processor to vitest * chore: convert cloud span exporter to vitest * convert trace link exporter to vitest * chore: convert ipc span exporter tests to vitest * chore: convert websocket span exporter to vitest * chore: convert browser spec to vitest * chore: convert index spec to vitest * chore: convert node spec to vitest * clean up telemetry package after vite conversion * add missing import * change assertions to be not.toBeUndefined() to toBeDefined() * correctly pack config * chore: stub out the @opentelemetry/exporter-trace-otlp-http
1 parent 0ad725d commit cef5820

16 files changed

+606
-655
lines changed

.circleci/src/workflows/@workflows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ jobs:
17841784
source ./scripts/ensure-node.sh
17851785
yarn lerna run types
17861786
- sanitize-verify-and-store-mocha-results:
1787-
expectedResultCount: 17
1787+
expectedResultCount: 15
17881788

17891789
verify-release-readiness:
17901790
<<: *defaults

.circleci/workflows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3486,7 +3486,7 @@ jobs:
34863486
yarn lerna run types
34873487
name: Test types
34883488
- sanitize-verify-and-store-mocha-results:
3489-
expectedResultCount: 16
3489+
expectedResultCount: 15
34903490
working_directory: ~/cypress
34913491
v8-integration-tests:
34923492
environment:

guides/esm-migration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
- [ ] packages/server
107107
- [ ] packages/socket
108108
- [x] packages/stderr-filtering ✅ **COMPLETED**
109-
- [ ] packages/telemetry
109+
- [x] packages/telemetry**COMPLETED**
110110
- [ ] packages/ts - ultimate goal is removal and likely not worth the effort to convert
111111
- [x] packages/types ✅ **COMPLETED**
112112
- [ ] packages/v8-snapshot-require

packages/telemetry/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"check-ts": "tsc --noEmit && yarn -s tslint",
1111
"clean": "rimraf dist",
1212
"test": "yarn test-unit",
13-
"test-unit": "mocha --config ./test/.mocharc.js",
13+
"test-debug": "vitest --inspect-brk --no-file-parallelism --test-timeout=0",
14+
"test-unit": "vitest run",
1415
"tslint": "tslint --config ../ts/tslint.json --project .",
1516
"watch": "tsc --watch"
1617
},
@@ -27,7 +28,7 @@
2728
},
2829
"devDependencies": {
2930
"@packages/ts": "0.0.0-development",
30-
"mocha": "7.0.1"
31+
"vitest": "^3.2.4"
3132
},
3233
"files": [
3334
"dist",

packages/telemetry/test/.mocharc.js

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,83 @@
11
// @ts-expect-error
22
global.window = {}
33

4-
import { expect } from 'chai'
5-
4+
import { describe, it, expect, beforeAll } from 'vitest'
65
import { telemetry } from '../src/browser'
7-
86
import { Telemetry as TelemetryClass } from '../src/index'
97

108
describe('telemetry is disabled', () => {
119
describe('init', () => {
1210
it('does not throw', () => {
13-
expect(telemetry.init({
14-
namespace: 'namespace',
15-
config: { version: 'version' },
16-
})).to.not.throw
11+
expect(() => {
12+
telemetry.init({
13+
namespace: 'namespace',
14+
config: { version: 'version' },
15+
})
16+
}).not.toThrow()
1717

18-
expect(window.cypressTelemetrySingleton).to.be.undefined
18+
expect(window.cypressTelemetrySingleton).toBeUndefined()
1919
})
2020
})
2121

2222
describe('attach', () => {
2323
it('returns void', () => {
24-
expect(telemetry.attach()).to.not.throw
24+
expect(() => telemetry.attach()).not.toThrow()
2525
})
2626
})
2727

2828
describe('startSpan', () => {
2929
it('returns undefined', () => {
30-
expect(telemetry.startSpan({ name: 'nope' })).to.be.undefined
30+
expect(telemetry.startSpan({ name: 'nope' })).toBeUndefined()
3131
})
3232
})
3333

3434
describe('getSpan', () => {
3535
it('returns undefined', () => {
36-
expect(telemetry.getSpan('nope')).to.be.undefined
36+
expect(telemetry.getSpan('nope')).toBeUndefined()
3737
})
3838
})
3939

4040
describe('findActiveSpan', () => {
4141
it('returns undefined', () => {
42-
expect(telemetry.findActiveSpan((span) => true)).to.be.undefined
42+
expect(telemetry.findActiveSpan((span) => true)).toBeUndefined()
4343
})
4444
})
4545

4646
describe('endActiveSpanAndChildren', () => {
4747
it('does not throw', () => {
4848
const spanny = telemetry.startSpan({ name: 'active', active: true })
4949

50-
expect(telemetry.endActiveSpanAndChildren(spanny)).to.not.throw
50+
expect(() => telemetry.endActiveSpanAndChildren(spanny)).not.toThrow()
5151
})
5252
})
5353

5454
describe('getActiveContextObject', () => {
5555
it('returns an empty object', () => {
56-
expect(telemetry.getActiveContextObject().context).to.be.undefined
56+
expect(telemetry.getActiveContextObject().context).toBeUndefined()
5757
})
5858
})
5959

6060
describe('shutdown', () => {
6161
it('does not throw', () => {
62-
expect(telemetry.shutdown()).to.not.throw
62+
expect(() => telemetry.shutdown()).not.toThrow()
6363
})
6464
})
6565

6666
describe('attachWebSocket', () => {
6767
it('does not throw', () => {
68-
expect(telemetry.attachWebSocket('s')).to.not.throw
68+
expect(() => telemetry.attachWebSocket('s')).not.toThrow()
6969
})
7070
})
7171

7272
describe('setRootContext', () => {
7373
it('does not throw', () => {
74-
expect(telemetry.setRootContext()).to.not.throw
74+
expect(() => telemetry.setRootContext()).not.toThrow()
7575
})
7676
})
7777
})
7878

7979
describe('telemetry is enabled', () => {
80-
before('init', () => {
80+
beforeAll(() => {
8181
// @ts-expect-error
8282
global.window.__CYPRESS_TELEMETRY__ = {
8383
context: {
@@ -91,41 +91,43 @@ describe('telemetry is enabled', () => {
9191
isVerbose: false,
9292
}
9393

94-
expect(telemetry.init({
95-
namespace: 'namespace',
96-
config: { version: 'version' },
97-
})).to.not.throw
94+
expect(() => {
95+
telemetry.init({
96+
namespace: 'namespace',
97+
config: { version: 'version' },
98+
})
99+
}).not.toThrow()
98100

99-
expect(window.cypressTelemetrySingleton).to.be.instanceOf(TelemetryClass)
100-
expect(window.cypressTelemetrySingleton.getResources()).to.contain({ herp: 'derp' })
101+
expect(window.cypressTelemetrySingleton).toBeInstanceOf(TelemetryClass)
102+
expect(window.cypressTelemetrySingleton.getResources()).toEqual(expect.objectContaining({ herp: 'derp' }))
101103
})
102104

103105
describe('attachWebSocket', () => {
104106
it('returns true', () => {
105107
telemetry.attachWebSocket('ws')
106108

107-
expect(window.cypressTelemetrySingleton?.getExporter()?.ws).to.equal('ws')
109+
expect(window.cypressTelemetrySingleton?.getExporter()?.ws).toEqual('ws')
108110
})
109111
})
110112

111113
describe('startSpan', () => {
112114
it('returns undefined', () => {
113-
expect(telemetry.startSpan({ name: 'nope' })).to.exist
115+
expect(telemetry.startSpan({ name: 'nope' })).toBeDefined()
114116
})
115117
})
116118

117119
describe('getSpan', () => {
118120
it('returns undefined', () => {
119121
telemetry.startSpan({ name: 'nope' })
120-
expect(telemetry.getSpan('nope')).to.be.exist
122+
expect(telemetry.getSpan('nope')).toBeDefined()
121123
})
122124
})
123125

124126
describe('findActiveSpan', () => {
125127
it('returns undefined', () => {
126128
const spanny = telemetry.startSpan({ name: 'active', active: true })
127129

128-
expect(telemetry.findActiveSpan((span) => true)).to.be.exist
130+
expect(telemetry.findActiveSpan((span) => true)).toBeDefined()
129131
spanny?.end()
130132
})
131133
})
@@ -134,24 +136,24 @@ describe('telemetry is enabled', () => {
134136
it('does not throw', () => {
135137
const spanny = telemetry.startSpan({ name: 'active', active: true })
136138

137-
expect(telemetry.endActiveSpanAndChildren(spanny)).to.not.throw
139+
expect(() => telemetry.endActiveSpanAndChildren(spanny)).not.toThrow()
138140

139-
expect(telemetry.getActiveContextObject().context).to.be.undefined
141+
expect(telemetry.getActiveContextObject().context).toBeUndefined()
140142
})
141143
})
142144

143145
describe('getActiveContextObject', () => {
144146
it('returns an empty object', () => {
145147
const spanny = telemetry.startSpan({ name: 'active', active: true })
146148

147-
expect(telemetry.getActiveContextObject().context.traceparent).to.exist
149+
expect(telemetry.getActiveContextObject().context.traceparent).toBeDefined()
148150
spanny?.end()
149151
})
150152
})
151153

152154
describe('shutdown', () => {
153155
it('does not throw', () => {
154-
expect(telemetry.shutdown()).to.not.throw
156+
expect(() => telemetry.shutdown()).not.toThrow()
155157
})
156158
})
157159

@@ -163,20 +165,24 @@ describe('telemetry is enabled', () => {
163165
config: { version: 'version' },
164166
})
165167
} catch (err) {
166-
expect(err).to.equal('Telemetry instance has already be initialized')
168+
expect(err).toEqual('Telemetry instance has already be initialized')
169+
170+
return
167171
}
172+
173+
throw 'should not be called'
168174
})
169175
})
170176

171177
describe('setRootContext', () => {
172178
it('it sets the context', () => {
173179
// @ts-expect-error
174-
expect(window.cypressTelemetrySingleton?.rootContext?.getValue(Symbol.for('OpenTelemetry Context Key SPAN'))._spanContext.spanId).to.equal('4ad8bd26672a01b0')
180+
expect(window.cypressTelemetrySingleton?.rootContext?.getValue(Symbol.for('OpenTelemetry Context Key SPAN'))._spanContext.spanId).toEqual('4ad8bd26672a01b0')
175181

176182
telemetry.setRootContext({ context: { traceparent: '00-a14c8519972996a2a0748f2c8db5a775-4ad8bd26672a01b1-01' } })
177183

178184
// @ts-expect-error
179-
expect(window.cypressTelemetrySingleton?.rootContext?.getValue(Symbol.for('OpenTelemetry Context Key SPAN'))._spanContext.spanId).to.equal('4ad8bd26672a01b1')
185+
expect(window.cypressTelemetrySingleton?.rootContext?.getValue(Symbol.for('OpenTelemetry Context Key SPAN'))._spanContext.spanId).toEqual('4ad8bd26672a01b1')
180186
})
181187
})
182188
})

0 commit comments

Comments
 (0)