Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Karma + Mocha with Jest #30

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"env": {
"browser": true,
"es6": true,
"mocha": true
"jest/globals": true,
"node": true
havardholvik marked this conversation as resolved.
Show resolved Hide resolved
},
"extends": ["@whereby/eslint-config/browser", "prettier"],
"parserOptions": {
"sourceType": "module",
"requireConfigFile": false
},
"plugins": ["mocha-no-only", "prettier"],
"plugins": ["prettier", "jest"],
"rules": {
"no-console": ["error", { "allow": ["warn", "error"] }],
"prettier/prettier": "error"
Expand Down
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: [["@babel/preset-env", { targets: { node: "current" } }]],
};
83 changes: 0 additions & 83 deletions config/karma.conf.js

This file was deleted.

5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
testEnvironment: "jsdom",
testMatch: ["<rootDir>/tests/**/?(*.)+(spec|test).[jt]s?(x)"],
roots: ["<rootDir>"],
};
40 changes: 13 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@whereby/jslib-media",
"description": "Media library for Whereby",
"version": "1.3.5",
"version": "1.3.6",
"private": false,
"license": "MIT",
"homepage": "https://github.com/whereby/jslib-media",
Expand All @@ -13,9 +13,9 @@
"url": "https://github.com/whereby/jslib-media/issues"
},
"scripts": {
"lint": "eslint src tests config",
"lint": "eslint src tests",
"test": "npm run lint && npm run test:unit",
"test:unit": "karma start config/karma.conf.js",
"test:unit": "jest",
"format:fix": "prettier --write './**/*.{js,ts,yml,json}'",
"format:check": "prettier --check './**/*.{js,ts,yml,json}'"
},
Expand All @@ -31,39 +31,25 @@
"webrtc-adapter": "^7.3.0"
},
"devDependencies": {
"@babel/core": "^7.17.5",
"@babel/core": "^7.23.2",
"@babel/eslint-parser": "^7.22.15",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.13.8",
"@babel/plugin-proposal-optional-chaining": "^7.13.12",
"@babel/preset-env": "^7.12.13",
"@babel/preset-env": "^7.23.2",
"@whereby/auto-config": "0.0.2",
"@whereby/eslint-config": "5.0.4",
"babel-loader": "^9.1.3",
"babel-plugin-rewire-exports": "^2.3.0",
"chai": "^4.0.2",
"chai-as-promised": "^7.1.1",
"dirty-chai": "^2.0.0",
"babel-jest": "^29.7.0",
"eslint": "^8.49.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-mocha-no-only": "^1.1.1",
"eslint-plugin-jest": "^27.6.0",
"eslint-plugin-prettier": "^5.0.0",
"i18n-iso-countries": "^7.7.0",
"istanbul-instrumenter-loader": "^3.0.1",
"karma": "^6.1.0",
"karma-babel-preprocessor": "^8.0.1",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage-istanbul-reporter": "^3.0.3",
"karma-mocha": "^2.0.1",
"karma-webpack": "^5.0.0",
"libphonenumber-js": "^1.9.7",
"mocha": "^10.2.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"prettier": "3.0.3",
"process": "^0.11.10",
"sinon": "^16.0.0",
"sinon-chai": "^3.7.0",
"webpack": "^5.69.1"
"process": "^0.11.10"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com"
},
"engines": {
"node": ">=16.0.0"
}
}
26 changes: 13 additions & 13 deletions src/webrtc/BaseRtcManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { MAXIMUM_TURN_BANDWIDTH, MAXIMUM_TURN_BANDWIDTH_UNLIMITED, MEDIA_JITTER_
import adapter from "webrtc-adapter";

const CAMERA_STREAM_ID = RtcStream.getCameraId();
const logger = console;
const browserName = adapter.browserDetails.browser;
const browserVersion = adapter.browserDetails.version;

Expand All @@ -27,7 +26,7 @@ if (browserName === "chrome") {
}

export default class BaseRtcManager {
constructor({ selfId, room, emitter, serverSocket, webrtcProvider, features }) {
constructor({ selfId, room, emitter, serverSocket, webrtcProvider, features, logger = console }) {
assert.ok(selfId, "selfId is required");
assert.ok(room, "room is required");
assert.ok(emitter && emitter.emit, "emitter is required");
Expand All @@ -48,6 +47,7 @@ export default class BaseRtcManager {
this._serverSocket = serverSocket;
this._webrtcProvider = webrtcProvider;
this._features = features || {};
this._logger = logger;

this.offerOptions = { offerToReceiveAudio: true, offerToReceiveVideo: true };
this._pendingActionsForConnectedPeerConnections = [];
Expand Down Expand Up @@ -116,7 +116,7 @@ export default class BaseRtcManager {
receiver.playoutDelayHint = MEDIA_JITTER_BUFFER_TARGET / 1000; // seconds
});
} catch (error) {
logger.error("Error during setting jitter buffer target:", error);
this._logger.error("Error during setting jitter buffer target:", error);
}
}

Expand Down Expand Up @@ -428,7 +428,7 @@ export default class BaseRtcManager {
_cleanup(peerConnectionId) {
const session = this._getSession(peerConnectionId);
if (!session) {
logger.warn("No RTCPeerConnection in RTCManager.disconnect()", peerConnectionId);
this._logger.warn("No RTCPeerConnection in RTCManager.disconnect()", peerConnectionId);
return;
}
session.close();
Expand Down Expand Up @@ -458,10 +458,10 @@ export default class BaseRtcManager {
const promises = [];
this._forEachPeerConnection((session) => {
if (!session.hasConnectedPeerConnection()) {
logger.log("Session doesn't have a connected PeerConnection, adding pending action!");
this._logger.log("Session doesn't have a connected PeerConnection, adding pending action!");
const pendingActions = this._pendingActionsForConnectedPeerConnections;
if (!pendingActions) {
logger.warn(
this._logger.warn(
`No pending action is created to repalce track, because the pending actions array is null`
);
return;
Expand All @@ -470,7 +470,7 @@ export default class BaseRtcManager {
const action = () => {
const replacedTrackPromise = session.replaceTrack(oldTrack, newTrack);
if (!replacedTrackPromise) {
logger.error("replaceTrack returned false!");
this._logger.error("replaceTrack returned false!");
reject(`ReplaceTrack returned false`);
return;
}
Expand All @@ -483,7 +483,7 @@ export default class BaseRtcManager {
}
const replacedTrackResult = session.replaceTrack(oldTrack, newTrack);
if (!replacedTrackResult) {
logger.error("replaceTrack returned false!");
this._logger.error("replaceTrack returned false!");
return;
}
promises.push(replacedTrackResult);
Expand Down Expand Up @@ -651,7 +651,7 @@ export default class BaseRtcManager {

this._serverSocket.on(PROTOCOL_RESPONSES.MEDIASERVER_CONFIG, (data) => {
if (data.error) {
logger.warn("FETCH_MEDIASERVER_CONFIG failed:", data.error);
this._logger.warn("FETCH_MEDIASERVER_CONFIG failed:", data.error);
return;
}
this._updateAndScheduleMediaServersRefresh(data);
Expand All @@ -664,7 +664,7 @@ export default class BaseRtcManager {
this._serverSocket.on(RELAY_MESSAGES.ICE_CANDIDATE, (data) => {
const session = this._getSession(data.clientId);
if (!session) {
logger.warn("No RTCPeerConnection on ICE_CANDIDATE", data);
this._logger.warn("No RTCPeerConnection on ICE_CANDIDATE", data);
return;
}
session.addIceCandidate(data.message);
Expand All @@ -673,7 +673,7 @@ export default class BaseRtcManager {
this._serverSocket.on(RELAY_MESSAGES.ICE_END_OF_CANDIDATES, (data) => {
const session = this._getSession(data.clientId);
if (!session) {
logger.warn("No RTCPeerConnection on ICE_END_OF_CANDIDATES", data);
this._logger.warn("No RTCPeerConnection on ICE_END_OF_CANDIDATES", data);
return;
}
session.addIceCandidate(null);
Expand All @@ -683,7 +683,7 @@ export default class BaseRtcManager {
this._serverSocket.on(RELAY_MESSAGES.SDP_OFFER, (data) => {
const session = this._getSession(data.clientId);
if (!session) {
logger.warn("No RTCPeerConnection on SDP_OFFER", data);
this._logger.warn("No RTCPeerConnection on SDP_OFFER", data);
return;
}
const offer = this._transformIncomingSdp(data.message, session.pc);
Expand All @@ -699,7 +699,7 @@ export default class BaseRtcManager {
this._serverSocket.on(RELAY_MESSAGES.SDP_ANSWER, (data) => {
const session = this._getSession(data.clientId);
if (!session) {
logger.warn("No RTCPeerConnection on SDP_ANSWER", data);
this._logger.warn("No RTCPeerConnection on SDP_ANSWER", data);
return;
}
const answer = this._transformIncomingSdp(data.message, session.pc);
Expand Down
13 changes: 6 additions & 7 deletions src/webrtc/P2pRtcManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { getOptimalBitrate } from "../utils/optimalBitrate";
import { setCodecPreferenceSDP } from "./sdpModifier";
import adapter from "webrtc-adapter";

const logger = console;
const CAMERA_STREAM_ID = RtcStream.getCameraId();
const browserName = adapter.browserDetails.browser;

Expand All @@ -17,7 +16,7 @@ export default class P2pRtcManager extends BaseRtcManager {
let session = this._getSession(clientId);
let bandwidth = (session && session.bandwidth) || 0;
if (session) {
logger.warn("Replacing peer session", clientId);
this._logger.warn("Replacing peer session", clientId);
this._cleanup(clientId);
} else {
bandwidth = this._changeBandwidthForAllClients(true);
Expand Down Expand Up @@ -92,13 +91,13 @@ export default class P2pRtcManager extends BaseRtcManager {
videoTransceiver.setCodecPreferences(capabilities.codecs);
});
} catch (error) {
logger.error("Error during setting setCodecPreferences:", error);
this._logger.error("Error during setting setCodecPreferences:", error);
}
}

_negotiatePeerConnection(clientId, session, constraints) {
if (!session) {
logger.warn("No RTCPeerConnection in negotiatePeerConnection()", clientId);
this._logger.warn("No RTCPeerConnection in negotiatePeerConnection()", clientId);
return;
}
const pc = session.pc;
Expand Down Expand Up @@ -140,11 +139,11 @@ export default class P2pRtcManager extends BaseRtcManager {
return;
}
pc.setLocalDescription(offer).catch((e) => {
logger.warn("RTCPeerConnection.setLocalDescription() failed with local offer", e);
this._logger.warn("RTCPeerConnection.setLocalDescription() failed with local offer", e);
});
})
.catch((e) => {
logger.warn("RTCPeerConnection.createOffer() failed to create local offer", e);
this._logger.warn("RTCPeerConnection.createOffer() failed to create local offer", e);
});
}

Expand Down Expand Up @@ -283,7 +282,7 @@ export default class P2pRtcManager extends BaseRtcManager {
if (session) {
// this will happen on a signal-server reconnect
// before we tried an ice-restart here, now we recreate the session/pc
logger.warn("Replacing peer session", clientId);
this._logger.warn("Replacing peer session", clientId);
this._cleanup(clientId); // will cleanup and delete session/pc
} else {
// we adjust bandwidth based on number of sessions/pcs
Expand Down
Loading