Edit

Testing

Environment: server.

Telefunctions are plain functions — you can unit-test them by importing and calling them directly. No server, no HTTP, no mocking.

// hello.telefunc.test.js
// Environment: server
 
import { onHello } from './hello.telefunc'
 
test('onHello', async () => {
  expect(await onHello('Eve')).toBe('Welcome, Eve')
})
// hello.telefunc.test.ts
// Environment: server
 
import { onHello } from './hello.telefunc'
 
test('onHello', async () => {
  expect(await onHello('Eve')).toBe('Welcome, Eve')
})

Providing context

If a telefunction reads getContext() (e.g. to get the logged-in user), provide the context in your test setup with provideTelefuncContext() before calling it:

// Team.telefunc.test.js
// Environment: server
 
import { provideTelefuncContext } from 'telefunc'
import { onJoinTeam } from './Team.telefunc'
 
test('rejects a non-member of the team', async () => {
  provideTelefuncContext({ user: { id: 1, teams: [] } })
  await expect(onJoinTeam('acme')).rejects.toThrow() // throws Abort()
})
// Team.telefunc.test.ts
// Environment: server
 
import { provideTelefuncContext } from 'telefunc'
import { onJoinTeam } from './Team.telefunc'
 
test('rejects a non-member of the team', async () => {
  provideTelefuncContext({ user: { id: 1, teams: [] } })
  await expect(onJoinTeam('acme')).rejects.toThrow() // throws Abort()
})

This is also how you test permissions — set up a context that should (or shouldn't) pass, then assert the telefunction returns or throws Abort() accordingly.

Channels & the wire protocol

A telefunction that opens a Channel is still a plain function — call it directly to assert it authorizes correctly and wires up its listeners.

The wire protocol — reconnection, multi-client broadcast, transport upgrades — only exists over a real connection, so cover it with an end-to-end test against a running server.

See also