Skip to content
Understudy Get started

All examples

UnderstudyClock

Greets by name once the clock passes the delay

Code that sleeps takes a clock. The test passes a TestClock and advances it, so the delay passes in no real time and the test never waits.

Run it

swift test --filter greetsAfterTheDelay
The whole file on GitHub
TestCodeThatWaitsGuide.swift
@Test("Greets by name once the test clock passes the delay", .timeLimit(.minutes(1)))
func greetsAfterTheDelay() async throws {
    let clock = TestClock()
    let greeter = Greeter(clock: clock)

    async let greeting = greeter.greet("ana")
    await clock.advance(by: .milliseconds(400))

    #expect(try await greeting == "Hello, ana")
}