Skip to content

Commit e6dae91

Browse files
committed
♻️ merge the Vitest test helpers into the shared ones and drop the aliases
The Karma versions of the test helpers are dead code now: replace them with their Vitest implementations and remove the aliases redirecting to the copies in test/unit-vitest. - registerCleanupTask: runs tasks with `onTestFinished` - getCurrentJasmineSpec: tracked from forEach.ts (run for every test file) - disableJasmineUncaughtExceptionTracking: Vitest-compatible listeners - allJsonSchemas: `import.meta.glob` instead of webpack's `require.context` (no more JS + d.ts pair) - vitest declared as a devDependency of @datadog/browser-core, which now imports it in test helpers
1 parent 8a42c73 commit e6dae91

15 files changed

Lines changed: 54 additions & 123 deletions

‎packages/browser-core/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"@datadog/js-core": "workspace:*"
2727
},
2828
"devDependencies": {
29-
"pako": "3.0.2"
29+
"pako": "3.0.2",
30+
"vitest": "4.1.8"
3031
},
3132
"volta": {
3233
"extends": "../../package.json"

‎packages/browser-core/test/disableJasmineUncaughtExceptionTracking.ts‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ import { noop } from '@datadog/js-core/util'
22
import { registerCleanupTask } from './registerCleanupTask'
33

44
/**
5-
* Disable Jasmine's uncaught error handling. This is useful for test cases throwing exceptions or
6-
* unhandled rejections that are expected to be caught somehow, but Jasmine also catch them and
7-
* fails the test.
8-
*
9-
* Replace the handler instead of spying on it, so tests can still spy on `window.onerror`.
5+
* Vitest will ignore uncaught errors if we add our own listener, see
6+
* https://vitest.dev/guide/features.html#unhandled-errors
107
*/
118
export function disableJasmineUncaughtExceptionTracking() {
12-
const originalOnError = window.onerror
13-
window.onerror = noop
9+
window.addEventListener('error', noop)
10+
window.addEventListener('unhandledrejection', noop)
11+
const originalConsoleError = console.error
12+
console.error = (...args) => {
13+
if (new Error().stack!.includes('error-catcher.js')) {
14+
return
15+
}
16+
originalConsoleError(...args)
17+
}
1418
registerCleanupTask(() => {
15-
window.onerror = originalOnError
19+
console.error = originalConsoleError
20+
window.removeEventListener('error', noop)
21+
window.removeEventListener('unhandledrejection', noop)
1622
})
1723
}

‎packages/browser-core/test/forEach.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ import { resetInteractionCountPolyfill } from '../../browser-rum-core/src/domain
1414
import { resetTelemetry } from '../src/domain/telemetry'
1515
import { resetSampleDecisionCache } from '../src/domain/sampler'
1616
import { startLeakDetection } from './leakDetection'
17+
import { trackCurrentJasmineSpec } from './getCurrentJasmineSpec'
1718
;(window as any).IS_REACT_ACT_ENVIRONMENT = true
1819

20+
trackCurrentJasmineSpec()
21+
1922
beforeEach(() => {
2023
// prevent 'Some of your tests did a full page reload!' issue
2124
window.onbeforeunload = () => 'stop'
Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
1-
let currentSpec: jasmine.SpecResult | null = null
1+
import { beforeEach, onTestFinished } from 'vitest'
2+
3+
let currentSpec: { fullName: string } | null = null
24

35
export function getCurrentJasmineSpec() {
46
return currentSpec
57
}
68

7-
jasmine.getEnv().addReporter({
8-
specStarted(specResult) {
9-
currentSpec = specResult
10-
},
11-
specDone() {
12-
currentSpec = null
13-
},
14-
})
9+
/**
10+
* Called from forEach.ts, a setup file which runs for every test file. Hooks registered when this module is
11+
* evaluated would only apply to the first test file when files share the same page (`isolate:
12+
* false`).
13+
*/
14+
export function trackCurrentJasmineSpec() {
15+
beforeEach((context) => {
16+
const parts: string[] = []
17+
let current: any = context.task
18+
while (current) {
19+
if (current.name) {
20+
parts.unshift(current.name)
21+
}
22+
current = current.suite
23+
}
24+
currentSpec = { fullName: parts.join(' ') }
25+
26+
onTestFinished(() => {
27+
currentSpec = null
28+
})
29+
})
30+
}
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
type CleanupTask = () => unknown
1+
import { onTestFinished } from 'vitest'
22

3-
const cleanupTasks: CleanupTask[] = []
3+
type CleanupTask = () => unknown
44

55
export function registerCleanupTask(task: CleanupTask) {
6-
cleanupTasks.unshift(task)
7-
}
8-
9-
afterEach(async () => {
10-
for (const task of cleanupTasks.splice(0)) {
6+
onTestFinished(async () => {
117
await task()
12-
}
13-
})
8+
})
9+
}

‎packages/browser-rum-core/test/allJsonSchemas.d.ts‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎packages/browser-rum-core/test/allJsonSchemas.js‎

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/// <reference types="vite/client" />
2+
3+
const modules = import.meta.glob('../../../node_modules/@datadog/rum-events-format/schemas/**/*.json', { eager: true })
4+
export const allJsonSchemas = Object.values(modules)

‎test/unit-vitest/allJsonSchemas.ts‎

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

‎test/unit-vitest/disableJasmineUncaughtExceptionTracking.ts‎

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

0 commit comments

Comments
 (0)