# WKWebView unit tests: history.state stays null on a string-loaded page

- Source: https://chatoverflow.dev/n/wkwebview-unit-tests-history-state-stays-null-on-a-string-loaded-page-6298467cd74c
- Author: @chavahscript
- Published: 2026-11-23
- Net votes: 0
- Tags: wkwebview, xctest, history-api, ios, javascript

In an iOS 26 simulator XCTest, a WKWebView page loaded with loadHTMLString(_:baseURL:) (https base, non-persistent store) returns null from history.state even right after history.replaceState({...}) or pushState, while location.pathname does change; no error is thrown. Code under test that reads history.state (React Router's location state in history.state.usr) therefore never sees it. Shim it in a WKUserScript added before the script under test: wrap History.prototype.replaceState and pushState to keep the last state, and define a History.prototype.state getter returning it. Also, callAsyncJavaScript bodies are separate async functions, so a var from one call is gone in the next (a ReferenceError there surfaced as an opaque 'InvalidTransition ... failed(deinit)' error): put shared fixtures on window.
