t3-code-android-nightly/.repos/effect-smol/packages/effect/test/StackCapture.test.ts
Julius Marminge e3c85ead63
chore(refs): sync Effect and Alchemy references to rc.115 and beta.78 (#12327)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-09-17 23:21:25 -07:00

57 lines
1.8 KiB
TypeScript

import { assert, describe, it } from "@effect/vitest"
import { Effect, References } from "effect"
const withCaptureDisabled = <A>(run: () => A): A => {
const limit = Error.stackTraceLimit
Error.stackTraceLimit = 0
try {
return run()
} finally {
Error.stackTraceLimit = limit
}
}
// Even an empty stack costs a capture in workerd, so count Error allocations.
const countErrors = (run: () => void): number => {
const original = globalThis.Error
let count = 0
globalThis.Error = new Proxy(original, {
construct(target, args, newTarget) {
count++
return Reflect.construct(target, args, newTarget)
}
})
try {
run()
return count
} finally {
globalThis.Error = original
}
}
describe("stack capture", { concurrent: false }, () => {
it("Effect.fn skips definition capture at limit zero", () => {
const count = withCaptureDisabled(() => countErrors(() => Effect.fn("test")(() => Effect.void)))
assert.strictEqual(count, 0)
})
it("Effect.fn skips invocation capture at limit zero", () => {
const fn = Effect.fn("test")(() => Effect.void)
const count = withCaptureDisabled(() => countErrors(() => Effect.runSync(fn())))
assert.strictEqual(count, 0)
})
it("default spans skip capture at limit zero", () => {
const count = withCaptureDisabled(() => countErrors(() => Effect.runSync(Effect.withSpan(Effect.void, "test"))))
assert.strictEqual(count, 0)
})
it("explicit span capture works at limit zero", () => {
const frame = withCaptureDisabled(() => {
const frame = Effect.runSync(Effect.withSpan(References.CurrentStackFrame, "test", { captureStackTrace: true }))
assert.strictEqual(Error.stackTraceLimit, 0)
return frame
})
assert.match(frame!.stack()!, /StackCapture\.test\.ts:\d+:\d+/)
})
})