Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

889 B
Raw Blame
import { Scope } from '../../scope'
import { ArrayExpr } from './array'
import { CoalesceExpr } from './coalesce'
import { LiteralExpr } from './literal'

const coalesce = (...values: unknown[]): CoalesceExpr =>
    new CoalesceExpr(new ArrayExpr(values.map((v) => new LiteralExpr(v))))

describe('CoalesceExpr', () => {
    const scope = Scope.forNode({}, 'user')

    it('returns the first non-nullish value', () => {
        expect(coalesce(null, undefined, 'chosen', 'later').eval(scope)).toBe('chosen')
    })

    it('skips nullish values but keeps falsy ones like 0 and empty string', () => {
        expect(coalesce(null, 0, 'later').eval(scope)).toBe(0)
        expect(coalesce(undefined, '', 'later').eval(scope)).toBe('')
    })

    it('returns undefined when every candidate is nullish', () => {
        expect(coalesce(null, undefined).eval(scope)).toBeUndefined()
    })
})