Testing
The tests on this page are a real file. They run in this repository's CI, under npm test, alongside the library's own suite - so if something here is wrong, the build goes red.
Testing update
update is a function from two values to two values. That is the whole story:
describe('update', () => {
it('is an ordinary function: call it, compare the result', () => {
const [model] = Counter.update({ type: 'Increment' }, { count: 0 })
expect(model).toEqual({ count: 1 })
})
it('needs no renderer, no mocks and nothing async', () => {
const [model] = Counter.update({ type: 'Reset' }, { count: 41 })
expect(model).toEqual({ count: 0 })
})
})No renderer, no test harness, no act(), nothing async. This is the single biggest practical benefit of keeping side effects out of update, and it is why the architecture insists on it.
Testing commands
A Cmd is a Stream of messages, so running it and collecting what comes out is an ordinary Effect operation:
describe('commands', () => {
it('a Cmd is a Stream of messages - run it to see what it produces', async () => {
const [model, cmd] = Dice.update({ type: 'Roll' }, { face: 1, rolling: false })
// The model records the roll immediately; the number is not known yet.
expect(model).toEqual({ face: 1, rolling: true })
const messages = await Effect.runPromise(Stream.runCollect(cmd))
expect(messages).toHaveLength(1)
expect(messages[0]).toMatchObject({ type: 'Rolled' })
})
})Notice the first assertion. The model records that a roll is in progress immediately, while the number itself does not exist yet - it arrives later, as a message. Testing those two things separately is exactly right, because the runtime keeps them separate too.
Testing a command that needs a service
This is where dependency injection pays off:
describe('a command that requires a service', () => {
it('runs against a test layer - same key, different value behind it', async () => {
const [, cmd] = Users.update({ type: 'LoadRequested' }, { _tag: 'Idle' })
// `cmd` has ApiClient in its `R`. Providing the test layer discharges it.
const messages = await Effect.runPromise(
Stream.runCollect(cmd).pipe(Effect.provide(ApiClientTest([])))
)
expect(messages).toEqual([{ type: 'UsersReceived', users: [] }])
})
})Users.update returns a command whose R is ApiClient. Providing ApiClientTest discharges it. There is no mocking library involved, no module interception, and nothing to reset between tests - the substitute is a value, and the compiler checks it has the right shape.
Testing a whole program
When you want the loop itself under test, Platform.program gives you one without any rendering:
describe('a whole program', () => {
it('applies update synchronously, before dispatch returns', async () => {
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const program = yield* Platform.program(Counter.init, Counter.update)
const seen: Array<number> = []
// `subscribe` fires once with the current model, then inside every dispatch.
const stop = program.subscribe((model) => seen.push(model.count))
program.dispatch({ type: 'Increment' })
program.dispatch({ type: 'Increment' })
program.dispatch({ type: 'Decrement' })
// No awaiting, no flushing: the values are already there.
expect(seen).toEqual([0, 1, 2, 1])
stop()
yield* program.shutdown
})
)
)
})
})The assertion [0, 1, 2, 1] is worth pausing on. There is no await, no timer, no flush - and the values are already there. dispatch runs update and notifies subscribe listeners before it returns, which is what makes tea-effect safe for controlled inputs. Here it also makes the test boring, which is the point.
The complete file, imports included
import { Effect, Stream } from 'effect'
import { describe, expect, it } from 'vitest'
import * as Platform from 'tea-effect/Platform'
import * as Counter from '../counter/Counter'
import { ApiClientTest } from '../di/ApiClient'
import * as Dice from '../mental-model/Dice'
import * as Users from '../di/Users'
// #region pure
describe('update', () => {
it('is an ordinary function: call it, compare the result', () => {
const [model] = Counter.update({ type: 'Increment' }, { count: 0 })
expect(model).toEqual({ count: 1 })
})
it('needs no renderer, no mocks and nothing async', () => {
const [model] = Counter.update({ type: 'Reset' }, { count: 41 })
expect(model).toEqual({ count: 0 })
})
})
// #endregion pure
// #region cmd
describe('commands', () => {
it('a Cmd is a Stream of messages - run it to see what it produces', async () => {
const [model, cmd] = Dice.update({ type: 'Roll' }, { face: 1, rolling: false })
// The model records the roll immediately; the number is not known yet.
expect(model).toEqual({ face: 1, rolling: true })
const messages = await Effect.runPromise(Stream.runCollect(cmd))
expect(messages).toHaveLength(1)
expect(messages[0]).toMatchObject({ type: 'Rolled' })
})
})
// #endregion cmd
// #region layer
describe('a command that requires a service', () => {
it('runs against a test layer - same key, different value behind it', async () => {
const [, cmd] = Users.update({ type: 'LoadRequested' }, { _tag: 'Idle' })
// `cmd` has ApiClient in its `R`. Providing the test layer discharges it.
const messages = await Effect.runPromise(
Stream.runCollect(cmd).pipe(Effect.provide(ApiClientTest([])))
)
expect(messages).toEqual([{ type: 'UsersReceived', users: [] }])
})
})
// #endregion layer
// #region program
describe('a whole program', () => {
it('applies update synchronously, before dispatch returns', async () => {
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const program = yield* Platform.program(Counter.init, Counter.update)
const seen: Array<number> = []
// `subscribe` fires once with the current model, then inside every dispatch.
const stop = program.subscribe((model) => seen.push(model.count))
program.dispatch({ type: 'Increment' })
program.dispatch({ type: 'Increment' })
program.dispatch({ type: 'Decrement' })
// No awaiting, no flushing: the values are already there.
expect(seen).toEqual([0, 1, 2, 1])
stop()
yield* program.shutdown
})
)
)
})
})
// #endregion programWhat to test, and what not to
| Worth a test | Why |
|---|---|
update, every branch | It is where all the logic lives, and it is free to test |
| Commands, via their messages | Confirms the right effect was described |
| A service's real implementation | Once, on its own - not through every feature |
| Not worth a test | Why |
|---|---|
view output | It is a pure function of the model; assert on the model instead |
| That the runtime dispatches | That is the library's job, and the library tests it |
| Message constructors | (x) => ({ type: 'X', x }) cannot be wrong |
Setting it up in your own project
There is nothing to configure. tea-effect resolves from node_modules like any dependency, and update is a plain function:
// vitest.config.ts
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
environment: 'node'
}
})environment: 'node' is enough for everything on this page. You only need jsdom if you test Navigation or LocalStorage, which touch window.
(This repository does add a path alias, but only because its documentation examples import tea-effect/... from inside the package itself. You will not need that.)
Next
- Gotchas - the failures that are worth knowing before you hit them.