forked from QuackbackIO/quackback
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
23 lines (22 loc) · 1.13 KB
/
Copy pathvitest.setup.ts
File metadata and controls
23 lines (22 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import '@testing-library/jest-dom'
import { AsyncLocalStorage } from 'node:async_hooks'
// Since @tanstack/react-start 1.168, executing a createServerFn runs the global
// request middleware, which reads the "Start context" from an AsyncLocalStorage
// keyed by this well-known global Symbol. Outside the server runtime (i.e. unit
// tests that call a server function directly, as the docs show) that store is
// empty and getStartContext() throws "No Start context found in AsyncLocalStorage".
//
// Seed the framework's own ALS (it guards creation with `if (!exists)`, so it
// reuses this instance) and make getStore() fall back to an empty context when
// none is active. Empty requestMiddleware means the app's global CSRF/logging
// middleware does not run in unit tests; a real server context still wins.
{
const KEY = Symbol.for('tanstack-start:start-storage-context')
const g = globalThis as unknown as Record<symbol, unknown>
if (!g[KEY]) {
const als = new AsyncLocalStorage<unknown>()
const getStore = als.getStore.bind(als)
als.getStore = () => getStore() ?? { startOptions: { requestMiddleware: [] } }
g[KEY] = als
}
}