Skip to content

Commit 28c9f94

Browse files
authored
Merge pull request #1138 from webrtcHacks/jest
Move unit tests to jest
2 parents 873a68d + d326c5c commit 28c9f94

13 files changed

+218
-258
lines changed

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"prepare": "grunt build",
2222
"prepublishonly": "npm test",
2323
"test": "grunt && mocha test/unit && karma start test/karma.conf.js",
24-
"lint-and-unit-tests": "grunt && mocha test/unit",
24+
"lint-and-unit-tests": "grunt && jest test/unit",
2525
"e2e-tests": "grunt && karma start test/karma.conf.js"
2626
},
2727
"dependencies": {
@@ -37,14 +37,15 @@
3737
"@puppeteer/browsers": "^1.4.1",
3838
"babel-preset-env": "^1.7.0",
3939
"brfs": "^1.5.0",
40-
"chai": "^3.5.0",
40+
"eslint-plugin-jest": "^27.4.0",
4141
"grunt": "^1.1.0",
4242
"grunt-babel": "^8.0.0",
4343
"grunt-browserify": "^6.0.0",
4444
"grunt-cli": "^1.3.1",
4545
"grunt-contrib-clean": "^1.1.0",
4646
"grunt-contrib-copy": "^1.0.0",
4747
"grunt-eslint": "^24.0.0",
48+
"jest": "^29.7.0",
4849
"karma": "^6.4.1",
4950
"karma-browserify": "^8.1.0",
5051
"karma-chai": "^0.1.0",
@@ -54,8 +55,6 @@
5455
"karma-mocha-reporter": "^2.2.3",
5556
"karma-safari-launcher": "^1.0.0",
5657
"karma-stability-reporter": "^3.0.1",
57-
"mocha": "^10.1.0",
58-
"sinon": "^2.2.0",
59-
"sinon-chai": "^2.14.0"
58+
"mocha": "^10.2.0"
6059
}
6160
}

test/unit/.eslintrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"env": {
3-
"mocha": true,
3+
"jest": true,
44
"browser": true
55
},
6-
"rules": {}
6+
"plugins": ["jest"]
77
}

test/unit/adapter_factory.js renamed to test/unit/adapterfactory.test.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,30 @@
55
* that can be found in the LICENSE file in the root of the source
66
* tree.
77
*/
8-
/* eslint-env node */
9-
const chai = require('chai');
10-
const expect = chai.expect;
11-
const sinon = require('sinon');
12-
const sinonChai = require('sinon-chai');
13-
chai.use(sinonChai);
14-
158
describe('adapter factory', () => {
169
const {adapterFactory} = require('../../dist/adapter_factory.js');
1710
const utils = require('../../dist/utils.js');
1811

1912
let window;
2013
beforeEach(() => {
2114
window = {
22-
RTCPeerConnection: sinon.stub(),
15+
RTCPeerConnection: jest.fn(),
2316
};
2417
});
2518

2619
describe('does not shim', () => {
2720
afterEach(() => {
28-
utils.detectBrowser.restore();
21+
utils.detectBrowser.mockRestore();
2922
});
3023
['Chrome', 'Firefox', 'Safari'].forEach(browser => {
3124
it(browser + ' when disabled', () => {
32-
sinon.stub(utils, 'detectBrowser').returns({
25+
jest.spyOn(utils, 'detectBrowser').mockReturnValue({
3326
browser: browser.toLowerCase()
3427
});
3528
let options = {};
3629
options['shim' + browser] = false;
3730
const adapter = adapterFactory(window, options);
38-
expect(adapter).not.to.have.property('browserShim');
31+
expect(adapter).not.toHaveProperty('browserShim');
3932
});
4033
});
4134
});
@@ -48,6 +41,6 @@ describe('adapter factory', () => {
4841
'Gecko/20100101 Firefox/44.0'
4942
}};
5043
const constructor = () => adapterFactory({window});
51-
expect(constructor).not.to.throw();
44+
expect(constructor).not.toThrow();
5245
});
5346
});

