diff --git a/.eslintrc.js b/.eslintrc.js index 3ca41f42..7269e9e7 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -6,7 +6,7 @@ module.exports = { }, "parser": "@typescript-eslint/parser", "parserOptions": { - "project": "tsconfig.json", + "project": "tsconfig.eslint.json", "sourceType": "module" }, "plugins": [ diff --git a/package-lock.json b/package-lock.json index 9b04ebd5..7f786011 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "opentok-network-test-js", - "version": "3.1.1", + "version": "3.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "opentok-network-test-js", - "version": "3.1.1", + "version": "3.1.2", "license": "MIT", "dependencies": { "axios": "^0.21.1", diff --git a/package.json b/package.json index b042e61d..5b54de39 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/sample/package-lock.json b/sample/package-lock.json index 6ac028cd..d9ace7af 100644 --- a/sample/package-lock.json +++ b/sample/package-lock.json @@ -17,7 +17,7 @@ } }, "..": { - "version": "3.1.1", + "version": "3.1.2", "license": "MIT", "dependencies": { "axios": "^0.21.1", diff --git a/src/NetworkTest/testQuality/index.ts b/src/NetworkTest/testQuality/index.ts index e1d48032..a80938dc 100644 --- a/src/NetworkTest/testQuality/index.ts +++ b/src/NetworkTest/testQuality/index.ts @@ -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', diff --git a/test/NetworkTest.spec.ts b/test/NetworkTest.spec.ts index 022d2559..d9912dc6 100644 --- a/test/NetworkTest.spec.ts +++ b/test/NetworkTest.spec.ts @@ -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' }; @@ -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; @@ -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'; @@ -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()); @@ -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); @@ -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); @@ -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) => { @@ -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); @@ -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) @@ -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) @@ -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 }; @@ -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); }); }); @@ -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)); @@ -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) @@ -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); @@ -451,7 +457,7 @@ describe('NetworkTest', () => { navigator.mozGetUserMedia = mozGetUserMedia; navigator.webkitGetUserMedia = webkitGetUserMedia; done(); - }); + }); }, 10000); it('results in a failed test if OT.initPublisher() returns an error', (done) => { @@ -459,7 +465,7 @@ describe('NetworkTest', () => { 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); @@ -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); diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json new file mode 100644 index 00000000..5846cb35 --- /dev/null +++ b/tsconfig.eslint.json @@ -0,0 +1,9 @@ +// tsconfig.eslint.json + +{ + "extends": "./tsconfig.json", + "include": [ + "src/**/*.ts", + "test/**/*.ts" + ] +} diff --git a/tsconfig.json b/tsconfig.json index 0e1da38f..1cae41fa 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -29,6 +29,6 @@ "exclude": [ "node_modules", "dist", - "test/**/*.ts" + "test/setup/setup.js" ] } \ No newline at end of file