Skip to main content
coreydaley.dev

StoreKit tests pass in Xcode and fail in the terminal

My StoreKit 2 tests passed when I pressed ⌘U in Xcode and failed when the same xcodebuild test ran from a shell. Not all of them: every “not owned” test passed, and every “buy, then expect owned” test failed. The transactions SKTestSession created came back .unverified, never finished, and Transaction.currentEntitlements was empty.

That pattern looks exactly like a bug in the entitlement code. It isn’t. I spent a session chasing it in Purrchiver’s purchase code before I worked out that the code was fine and the shell was the problem.

The symptom

The tests use the standard setup: an SKTestSession backed by a local .storekit configuration file, a purchase made through the session, then an assertion on what the app’s store object thinks you own.

let session = try SKTestSession(configurationFileNamed: "Products")
session.resetToDefaultState()
session.disableDialogs = true
session.clearTransactions()

_ = try await session.buyProduct(identifier: productID)

let store = makeFreshStore()
await store.waitUntilResolved()
#expect(store.isUnlocked)

From Xcode: green. From a non-GUI context, the buyProduct call succeeds, but the transaction it produces is unverified, and the app (correctly) refuses to grant anything from an unverified transaction. So isUnlocked stays false. The “not owned” tests pass because “nothing is owned” is the answer you get when nothing works.

I hit it from an AI agent’s shell tool, which runs outside the logged-in user’s GUI launchd domain. A plain SSH session or a launchd-started process is outside it in the same way, so I treat them the same. The same tests passed in Xcode on the same Mac, and on a second Mac where the test runner executes inside the logged-in GUI session.

The rule

SKTestSession tests only worked for me in the logged-in GUI launchd domain. I don’t know the internal reason; the behavior was consistent enough that I stopped looking for one and wrote down the rule instead:

  • Never trust a shell xcodebuild test result for StoreKit entitlement tests unless you know the shell is in the user’s GUI session.
  • Verify with ⌘U, or on a runner that runs inside a logged-in session.
  • If your CI or agent tooling runs tests over SSH, StoreKit tests need a different path from the rest of the suite.

There’s a second, different symptom worth separating out: transactions come back unverified even in Xcode, even with a single test process. That one was a broken local StoreKit agent, not the launchd domain. What fixed it for me was quitting Xcode, deleting ~/Library/Caches/com.apple.storekitagent, and rebooting. Deleting the cache alone isn’t enough while the agent is still running; the reboot is the part that matters.

Three more traps in the same tests

Once the tests ran somewhere they could pass, three more problems turned up.

Leftover purchases in the test storefront

My tests called clearTransactions() at the start, but not at the end. The last test’s purchase stayed in the local test storefront after the run. The next ⌘U run started from a dirty state, and the next ⌘R launched the app already unlocked.

The fix is one line per test:

defer { session.clearTransactions() }

Every test now cleans up on the way out, including the ones that deliberately leave a transaction unfinished.

A shared store that resolved before the session existed

The app’s store object is a singleton that reads currentEntitlements once at launch. In a hosted unit-test bundle, “launch” is when the test host app starts, which is before any test has created its SKTestSession. So the singleton had already resolved against no session at all, and the “owned after purchase” test read stale state.

Each test now builds its own store instance, after setting up the purchase or refund it wants to test, so the instance’s first scan sees that scenario. The test-only instance also skips the long-lived Transaction.updates listener, so one test’s observer can’t carry state into the next.

Refunds propagate asynchronously

session.refundTransaction(identifier:) returns before currentEntitlements reflects the refund. A test that refunds and immediately asserts “locked” is racing. I poll with a bound instead:

try await session.refundTransaction(identifier: UInt(tx.id))
var relocked = false
for _ in 0..<50 {
    await store.reconcile()
    if !store.isUnlocked { relocked = true; break }
    try await Task.sleep(for: .milliseconds(100))
}
#expect(relocked)

Run them serially

SKTestSession state is process-wide and per-user, so two tests manipulating it at once interfere with each other. The suite is marked @Suite(.serialized) and the test target doesn’t run in parallel.

What SKTestSession can’t do

Two cases I couldn’t drive from a test at all:

  • .userCancelled. With dialogs disabled there’s no purchase sheet to cancel, so that branch of purchase() goes untested. It’s a no-op break, so I documented it in the test instead of asserting it.
  • Family Sharing. SKTestSession buys as the tester, so in my tests the transaction’s ownershipType was always .purchased. I couldn’t create a .familyShared transaction to test against.

Ask-to-Buy does work: set session.askToBuyEnabled = true and product.purchase() returns .pending, which is the path a child’s request to a parent takes.

What to take away

If a StoreKit test fails only in the “owned” direction, check where it ran before you read the code. A result that splits cleanly along “owned / not owned” is more likely to be the environment than the logic, because a real logic bug rarely breaks only the positive cases. And clean up the test storefront after each test, not just before.

Related posts