Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

fix(tests): register custom expect matchers from the before hook

damencho8a5264f4ce9f
diff --git a/tests/helpers/TestProperties.ts b/tests/helpers/TestProperties.ts
index 80a5d8d704..b7d1a758ea 100644
--- a/tests/helpers/TestProperties.ts
+++ b/tests/helpers/TestProperties.ts
@@ -74,9 +74,7 @@ export function loadTestFiles(files: string[]): void {
// do nothing
};
- // helpers/matchers.ts calls expect.extend({...}) at module load to
- // register a custom matcher; without a stub for .extend the require()
- // below would throw and we'd skip the spec's setTestProperties calls.
+ // Tolerate any expect.extend({...}) calls a spec or helper might do at module load.
if (fn === 'expect') {
stub.extend = () => { /* no-op */ };
}
diff --git a/tests/helpers/matchers.ts b/tests/helpers/matchers.ts
index c7a1843259..064eb82397 100644
--- a/tests/helpers/matchers.ts
+++ b/tests/helpers/matchers.ts
@@ -1,37 +1,42 @@
-expect.extend({
- toPartiallyMatch(received: string, other: string, receivedLabel = 'received', otherLabel = 'other') {
- const pass = received.includes(other) || other.includes(received);
+// Register custom matchers on the global expect set up by @wdio/runner during _initSession.
+// Must be called from a wdio config hook that runs after _initSession (e.g. `before`); calling
+// at module load extends a stale `globalThis.expect` that gets replaced before tests run.
+export function registerCustomMatchers(): void {
+ expect.extend({
+ toPartiallyMatch(received: string, other: string, receivedLabel = 'received', otherLabel = 'other') {
+ const pass = received.includes(other) || other.includes(received);
- return {
- pass,
- message: () => pass
- ? `Expected ${receivedLabel} NOT to partially match ${otherLabel}.\n ${receivedLabel}: "${received}"\n ${otherLabel}: "${other}"`
- : `Expected ${receivedLabel} to partially match ${otherLabel} (one should contain the other).\n ${receivedLabel}: "${received}"\n ${otherLabel}: "${other}"`
- };
- },
+ return {
+ pass,
+ message: () => pass
+ ? `Expected ${receivedLabel} NOT to partially match ${otherLabel}.\n ${receivedLabel}: "${received}"\n ${otherLabel}: "${other}"`
+ : `Expected ${receivedLabel} to partially match ${otherLabel} (one should contain the other).\n ${receivedLabel}: "${received}"\n ${otherLabel}: "${other}"`
+ };
+ },
- toStartWith(received: string, prefix: string, label = 'value') {
- const pass = received.startsWith(prefix);
+ toStartWith(received: string, prefix: string, label = 'value') {
+ const pass = received.startsWith(prefix);
- return {
- pass,
- message: () => pass
- ? `Expected ${label} NOT to start with "${prefix}".\n ${label}: "${received}"`
- : `Expected ${label} to start with "${prefix}".\n ${label}: "${received}"`
- };
- },
+ return {
+ pass,
+ message: () => pass
+ ? `Expected ${label} NOT to start with "${prefix}".\n ${label}: "${received}"`
+ : `Expected ${label} to start with "${prefix}".\n ${label}: "${received}"`
+ };
+ },
- toBeInteger(received: unknown, label = 'value') {
- const pass = Number.isInteger(received);
+ toBeInteger(received: unknown, label = 'value') {
+ const pass = Number.isInteger(received);
- return {
- pass,
- message: () => pass
- ? `Expected ${label} NOT to be an integer.\n ${label}: ${JSON.stringify(received)}`
- : `Expected ${label} to be an integer.\n ${label}: ${JSON.stringify(received)}`
- };
- }
-});
+ return {
+ pass,
+ message: () => pass
+ ? `Expected ${label} NOT to be an integer.\n ${label}: ${JSON.stringify(received)}`
+ : `Expected ${label} to be an integer.\n ${label}: ${JSON.stringify(received)}`
+ };
+ }
+ });
+}
declare module 'expect' {
// eslint-disable-next-line @typescript-eslint/naming-convention
diff --git a/tests/specs/jaas/dial/dialin.spec.ts b/tests/specs/jaas/dial/dialin.spec.ts
index 7242372507..06b91a364a 100644
--- a/tests/specs/jaas/dial/dialin.spec.ts
+++ b/tests/specs/jaas/dial/dialin.spec.ts
@@ -1,4 +1,3 @@
-import '../../../helpers/matchers';
import type { Participant } from '../../../helpers/Participant';
import { setTestProperties } from '../../../helpers/TestProperties';
import { config as testsConfig } from '../../../helpers/TestsConfig';
diff --git a/tests/specs/jaas/recording.spec.ts b/tests/specs/jaas/recording.spec.ts
index 3a9558d55f..f223445202 100644
--- a/tests/specs/jaas/recording.spec.ts
+++ b/tests/specs/jaas/recording.spec.ts
@@ -1,4 +1,3 @@
-import '../../helpers/matchers';
import { Participant } from '../../helpers/Participant';
import { setTestProperties } from '../../helpers/TestProperties';
import { config as testsConfig } from '../../helpers/TestsConfig';
diff --git a/tests/specs/jaas/transcriptions.spec.ts b/tests/specs/jaas/transcriptions.spec.ts
index 74b0cdd770..b9f05dcfcd 100644
--- a/tests/specs/jaas/transcriptions.spec.ts
+++ b/tests/specs/jaas/transcriptions.spec.ts
@@ -1,4 +1,3 @@
-import '../../helpers/matchers';
import type { Participant } from '../../helpers/Participant';
import { setTestProperties } from '../../helpers/TestProperties';
import type WebhookProxy from '../../helpers/WebhookProxy';
diff --git a/tests/specs/misc/connectivity.spec.ts b/tests/specs/misc/connectivity.spec.ts
index 16a4e3b04a..d8a0602073 100644
--- a/tests/specs/misc/connectivity.spec.ts
+++ b/tests/specs/misc/connectivity.spec.ts
@@ -1,4 +1,3 @@
-import '../../helpers/matchers';
import type { Participant } from '../../helpers/Participant';
import { setTestProperties } from '../../helpers/TestProperties';
import { config as testsConfig } from '../../helpers/TestsConfig';
diff --git a/tests/specs/moderation/lobby.spec.ts b/tests/specs/moderation/lobby.spec.ts
index 8246a719c6..2bc4c3dc27 100644
--- a/tests/specs/moderation/lobby.spec.ts
+++ b/tests/specs/moderation/lobby.spec.ts
@@ -1,4 +1,3 @@
-import '../../helpers/matchers';
import { P1, P3, Participant } from '../../helpers/Participant';
import { setTestProperties } from '../../helpers/TestProperties';
import { expectations } from '../../helpers/expectations';
diff --git a/tests/wdio.conf.ts b/tests/wdio.conf.ts
index 7c4a19d46b..5971cb8cd1 100644
--- a/tests/wdio.conf.ts
+++ b/tests/wdio.conf.ts
@@ -12,6 +12,7 @@ import { getTestProperties, loadTestFiles } from './helpers/TestProperties';
import { config as testsConfig } from './helpers/TestsConfig';
import WebhookProxy from './helpers/WebhookProxy';
import { getLogs, initLogger, logInfo, saveLogs } from './helpers/browserLogger';
+import { registerCustomMatchers } from './helpers/matchers';
import { IContext } from './helpers/types';
import { generateRoomName } from './helpers/utils';
@@ -303,6 +304,11 @@ export const config: WebdriverIO.MultiremoteConfig = {
* @returns {Promise<void>}
*/
async before(cid, _, files) {
+ // Register custom matchers here (not at matchers.ts module load): @wdio/runner replaces
+ // globalThis.expect via _setGlobal AFTER mocha has already loaded the spec files, so any
+ // expect.extend at module load runs against an expect that's about to be discarded.
+ registerCustomMatchers();
+
if (files.length !== 1) {
console.warn('We expect to run a single suite, but got more than one');
}