Skip to content

Commit 3b61954

Browse files
committed
Use jest in favour of Karma + Mocha
To align with our other repos, replace the ageing Karma + Mocha setup in favour of Jest.
1 parent 9b2215f commit 3b61954

16 files changed

+1878
-1954
lines changed

.eslintrc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
22
"parser": "@babel/eslint-parser",
33
"env": {
4+
"jest/globals": true,
45
"browser": true,
5-
"es6": true,
6-
"mocha": true
6+
"es6": true
77
},
88
"extends": ["@whereby/eslint-config/browser", "prettier"],
99
"parserOptions": {
1010
"sourceType": "module",
1111
"requireConfigFile": false
1212
},
13-
"plugins": ["mocha-no-only", "prettier"],
13+
"plugins": ["prettier", "jest"],
1414
"rules": {
1515
"no-console": ["error", { "allow": ["warn", "error"] }],
1616
"prettier/prettier": "error"

babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: [["@babel/preset-env", { targets: { node: "current" } }]],
3+
};

jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
testEnvironment: "jsdom",
3+
testMatch: ["<rootDir>/tests/**/?(*.)+(spec|test).[jt]s?(x)"],
4+
roots: ["<rootDir>"],
5+
};

package.json

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@whereby/jslib-media",
33
"description": "Media library for Whereby",
4-
"version": "1.3.5",
4+
"version": "1.3.6",
55
"private": false,
66
"license": "MIT",
77
"homepage": "https://github.com/whereby/jslib-media",
@@ -15,7 +15,7 @@
1515
"scripts": {
1616
"lint": "eslint src tests config",
1717
"test": "npm run lint && npm run test:unit",
18-
"test:unit": "karma start config/karma.conf.js",
18+
"test:unit": "jest",
1919
"format:fix": "prettier --write './**/*.{js,ts,yml,json}'",
2020
"format:check": "prettier --check './**/*.{js,ts,yml,json}'"
2121
},
@@ -31,39 +31,26 @@
3131
"webrtc-adapter": "^7.3.0"
3232
},
3333
"devDependencies": {
34-
"@babel/core": "^7.17.5",
34+
"@babel/core": "^7.23.2",
3535
"@babel/eslint-parser": "^7.22.15",
36-
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.13.8",
37-
"@babel/plugin-proposal-optional-chaining": "^7.13.12",
38-
"@babel/preset-env": "^7.12.13",
36+
"@babel/preset-env": "^7.23.2",
3937
"@whereby/auto-config": "0.0.2",
4038
"@whereby/eslint-config": "5.0.4",
41-
"babel-loader": "^9.1.3",
42-
"babel-plugin-rewire-exports": "^2.3.0",
43-
"chai": "^4.0.2",
44-
"chai-as-promised": "^7.1.1",
45-
"dirty-chai": "^2.0.0",
39+
"babel-jest": "^29.7.0",
4640
"eslint": "^8.49.0",
4741
"eslint-config-prettier": "^9.0.0",
42+
"eslint-plugin-jest": "^27.6.0",
4843
"eslint-plugin-mocha-no-only": "^1.1.1",
4944
"eslint-plugin-prettier": "^5.0.0",
50-
"i18n-iso-countries": "^7.7.0",
51-
"istanbul-instrumenter-loader": "^3.0.1",
52-
"karma": "^6.1.0",
53-
"karma-babel-preprocessor": "^8.0.1",
54-
"karma-chrome-launcher": "^3.1.0",
55-
"karma-coverage-istanbul-reporter": "^3.0.3",
56-
"karma-mocha": "^2.0.1",
57-
"karma-webpack": "^5.0.0",
58-
"libphonenumber-js": "^1.9.7",
59-
"mocha": "^10.2.0",
45+
"jest": "^29.7.0",
46+
"jest-environment-jsdom": "^29.7.0",
6047
"prettier": "3.0.3",
61-
"process": "^0.11.10",
62-
"sinon": "^16.0.0",
63-
"sinon-chai": "^3.7.0",
64-
"webpack": "^5.69.1"
48+
"process": "^0.11.10"
6549
},
6650
"publishConfig": {
6751
"registry": "https://npm.pkg.github.com"
52+
},
53+
"engines": {
54+
"node": ">=16.0.0"
6855
}
6956
}

