Skip to content

Commit

Permalink
Fix tests and lint errors (#258)
Browse files Browse the repository at this point in the history
* Remove unneeded logging

* Fix linting errors

* Add delay between network tests to wait for Sessions to disconnect

* Rev version to 3.1.2

* Fix build

(exclude test files)
  • Loading branch information
jeffswartz authored Nov 29, 2023
1 parent eb60701 commit fb6d04e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig.json",
"project": "tsconfig.eslint.json",
"sourceType": "module"
},
"plugins": [
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opentok-network-test-js",
"version": "3.1.1",
"version": "3.1.2",
"description": "Precall network test for applications using the OpenTok platform.",
"main": "dist/NetworkTest/index.js",
"types": "dist/NetworkTest/index.d.ts",
Expand All @@ -13,8 +13,8 @@
"example": "npm run build && cd sample && npm run build && python -m SimpleHTTPServer",
"lint": "npm run tslint",
"lint-fix": "npm run tslint-fix",
"tslint": "./node_modules/eslint/bin/eslint.js src",
"tslint-fix": "./node_modules/eslint/bin/eslint.js src --fix"
"tslint": "./node_modules/eslint/bin/eslint.js src test/**/*.ts",
"tslint-fix": "./node_modules/eslint/bin/eslint.js src test/**/*.ts --fix"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion sample/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/NetworkTest/testQuality/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ function publishAndSubscribe(OT: OT.Client, options?: NetworkTestOptions) {
audioOnly = true;
}
const publisherOptions: OT.PublisherProperties = {
resolution: options.fullHd ? FULL_HD_RESOLUTION : HD_RESOUTION,
scalableVideo: options.scalableVideo,
resolution: options?.fullHd ? FULL_HD_RESOLUTION : HD_RESOUTION,
scalableVideo: options?.scalableVideo,
width: '100%',
height: '100%',
insertMode: 'append',
Expand Down
72 changes: 39 additions & 33 deletions test/NetworkTest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import NetworkTest, { ErrorNames } from '../src/NetworkTest';
import { ConnectivityTestResults } from '../src/NetworkTest/testConnectivity/index';
import { QualityTestError } from '../src/NetworkTest/testQuality/errors/index';

type Util = jasmine.MatchersUtil;
type CustomMatcher = jasmine.CustomMatcher;
type EqualityTesters = jasmine.CustomEqualityTester[];

const malformedCredentials = { apiKey: '1234', invalidProp: '1234', token: '1234' };
const badCredentials = { apiKey: '1234', sessionId: '1234', token: '1234' };
Expand All @@ -32,7 +30,7 @@ const badCredentialsNetworkTest = new NetworkTest(OTClient, badCredentials);
const validOnUpdateCallback = (stats: OT.SubscriberStats) => stats;

const customMatchers: jasmine.CustomMatcherFactories = {
toBeInstanceOf: (util: Util, customEqualityTesters: EqualityTesters): CustomMatcher => {
toBeInstanceOf: (): CustomMatcher => {
return {
compare: (actual: any, expected: any): jasmine.CustomMatcherResult => {
const pass: boolean = actual instanceof expected;
Expand All @@ -41,7 +39,7 @@ const customMatchers: jasmine.CustomMatcherFactories = {
},
};
},
toBeABoolean: (util: Util, customEqualityTesters: EqualityTesters): CustomMatcher => {
toBeABoolean: (): CustomMatcher => {
return {
compare: (actual: any, expected: any): jasmine.CustomMatcherResult => {
const pass: boolean = typeof actual === 'boolean';
Expand All @@ -58,6 +56,14 @@ describe('NetworkTest', () => {
jasmine.addMatchers(customMatchers);
});

afterEach((done) => {
if (networkTest) {
networkTest.stop();
}
// A bit of a hack. But this prevents tests from failing if a previous test's Session didn't disconnect:
setTimeout(() => { done(); }, 1000);
});

it('its constructor requires OT and valid session credentials', () => {
expect(() => new NetworkTest(sessionCredentials)).toThrow(new MissingOpenTokInstanceError());
expect(() => new NetworkTest({}, sessionCredentials)).toThrow(new MissingOpenTokInstanceError());
Expand All @@ -72,7 +78,7 @@ describe('NetworkTest', () => {

describe('Connectivity Test', () => {
const testConnectFailure = (errorName, expectedType) => {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
const realInitSession = OT.initSession;
spyOn(OT, 'initSession').and.callFake((apiKey, sessionId) => {
const session = realInitSession(apiKey, sessionId);
Expand Down Expand Up @@ -131,11 +137,11 @@ describe('NetworkTest', () => {
...{
properties: {
...OTClient.properties,
loggingURL: OTClient.properties.loggingURL.replace('tokbox', 'bad-tokbox')
}
}
loggingURL: OTClient.properties.loggingURL.replace('tokbox', 'bad-tokbox'),
},
},
};
const badLoggingNetworkTest = new NetworkTest(badLoggingOT, badLoggingCredentials)
const badLoggingNetworkTest = new NetworkTest(badLoggingOT, badLoggingCredentials);
badLoggingNetworkTest.testConnectivity()
.then((results: ConnectivityTestResults) => {
expect(results.failedTests).toBeInstanceOf(Array);
Expand Down Expand Up @@ -276,9 +282,10 @@ describe('NetworkTest', () => {
};

it('validates its onUpdate callback', () => {
expect(() => networkTest.testQuality('callback').toThrow(new InvalidOnUpdateCallback()))
// eslint-disable-next-line
expect(() => networkTest.testQuality('bad-callback').toThrow(new InvalidOnUpdateCallback()));
expect(() => networkTest.testConnectivity(validOnUpdateCallback)
.not.toThrowError(NetworkTestError))
.not.toThrowError(NetworkTestError));
});

it('should return an error if invalid session credentials are used', (done) => {
Expand Down Expand Up @@ -314,7 +321,7 @@ describe('NetworkTest', () => {
});
networkTest.testQuality()
.catch((error?: QualityTestError) => {
expect(error.name).toBe(ErrorNames.FAILED_TO_OBTAIN_MEDIA_DEVICES);
expect(error?.name).toBe(ErrorNames.FAILED_TO_OBTAIN_MEDIA_DEVICES);
done();
});
}, 10000);
Expand All @@ -323,23 +330,23 @@ describe('NetworkTest', () => {
const realOTGetDevices = OT.getDevices;
spyOn(OT, 'getDevices').and.callFake((callbackFn) => {
realOTGetDevices((error, devices) => {
const onlyVideoDevices = devices.filter(device => device.kind !== 'audioInput');
const onlyVideoDevices = devices?.filter(device => device.kind !== 'audioInput');
callbackFn(error, onlyVideoDevices);
});
});
networkTest.testQuality()
.catch((error?: QualityTestError) => {
expect(error.name).toBe(ErrorNames.NO_AUDIO_CAPTURE_DEVICES);
expect(error?.name).toBe(ErrorNames.NO_AUDIO_CAPTURE_DEVICES);
done();
});
}, 10000);

it('should return valid test results or an error', (done) => {
const validateError = (error?: QualityTestError) => {
expect(error.name).toBe(QUALITY_TEST_ERROR);
expect(error?.name).toBe(ErrorNames.QUALITY_TEST_ERROR);
};

const onUpdate = (stats: Stats) => console.info('Subscriber stats:', stats);
const onUpdate = (stats: Stats) => validOnUpdateCallback(stats);

networkTest.testQuality(onUpdate)
.then(validateStandardResults)
Expand All @@ -361,10 +368,10 @@ describe('NetworkTest', () => {
};

const validateError = (error?: QualityTestError) => {
expect(error.name).toBe(QUALITY_TEST_ERROR);
expect(error?.name).toBe(ErrorNames.QUALITY_TEST_ERROR);
};

const onUpdate = (stats: Stats) => console.info('Subscriber stats:', stats);
const onUpdate = (stats: Stats) => validOnUpdateCallback(stats);

networkTestWithOptions.testQuality(onUpdate)
.then(validateResults)
Expand All @@ -374,11 +381,11 @@ describe('NetworkTest', () => {

it('should stop the quality test when you call the stop() method', (done) => {
const validateError = (error?: QualityTestError) => {
expect(error.name).toBe(QUALITY_TEST_ERROR);
expect(error?.name).toBe(ErrorNames.QUALITY_TEST_ERROR);
};

const onUpdate = (stats: Stats) => {
console.info('Subscriber stats:', stats);
validOnUpdateCallback(stats);
networkTest.stop(); // The test will wait for adequate stats before stopping
};

Expand All @@ -392,7 +399,7 @@ describe('NetworkTest', () => {
const realOTGetDevices = OT.getDevices;
spyOn(OT, 'getDevices').and.callFake((callbackFn) => {
realOTGetDevices((error, devices) => {
const onlyAudioDevices = devices.filter(device => device.kind !== 'videoInput');
const onlyAudioDevices = devices?.filter(device => device.kind !== 'videoInput');
callbackFn(error, onlyAudioDevices);
});
});
Expand All @@ -401,7 +408,6 @@ describe('NetworkTest', () => {
const { audio, video } = results;
expect(audio.bitrate).toEqual(jasmine.any(Number));
expect(audio.supported).toEqual(jasmine.any(Boolean));
expect(audio.reason || '').toEqual(jasmine.any(String));
expect(audio.packetLossRatio).toEqual(jasmine.any(Number));
expect(audio.mos).toEqual(jasmine.any(Number));

Expand All @@ -413,7 +419,7 @@ describe('NetworkTest', () => {
expect(error).toBe(QualityTestError);
};

const onUpdate = (stats: Stats) => console.info('Subscriber stats:', stats);
const onUpdate = (stats: Stats) => validOnUpdateCallback(stats);

networkTest.testQuality(onUpdate)
.then(validateResults)
Expand All @@ -424,17 +430,17 @@ describe('NetworkTest', () => {
it('should return an error if the window.navigator is undefined', () => {
spyOnProperty(window, 'navigator', 'get').and.returnValue(undefined);
networkTest.testQuality(null)
.then(validateResultsUndefined)
.catch(validateUnsupportedBrowserError)
.then(validateResultsUndefined)
.catch(validateUnsupportedBrowserError);
});

it('should return an unsupported browser error if the browser is an older version of Edge', () => {
spyOnProperty(window, 'navigator', 'get').and.returnValue({
mediaDevices: {},
webkitGetUserMedia: null,
mozGetUserMedia: null,
userAgent: 'Edge/12.10240',
});
mediaDevices: {},
webkitGetUserMedia: null,
mozGetUserMedia: null,
userAgent: 'Edge/12.10240',
});
networkTest.testQuality(null)
.then(validateResultsUndefined)
.catch(validateUnsupportedBrowserError);
Expand All @@ -451,15 +457,15 @@ describe('NetworkTest', () => {
navigator.mozGetUserMedia = mozGetUserMedia;
navigator.webkitGetUserMedia = webkitGetUserMedia;
done();
});
});
}, 10000);

it('results in a failed test if OT.initPublisher() returns an error', (done) => {
spyOn(OT, 'initPublisher').and.callFake((target, options, callback) => {
callback(new Error());
});
networkTest.testQuality().catch((error?: QualityTestError) => {
expect(error.name).toBe(ErrorNames.INIT_PUBLISHER_ERROR);
expect(error?.name).toBe(ErrorNames.INIT_PUBLISHER_ERROR);
done();
});
}, 10000);
Expand All @@ -475,7 +481,7 @@ describe('NetworkTest', () => {
return session;
});
networkTest.testQuality().catch((error?: QualityTestError) => {
expect(error.name).toBe(ErrorNames.SUBSCRIBE_TO_SESSION_ERROR);
expect(error?.name).toBe(ErrorNames.SUBSCRIBE_TO_SESSION_ERROR);
done();
});
}, 10000);
Expand Down
9 changes: 9 additions & 0 deletions tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// tsconfig.eslint.json

{
"extends": "./tsconfig.json",
"include": [
"src/**/*.ts",
"test/**/*.ts"
]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
"exclude": [
"node_modules",
"dist",
"test/**/*.ts"
"test/setup/setup.js"
]
}

0 comments on commit fb6d04e

Please sign in to comment.