-
Notifications
You must be signed in to change notification settings - Fork 0
Testing
Husnain Ali edited this page Sep 27, 2026
·
1 revision
Use an in-memory store in unit tests and SwiftUI previews.
inMemory keeps everything in memory and needs no cleanup, so each
test can create its own storage:
@Test func savesUser() async throws {
let storage = try LocalStorage(configuration: .inMemory)
try await storage.save(User(id: 1, name: "Ada"))
#expect(try await storage.fetch(User.self, id: 1)?.name == "Ada")
}To test expiration without waiting, the testing SPI lets you inject a clock:
@_spi(SwiftLocalStorageTesting) import SwiftLocalStorage
final class Clock: @unchecked Sendable { var now = Date() }
let clock = Clock()
let storage = try LocalStorage(configuration: .inMemory, now: { clock.now })
try await storage.save(user, expiration: .hours(1))
clock.now += 2 * 3_600 // two hours later
#expect(try await storage.fetch(User.self, id: user.id) == nil)SPI declarations are for tests only and aren't covered by the stability promise in API Stability.
SwiftLocalStorage, licensed under Apache 2.0. These pages cover v1.1.1.