tests/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function itShouldThrowIfMissing(missingPropertyName, fn) {
1212
it(`should throw if ${scenario}`, () => {
1313
expect(() => {
1414
fn();
15-
}).to.throw(expectedException);
15+
}).toThrow(expectedException);
1616
});
1717
}
1818

tests/model/RtcStream.spec.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,34 @@ describe("RtcStream", () => {
55
it("should throw error if id is null", () => {
66
expect(() => {
77
new RtcStream(null, STREAM_TYPES.SCREEN_SHARE); //eslint-disable-line no-new
8-
}).to.throw("id is required");
8+
}).toThrow("id is required");
99
});
1010

1111
it("should throw error if id is undefined", () => {
1212
expect(() => {
1313
new RtcStream(undefined, STREAM_TYPES.SCREEN_SHARE); //eslint-disable-line no-new
14-
}).to.throw("id is required");
14+
}).toThrow("id is required");
1515
});
1616

1717
it("should throw error if type is missing", () => {
1818
expect(() => {
1919
new RtcStream("1"); //eslint-disable-line no-new
20-
}).to.throw("type is required");
20+
}).toThrow("type is required");
2121
});
2222

2323
it("should create a stream with a string id if the specified id is a number", () => {
2424
const stream = new RtcStream(1, STREAM_TYPES.SCREEN_SHARE);
2525

26-
expect(stream.id).to.deep.equals("1");
26+
expect(stream.id).toEqual("1");
2727
});
2828

2929
it("should create a stream with the specified id and type", () => {
3030
const streamId = "1";
3131
const streamType = STREAM_TYPES.SCREEN_SHARE;
3232
const stream = new RtcStream(streamId, streamType);
3333

34-
expect(stream.id).to.deep.equals(streamId);
35-
expect(stream.type).to.deep.equals(streamType);
34+
expect(stream.id).toEqual(streamId);
35+
expect(stream.type).toEqual(streamType);
3636
});
3737
});
3838

@@ -42,29 +42,29 @@ describe("RtcStream", () => {
4242

4343
beforeEach(() => {
4444
fakeStream = {
45-
getVideoTracks: sinon.stub().returns([{ enabled: true }]),
46-
getAudioTracks: sinon.stub().returns([{ enabled: true }]),
45+
getVideoTracks: jest.fn().mockReturnValue([{ enabled: true }]),
46+
getAudioTracks: jest.fn().mockReturnValue([{ enabled: true }]),
4747
};
4848
});
4949

5050
it("applies isVideoEnabled state override from earlier", () => {
5151
const stream = new RtcStream("id", "type");
5252
stream.isVideoEnabled = false;
53-
sinon.spy(stream, "setVideoEnabled");
53+
jest.spyOn(stream, "setVideoEnabled");
5454

5555
stream.setup(fakeStream);
5656

57-
expect(stream.setVideoEnabled).to.have.been.calledWithExactly(false);
57+
expect(stream.setVideoEnabled).toHaveBeenCalledWith(false);
5858
});
5959

6060
it("applies isAudioEnabled state override from earlier", () => {
6161
const stream = new RtcStream("id", "type");
6262
stream.isAudioEnabled = false;
63-
sinon.spy(stream, "setAudioEnabled");
63+
jest.spyOn(stream, "setAudioEnabled");
6464

6565
stream.setup(fakeStream);
6666

67-
expect(stream.setAudioEnabled).to.have.been.calledWithExactly(false);
67+
expect(stream.setAudioEnabled).toHaveBeenCalledWith(false);
6868
});
6969
});
7070
});
@@ -74,31 +74,31 @@ describe("RtcStream", () => {
7474
beforeEach(() => {
7575
stream = new RtcStream("id", STREAM_TYPES.CAMERA);
7676
stream.stream = {
77-
getAudioTracks: sinon.stub().returns([]),
78-
getVideoTracks: sinon.stub().returns([]),
77+
getAudioTracks: jest.fn().mockReturnValue([]),
78+
getVideoTracks: jest.fn().mockReturnValue([]),
7979
};
8080
});
8181

8282
it("defaults to being true", () => {
83-
expect(stream.isAudioEnabled).to.equal(true);
83+
expect(stream.isAudioEnabled).toEqual(true);
8484
});
8585

8686
it("sets isAudioEnabled to the given value", () => {
8787
const expectedValue = false;
8888

8989
stream.setAudioEnabled(expectedValue);
9090

91-
expect(stream.isAudioEnabled).to.equal(expectedValue);
91+
expect(stream.isAudioEnabled).toEqual(expectedValue);
9292
});
9393

9494
it("sets each stream's track' enabled property", () => {
9595
const track = {};
96-
stream.stream.getAudioTracks = sinon.stub().returns([track]);
96+
stream.stream.getAudioTracks = jest.fn().mockReturnValue([track]);
9797

9898
const expectedValue = true;
9999
stream.setAudioEnabled(expectedValue);
100100

101-
expect(track.enabled).to.equal(expectedValue);
101+
expect(track.enabled).toEqual(expectedValue);
102102
});
103103
});
104104

