This looks very similar to the problem I encountered a few days ago. However, surprisingly, it was actually not a bug, but a feature. This is the devtools feature, that called "Emulate a focused page", and while it is active the page does not lose focus and, as a result, does not receive the expected events. To be clear, It is very useful for some situation, but has some caveats clearly noted:
Caution: With this option turned on, the document.visibilityState is set to visible and the visibilitychange event doesn't fire. For more information, see Page Visibility API.