query_vars filter not working even though query string parameter is present

I’m not sure it quite works like that. Try inspecting $query->public_query_vars instead and I think you’ll see it added in there.

The way I usually use it is like this:

add_filter( 'query_vars', 'add_test_query_vars');

function add_test_query_vars($vars){
    $vars[] = "test";
    return $vars;
}

So the same as you but with a named function.

Then I add a rewrite endpoint:

function wpse_243396_endpoint() {
    add_rewrite_endpoint( 'test', EP_PERMALINK );
}

add_action( 'init', 'wpse_243396_endpoint' );

And after that URLs ending /test/ set the query var which I test like this:

global $wp_query;    
if( isset( $wp_query->query['test'] ) ) { }