Skip to content

Commit e0baf5d

Browse files
committed
With this commit we solve the jsdom setup issue causing problems in github actions. Even though configurable: true is set, redefining navigator on window or global can fail in environments where it's already non-configurable, which seems to be the case in GitHub Actions.
1 parent d857c4a commit e0baf5d

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

tests/setup/jsdomSetup.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,30 @@ import { JSDOM } from 'jsdom';
3939
*/
4040
export function setupJsdom() {
4141
const { window } = new JSDOM('<!doctype html><html><body></body></html>');
42+
4243
global.window = window;
4344
global.document = window.document;
4445

45-
// Modify the existing navigator object instead of overwriting it
46-
Object.defineProperty(window, 'navigator', {
47-
value: { userAgent: 'node.js' },
48-
writable: false,
49-
configurable: true,
50-
});
46+
// Patch the userAgent safely, even if it's readonly
47+
if (!window.navigator.userAgent || window.navigator.userAgent !== 'node.js') {
48+
try {
49+
Object.defineProperty(window.navigator, 'userAgent', {
50+
value: 'node.js',
51+
configurable: true,
52+
});
53+
} catch (e) {
54+
// Fallback if it fails due to being non-configurable
55+
console.warn('Failed to override navigator.userAgent:', e.message);
56+
}
57+
}
5158

52-
// Optionally attach window.navigator to global
53-
Object.defineProperty(global, 'navigator', {
54-
get() {
55-
return window.navigator;
56-
},
57-
});
59+
// Bind window.navigator to global (as a getter, readonly)
60+
if (!('navigator' in global)) {
61+
Object.defineProperty(global, 'navigator', {
62+
get() {
63+
return window.navigator;
64+
},
65+
configurable: true,
66+
});
67+
}
5868
}

0 commit comments

Comments
 (0)