with-trace.test.js
989 Bytes
import { describe, it, expect } from 'vitest'
import { withTrace } from '../src/with-trace.js'
import { createRequestContext } from '../src/context.js'
describe('withTrace', () => {
it('injects a fresh ctx as first arg when none is passed', () => {
const wrapped = withTrace((ctx, x) => ({ traceId: ctx.traceId, x }))
const r = wrapped(42)
expect(typeof r.traceId).toBe('string')
expect(r.traceId.length).toBeGreaterThan(0)
expect(r.x).toBe(42)
})
it('passes through an existing ctx (same trace, no rewrap)', () => {
const ctx = createRequestContext({ traceId: 'keep-me' })
const wrapped = withTrace((c, y) => ({ traceId: c.traceId, y, same: c === ctx }))
const r = wrapped(ctx, 7)
expect(r.traceId).toBe('keep-me')
expect(r.y).toBe(7)
expect(r.same).toBe(true)
})
it('generated traceIds differ across calls without ctx', () => {
const wrapped = withTrace((ctx) => ctx.traceId)
expect(wrapped()).not.toBe(wrapped())
})
})