test/unit/addicecandidate.js renamed to test/unit/addicecandidate.test.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,33 @@
55
* that can be found in the LICENSE file in the root of the source
66
* tree.
77
*/
8-
/* eslint-env node */
9-
const chai = require('chai');
10-
const expect = chai.expect;
11-
const sinon = require('sinon');
12-
const sinonChai = require('sinon-chai');
13-
chai.use(sinonChai);
148

159
describe('addIceCandidate with null or empty candidate', () => {
1610
const shim = require('../../dist/common_shim');
1711
let window;
1812
let origAddIceCandidate;
1913
beforeEach(() => {
2014
window = {
21-
RTCPeerConnection: sinon.stub(),
15+
RTCPeerConnection: jest.fn(),
2216
};
23-
origAddIceCandidate = sinon.stub();
17+
origAddIceCandidate = jest.fn();
2418
window.RTCPeerConnection.prototype.addIceCandidate = origAddIceCandidate;
2519
});
2620

2721
describe('does nothing if', () => {
2822
it('RTCPeerConnection is not defined', () => {
29-
expect(() => shim.shimAddIceCandidateNullOrEmpty({}, {})).not.to.throw();
23+
expect(() => shim.shimAddIceCandidateNullOrEmpty({}, {})).not.toThrow();
3024
});
3125
it('RTCPeerConnection.prototype.addIceCandidate is undefined', () => {
3226
window.RTCPeerConnection.prototype.addIceCandidate = null;
33-
expect(() => shim.shimAddIceCandidateNullOrEmpty({}, {})).not.to.throw();
27+
expect(() => shim.shimAddIceCandidateNullOrEmpty({}, {})).not.toThrow();
3428
});
3529
it('the candidate argument is optional', () => {
3630
expect(window.RTCPeerConnection.prototype.addIceCandidate.length)
37-
.to.equal(0);
31+
.toBe(0);
3832
shim.shimAddIceCandidateNullOrEmpty({}, {});
3933
expect(window.RTCPeerConnection.prototype.addIceCandidate)
40-
.to.equal(origAddIceCandidate);
34+
.toBe(origAddIceCandidate);
4135
});
4236
});
4337

@@ -46,7 +40,7 @@ describe('addIceCandidate with null or empty candidate', () => {
4640
(candidate) => origAddIceCandidate(candidate);
4741
shim.shimAddIceCandidateNullOrEmpty(window, {});
4842
expect(window.RTCPeerConnection.prototype.addIceCandidate.length)
49-
.to.equal(0);
43+
.toBe(0);
5044
});
5145

5246
it('ignores addIceCandidate(null)', () => {
@@ -55,7 +49,7 @@ describe('addIceCandidate with null or empty candidate', () => {
5549
shim.shimAddIceCandidateNullOrEmpty(window, {});
5650
const pc = new window.RTCPeerConnection();
5751
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
58-
expect(origAddIceCandidate.callCount).to.equal(1);
52+
expect(origAddIceCandidate.mock.calls.length).toBe(1);
5953
});
6054

6155
describe('Chrome behaviour', () => {
@@ -73,7 +67,7 @@ describe('addIceCandidate with null or empty candidate', () => {
7367

7468
const pc = new window.RTCPeerConnection();
7569
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
76-
expect(origAddIceCandidate.callCount).to.equal(0);
70+
expect(origAddIceCandidate.mock.calls.length).toBe(0);
7771
});
7872

7973
it('passes {candidate: ""} after Chrome 78', () => {
@@ -82,7 +76,7 @@ describe('addIceCandidate with null or empty candidate', () => {
8276

8377
const pc = new window.RTCPeerConnection();
8478
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
85-
expect(origAddIceCandidate.callCount).to.equal(1);
79+
expect(origAddIceCandidate.mock.calls.length).toBe(1);
8680
});
8781
});
8882

@@ -101,7 +95,7 @@ describe('addIceCandidate with null or empty candidate', () => {
10195

10296
const pc = new window.RTCPeerConnection();
10397
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
104-
expect(origAddIceCandidate.callCount).to.equal(0);
98+
expect(origAddIceCandidate.mock.calls.length).toBe(0);
10599
});
106100

107101
it('passes {candidate: ""} after Firefox 68', () => {
@@ -110,7 +104,7 @@ describe('addIceCandidate with null or empty candidate', () => {
110104

111105
const pc = new window.RTCPeerConnection();
112106
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
113-
expect(origAddIceCandidate.callCount).to.equal(1);
107+
expect(origAddIceCandidate.mock.calls.length).toBe(1);
114108
});
115109
});
116110

@@ -128,7 +122,7 @@ describe('addIceCandidate with null or empty candidate', () => {
128122

129123
const pc = new window.RTCPeerConnection();
130124
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
131-
expect(origAddIceCandidate.callCount).to.equal(0);
125+
expect(origAddIceCandidate.mock.calls.length).toBe(0);
132126
});
133127
});
134128
});

test/unit/chrome.js renamed to test/unit/chrome.test.js

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
* that can be found in the LICENSE file in the root of the source
66
* tree.
77
*/
8-
/* eslint-env node */
9-
const chai = require('chai');
10-
const expect = chai.expect;
11-
const sinon = require('sinon');
12-
const sinonChai = require('sinon-chai');
13-
chai.use(sinonChai);
148

159
/* a mock of the Chrome RTCLegacyStatReport */
1610
function RTCLegacyStatsReport() {
@@ -41,7 +35,7 @@ describe('Chrome shim', () => {
4135
window = {
4236
navigator: {
4337
mediaDevices: {
44-
getUserMedia: sinon.stub().returns(Promise.resolve('stream')),
38+
getUserMedia: jest.fn().mockReturnValue(Promise.resolve('stream')),
4539
},
4640
},
4741
RTCPeerConnection: function() {}
@@ -74,20 +68,20 @@ describe('Chrome shim', () => {
7468

7569
it('returns chrome legacy getStats when called with a callback', (done) => {
7670
pc.getStats((result) => {
77-
expect(result).to.have.property('result');
71+
expect(result).toHaveProperty('result');
7872
const report = result.result()[0];
79-
expect(report).to.have.property('id');
80-
expect(report).to.have.property('type');
81-
expect(report).to.have.property('timestamp');
82-
expect(report).to.have.property('stat');
73+
expect(report).toHaveProperty('id');
74+
expect(report).toHaveProperty('type');
75+
expect(report).toHaveProperty('timestamp');
76+
expect(report).toHaveProperty('stat');
8377
done();
8478
});
8579
});
8680

8781
it('is translated into a Map', () => {
8882
return pc.getStats()
8983
.then(result => {
90-
expect(result).to.be.a('Map');
84+
expect(result).toBeInstanceOf(Map);
9185
});
9286
});
9387
});
@@ -119,80 +113,97 @@ describe('Chrome shim', () => {
119113
});
120114

121115
describe('getDisplayMedia shim', () => {
122-
const getSourceId = sinon.stub().returns(Promise.resolve('abc'));
116+
const getSourceId = jest.fn().mockReturnValue(Promise.resolve('abc'));
123117

124118
it('does not overwrite an existing ' +
125119
'navigator.mediaDevices.getDisplayMedia', () => {
126120
window.navigator.mediaDevices.getDisplayMedia = 'foo';
127121
shim.shimGetDisplayMedia(window, getSourceId);
128-
expect(window.navigator.mediaDevices.getDisplayMedia).to.equal('foo');
122+
expect(window.navigator.mediaDevices.getDisplayMedia).toBe('foo');
129123
});
130124

131125
it('does not if navigator.mediaDevices does not exist', () => {
132126
delete window.navigator.mediaDevices;
133127
shim.shimGetDisplayMedia(window);
134-
expect(window.navigator.mediaDevices).to.equal(undefined);
128+
expect(window.navigator.mediaDevices).toBe(undefined);
135129
});
136130

137131
it('shims navigator.mediaDevices.getDisplayMedia', () => {
138132
shim.shimGetDisplayMedia(window, getSourceId);
139-
expect(window.navigator.mediaDevices.getDisplayMedia).to.be.a('function');
133+
expect(typeof window.navigator.mediaDevices.getDisplayMedia)
134+
.toBe('function');
140135
});
141136

142-
it('calls getUserMedia with the sourceId', () => {
137+
it('calls getUserMedia with the sourceId', async() => {
143138
shim.shimGetDisplayMedia(window, getSourceId);
144-
return window.navigator.mediaDevices.getDisplayMedia({video: true})
145-
.then(() => {
146-
expect(window.navigator.mediaDevices.getUserMedia)
147-
.to.have.been.calledWith({video: {mandatory: {
139+
await window.navigator.mediaDevices.getDisplayMedia({video: true});
140+
expect(window.navigator.mediaDevices.getUserMedia.mock.calls.length)
141+
.toBe(1);
142+
expect(window.navigator.mediaDevices.getUserMedia.mock.calls[0][0])
143+
.toEqual({
144+
video: {
145+
mandatory: {
148146
chromeMediaSource: 'desktop',
149147
chromeMediaSourceId: 'abc',
150148
maxFrameRate: 3,
151-
}}});
149+
}
150+
}
152151
});
153152
});
154153

155-
it('translates frameRate to legacy maxFrameRate', () => {
154+
it('translates frameRate to legacy maxFrameRate', async() => {
156155
shim.shimGetDisplayMedia(window, getSourceId);
157-
return window.navigator.mediaDevices
158-
.getDisplayMedia({video: {frameRate: 25}})
159-
.then(() => {
160-
expect(window.navigator.mediaDevices.getUserMedia)
161-
.to.have.been.calledWith({video: {mandatory: {
156+
await window.navigator.mediaDevices
157+
.getDisplayMedia({video: {frameRate: 25}});
158+
expect(window.navigator.mediaDevices.getUserMedia.mock.calls.length)
159+
.toBe(1);
160+
expect(window.navigator.mediaDevices.getUserMedia.mock.calls[0][0])
161+
.toEqual({
162+
video: {
163+
mandatory: {
162164
chromeMediaSource: 'desktop',
163165
chromeMediaSourceId: 'abc',
164166
maxFrameRate: 25,
165-
}}});
167+
}
168+
}
166169
});
167170
});
168171

169-
it('translates width to legacy maxWidth', () => {
172+
it('translates width to legacy maxWidth', async() => {
170173
shim.shimGetDisplayMedia(window, getSourceId);
171-
return window.navigator.mediaDevices
172-
.getDisplayMedia({video: {width: 640}})
173-
.then(() => {
174-
expect(window.navigator.mediaDevices.getUserMedia)
175-
.to.have.been.calledWith({video: {mandatory: {
174+
await window.navigator.mediaDevices
175+
.getDisplayMedia({video: {width: 640}});
176+
expect(window.navigator.mediaDevices.getUserMedia.mock.calls.length)
177+
.toBe(1);
178+
expect(window.navigator.mediaDevices.getUserMedia.mock.calls[0][0])
179+
.toEqual({
180+
video: {
181+
mandatory: {
176182
chromeMediaSource: 'desktop',
177183
chromeMediaSourceId: 'abc',
178184
maxFrameRate: 3,
179185
maxWidth: 640,
180-
}}});
186+
}
187+
}
181188
});
182189
});
183190

184-
it('translates height to legacy maxHeight', () => {
191+
it('translates height to legacy maxHeight', async() => {
185192
shim.shimGetDisplayMedia(window, getSourceId);
186-
return window.navigator.mediaDevices
187-
.getDisplayMedia({video: {height: 480}})
188-
.then(() => {
189-
expect(window.navigator.mediaDevices.getUserMedia)
190-
.to.have.been.calledWith({video: {mandatory: {
193+
await window.navigator.mediaDevices
194+
.getDisplayMedia({video: {height: 480}});
195+
expect(window.navigator.mediaDevices.getUserMedia.mock.calls.length)
196+
.toBe(1);
197+
expect(window.navigator.mediaDevices.getUserMedia.mock.calls[0][0])
198+
.toEqual({
199+
video: {
200+
mandatory: {
191201
chromeMediaSource: 'desktop',
192202
chromeMediaSourceId: 'abc',
193203
maxFrameRate: 3,
194204
maxHeight: 480,
195-
}}});
205+
}
206+
}
196207
});
197208
});
198209
});

0 commit comments

Comments
 (0)