@@ -107,31 +107,31 @@ describe("RtcStream", () => {
107107
beforeEach(() => {
108108
stream = new RtcStream("id", STREAM_TYPES.CAMERA);
109109
stream.stream = {
110-
getAudioTracks: sinon.stub().returns([]),
111-
getVideoTracks: sinon.stub().returns([]),
110+
getAudioTracks: jest.fn().mockReturnValue([]),
111+
getVideoTracks: jest.fn().mockReturnValue([]),
112112
};
113113
});
114114

115115
it("defaults to being true", () => {
116-
expect(stream.isVideoEnabled).to.equal(true);
116+
expect(stream.isVideoEnabled).toEqual(true);
117117
});
118118

119119
it("sets isVideoEnabled to the given value", () => {
120120
const expectedValue = false;
121121

122122
stream.setVideoEnabled(expectedValue);
123123

124-
expect(stream.isVideoEnabled).to.equal(expectedValue);
124+
expect(stream.isVideoEnabled).toEqual(expectedValue);
125125
});
126126

127127
it("sets each stream's track' enabled property", () => {
128128
const track = {};
129-
stream.stream.getVideoTracks = sinon.stub().returns([track]);
129+
stream.stream.getVideoTracks = jest.fn().mockReturnValue([track]);
130130

131131
const expectedValue = true;
132132
stream.setVideoEnabled(expectedValue);
133133

134-
expect(track.enabled).to.equal(expectedValue);
134+
expect(track.enabled).toEqual(expectedValue);
135135
});
136136
});
137137
});

tests/utils/ServerSocket.spec.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,37 @@ describe("ServerSocket", () => {
66

77
beforeEach(() => {
88
serverSocket = new ServerSocket("https://localhost");
9-
sinon.stub(serverSocket._socket, "on");
10-
sinon.stub(serverSocket._socket, "off");
9+
jest.spyOn(serverSocket._socket, "on");
10+
jest.spyOn(serverSocket._socket, "off");
1111
});
1212

1313
afterEach(() => {
14-
serverSocket._socket.on.restore();
15-
serverSocket._socket.off.restore();
14+
jest.clearAllMocks();
1615
});
1716

1817
it("should attach an event listener to the internal socket", () => {
1918
const eventName = "some event";
20-
const handler = sinon.stub();
19+
const handler = jest.fn();
2120

2221
serverSocket.on(eventName, handler);
2322

24-
expect(serverSocket._socket.on).to.be.calledWithExactly(eventName, handler);
23+
expect(serverSocket._socket.on).toHaveBeenCalledWith(eventName, handler);
2524
});
2625

2726
it("should return a deregistering function", () => {
2827
const deregisterHandler = serverSocket.on("event", () => {});
2928

30-
expect(deregisterHandler).to.be.instanceOf(Function);
29+
expect(deregisterHandler).toBeInstanceOf(Function);
3130
});
3231

3332
it("should return a deregistering function which will remove the listener", () => {
3433
const eventName = "some event";
35-
const handler = sinon.stub();
34+
const handler = jest.fn();
3635

3736
const deregisterHandler = serverSocket.on(eventName, handler);
3837
deregisterHandler();
3938

40-
expect(serverSocket._socket.off).to.be.calledWithExactly(eventName, handler);
39+
expect(serverSocket._socket.off).toHaveBeenCalledWith(eventName, handler);
4140
});
4241
});
4342
});

0 commit comments

Comments
 (0)