Skip to content

Commit e8e085d

Browse files
committed
test(polyfill): Add dual-environment testing for querySelector polyfill
Enable validation of querySelector/querySelectorAll polyfill compatibility by running all existing tests in both native browser and React Native simulation environments. This ensures the codebase works correctly across web and mobile platforms. Changes: - Update @jitsi/js-utils to 2.6.7 with querySelector polyfill support - Add test-setup-polyfill.ts to override native querySelector APIs - Add karma-polyfill.conf.js for React Native simulation testing - Add npm scripts: test:native, test:polyfill for environment-specific runs - Update main test script to run both environments sequentially All 384 existing tests pass in both environments, confirming polyfill compatibility with real-world XMPP/Jingle XML processing.
1 parent c2d2fb8 commit e8e085d

File tree

4 files changed

+106
-9
lines changed

4 files changed

+106
-9
lines changed

karma-polyfill.conf.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Karma configuration for running tests with querySelector/querySelectorAll polyfill.
3+
* This configuration simulates a React Native environment where native querySelector
4+
* implementations are not available.
5+
*
6+
* The polyfill from @jitsi/js-utils is loaded before all tests via test-setup-polyfill.ts.
7+
*/
8+
9+
module.exports = function(config) {
10+
// Load base configuration.
11+
const baseConfig = require('./karma.conf.js');
12+
13+
// Create base config object.
14+
const polyfillConfig = {};
15+
16+
baseConfig(config);
17+
18+
// Get the base configuration that was set.
19+
const currentConfig = config.config || {};
20+
21+
// Extend the files array to include polyfill setup BEFORE all tests.
22+
const files = [
23+
'node_modules/core-js/index.js',
24+
'./test-setup-polyfill.ts', // Load polyfill setup first.
25+
'./modules/**/*.spec.js',
26+
'./modules/**/*.spec.ts',
27+
'./service/**/*.spec.ts',
28+
'./*.spec.ts'
29+
];
30+
31+
// Extend preprocessors to include the polyfill setup file.
32+
const preprocessors = {
33+
'./**/*.spec.js': [ 'webpack', 'sourcemap' ],
34+
'./**/*.spec.ts': [ 'webpack', 'sourcemap' ],
35+
'./test-setup-polyfill.ts': [ 'webpack', 'sourcemap' ],
36+
'node_modules/core-js/**': [ 'webpack' ]
37+
};
38+
39+
// Customize browser name for clarity in output.
40+
const customLaunchers = {
41+
ChromeHeadlessPolyfill: {
42+
base: 'ChromeHeadless',
43+
displayName: 'Chrome Headless (Polyfill)'
44+
}
45+
};
46+
47+
// Update configuration with polyfill-specific settings.
48+
config.set({
49+
files,
50+
preprocessors,
51+
customLaunchers,
52+
browsers: [ 'ChromeHeadlessPolyfill' ]
53+
});
54+
};

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"author": "",
1717
"readmeFilename": "README.md",
1818
"dependencies": {
19-
"@jitsi/js-utils": "2.4.6",
19+
"@jitsi/js-utils": "^2.6.7",
2020
"@jitsi/logger": "2.1.1",
2121
"@jitsi/precall-test": "1.0.6",
2222
"@jitsi/rtcstats": "9.7.0",
@@ -70,7 +70,9 @@
7070
"lint": "eslint . --max-warnings 0 && tsc --noEmit",
7171
"lint-fix": "eslint . --max-warnings 0 --fix",
7272
"prepack": "npm run build && npm run gen-types",
73-
"test": "karma start karma.conf.js",
73+
"test": "npm run test:native && npm run test:polyfill",
74+
"test:native": "karma start karma.conf.js",
75+
"test:polyfill": "karma start karma-polyfill.conf.js",
7476
"test-watch": "karma start karma.conf.js --no-single-run",
7577
"type-check": "tsc --noEmit",
7678
"typedoc": "npm run gen-version && typedoc",

test-setup-polyfill.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Test setup file that overrides native querySelector/querySelectorAll implementations
3+
* with the polyfill from @jitsi/js-utils. This simulates a React Native environment
4+
* where native querySelector is not available.
5+
*
6+
* This file is loaded by karma-polyfill.conf.js BEFORE any test specs, ensuring all
7+
* tests run with the polyfill active.
8+
*/
9+
10+
import { querySelector, querySelectorAll } from '@jitsi/js-utils/polyfills';
11+
12+
/**
13+
* Override Element.prototype methods with polyfill implementations.
14+
*/
15+
Element.prototype.querySelector = function(selectors: string): Element | null {
16+
return querySelector(this, selectors);
17+
};
18+
19+
Element.prototype.querySelectorAll = function(selectors: string): NodeListOf<Element> {
20+
const results = querySelectorAll(this, selectors);
21+
22+
// Convert array to NodeListOf<Element> for API compatibility.
23+
return results as unknown as NodeListOf<Element>;
24+
};
25+
26+
/**
27+
* Override Document.prototype methods with polyfill implementations.
28+
*/
29+
Document.prototype.querySelector = function(selectors: string): Element | null {
30+
return querySelector(this, selectors);
31+
};
32+
33+
Document.prototype.querySelectorAll = function(selectors: string): NodeListOf<Element> {
34+
const results = querySelectorAll(this, selectors);
35+
36+
return results as unknown as NodeListOf<Element>;
37+
};
38+
39+
// Log confirmation that polyfill is active.
40+
console.log('📱 querySelector/querySelectorAll polyfill is ACTIVE (React Native simulation mode)');
41+
console.log(' All tests will use the polyfill implementation from @jitsi/js-utils');

0 commit comments

Comments
 (0)