Programmatically search and replace text

I’m trying to programmatically search and replace text but it only shows the panel with the values and not replacing the text. Here’s my code below

function replace () {
    let searchQuery = new SearchQuery({search: 'except', replace: 'test'});

    replaceNext(searchQuery);

    view.dispatch({effects: setSearchQuery.of(searchQuery)});
}

These are commands and effects that are tied to the search UI panel. You can’t use them to do things programmatically. You’ll have to use SearchCursor directly to do something like that.