Repository navigation
test: add third automock foundation batch #18010
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 all commits
51fae10
9a22eab
fb2284a
3154338
c541feb
a491507
24459d6
56dba85
631a97e
433aa6a
137fd1c
c3a18ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { onTestFinished, vi } from 'vitest' | ||
| import type { DatadogRum as RealDatadogRum } from '@datadog/browser-rum' | ||
|
|
||
| type DatadogRum = Pick< | ||
| RealDatadogRum, | ||
| | 'getGlobalContext' | ||
| | 'getInitConfiguration' | ||
| | 'init' | ||
| | 'setGlobalContextProperty' | ||
| > | ||
|
|
||
| const context: ReturnType<DatadogRum['getGlobalContext']> = {} | ||
| let contextCleanupRegistered = false | ||
|
|
||
| export const datadogRum = vi.mockObject<DatadogRum>( | ||
| { | ||
| getGlobalContext: () => context, | ||
| getInitConfiguration: () => undefined, | ||
| init: () => {}, | ||
| setGlobalContextProperty: (key, value) => { | ||
| if (!contextCleanupRegistered) { | ||
| onTestFinished(() => { | ||
| for (const key of Object.keys(context)) delete context[key] | ||
| contextCleanupRegistered = false | ||
| }) | ||
| contextCleanupRegistered = true | ||
| } | ||
| context[key] = value | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
Collaborator
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. issue: the cleanup is registered on the read path, but the write path is what dirties the shared object — so a test that only initializes leaks context into the next test.
Reproduced at it('writes global context without reading it', async () => {
await initDatadogRum('cloud.comfy.org')
expect(datadogRum.setGlobalContextProperty).toHaveBeenCalled()
})
it('sees a pristine global context', () => {
expect(datadogRum.getGlobalContext()).toEqual({})
})The current adopter suite passes only because its tests happen not to read the context after an init-only test, which makes this an order-dependent trap in shared infrastructure the PR is explicitly building for reuse. The Cheapest fix that keeps your no-setter-hooks rule is to register from the write path too: setGlobalContextProperty: (key, value) => {
onTestFinished(() => {
for (const k of Object.keys(context)) delete context[k]
})
context[key] = value
}Or register the reset once at module scope with a
Contributor
Author
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. Fixed in
Contributor
Author
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. Follow-up in
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. Tracked as FE-2713. The PR merged with this thread unresolved, so the finding is carried in Linear rather than lost here. Filed automatically; close the ticket if the finding is no longer true at |
||
| } | ||
| }, | ||
| { spy: true } | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { vi } from 'vitest' | ||
| import type { useEventListener as realUseEventListener } from '@vueuse/core' | ||
|
|
||
| export const useEventListener = vi.fn<typeof realUseEventListener>(() => | ||
| vi.fn() | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { vi } from 'vitest' | ||
| import type realAxios from 'axios' | ||
|
|
||
| const axios: Pick<typeof realAxios, 'get'> = { | ||
| get: vi.fn() | ||
| } | ||
|
|
||
| export default axios |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { onTestFinished, vi } from 'vitest' | ||
| import type { LocationQuery, Router } from 'vue-router' | ||
|
|
||
| type VueRouterMock = { | ||
| useRoute: () => { query: LocationQuery } | ||
| useRouter: () => Pick<Router, 'replace'> | ||
| } | ||
|
|
||
| const query: LocationQuery = {} | ||
| const router: Pick<Router, 'replace'> = { | ||
| replace: vi.fn(async () => {}) | ||
| } | ||
|
|
||
| const vueRouter: VueRouterMock = { | ||
| useRoute: vi.fn(() => { | ||
| onTestFinished(() => { | ||
| for (const key of Object.keys(query)) delete query[key] | ||
| }) | ||
| return { query } | ||
| }), | ||
| useRouter: vi.fn(() => router) | ||
| } | ||
|
|
||
| export const { useRoute, useRouter } = vueRouter |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { vi } from 'vitest' | ||
| import type { st as realSt, t as realT } from '@/i18n' | ||
|
|
||
| export const t = vi.fn<typeof realT>((key) => String(key)) | ||
| export const st = vi.fn<typeof realSt>( | ||
| (key, fallbackMessage) => fallbackMessage || key | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { vi } from 'vitest' | ||
| import type { useMediaAssetActions as realUseMediaAssetActions } from '../useMediaAssetActions' | ||
|
|
||
| type MediaAssetActions = ReturnType<typeof realUseMediaAssetActions> | ||
|
|
||
| const mediaAssetActions = vi.mockObject<MediaAssetActions>( | ||
| { | ||
| downloadAssets: () => {}, | ||
| deleteAssets: async () => false, | ||
| copyJobId: async () => {}, | ||
| addWorkflow: async () => {}, | ||
| addMultipleToWorkflow: async () => {}, | ||
| openWorkflow: async () => {}, | ||
| openMultipleWorkflows: async () => {}, | ||
| exportWorkflow: async () => {}, | ||
| exportMultipleWorkflows: async () => {} | ||
| }, | ||
| { spy: true } | ||
| ) | ||
|
|
||
| export const useMediaAssetActions = vi.fn(() => mediaAssetActions) |
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.
suggestion: moving the hook onto the write path is the right fix, but it does shift where the "must be inside a test" constraint bites. Vitest throws
Hook onTestFinished() can only be called inside a testwhen there is no current test (@vitest/runnerchunk-artifact.js:915), so this now throws if an adopter writes context frombeforeAllor at module scope rather than from a test body.That is a more likely pattern than the old read-path placement invited — "initialize RUM once for the whole suite" in
beforeAllis a natural thing for the next adopter to write, and it would fail on the firstsetGlobalContextPropertycall with an error that points at Vitest internals rather than at this mock.initDatadogRum.test.tsis unaffected because everyinitDatadogRumcall happens inside a test.Worth either a short comment here recording that writes must happen inside a test, or having the mock clear
contextfrom abeforeEachso the constraint disappears entirely. Non-blocking — the current adopter is correct as written.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.
No code change. This constraint is intentional and is now explicit in
docs/guidance/vitest.md: shared mocks must not install module-levelbeforeEach; callers configure mutable mock state frombeforeEachor a test soonTestFinishedcan bind to the owning test. The current adopter complies.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.
Tracked as FE-2714. The PR merged with this thread unresolved, so the finding is carried in Linear rather than lost here. Filed automatically; close the ticket if the finding is no longer true at
main.