Skip to content

Commit

Permalink
Improve unit tests (video-dev#173)
Browse files Browse the repository at this point in the history
Convert unit tests from Node assertions to Chai BDD
  • Loading branch information
johnBartos authored Nov 12, 2018
1 parent 6277bac commit 2d60108
Show file tree
Hide file tree
Showing 31 changed files with 2,902 additions and 2,483 deletions.
139 changes: 70 additions & 69 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,58 @@ const pkgJson = require('./package.json');
const webpack = require('webpack');
const path = require('path');

module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'sinon'],

// list of files / patterns to load in the browser
files: [
'tests/unit/**/*.js'
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'sinon-chai'],

// list of files / patterns to load in the browser
files: [
'tests/index.js'
],

// list of files to exclude
exclude: [],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'tests/unit/**/*.js': ['webpack', 'sourcemap']
},

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['mocha', 'coverage-istanbul'],

coverageIstanbulReporter: {
reports: ['lcov', 'text-summary'],
fixWebpackSourcePaths: true
},

webpack: {
devtool: 'inline-source-map',
mode: 'development',
module: {
rules: [
// instrument only testing sources with Istanbul
{
test: /\.js$/,
include: path.resolve('src/'),
exclude: path.resolve(__dirname, 'node_modules'),
use: [
{
loader: 'istanbul-instrumenter-loader',
options: { esModules: true }
}
]
}
]
// list of files to exclude
exclude: [],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
// node_modules must not be webpacked or else Karma will fail to load frameworks
preprocessors: {
'tests/index.js': ['webpack', 'sourcemap']
},

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['mocha', 'coverage-istanbul'],

coverageIstanbulReporter: {
reports: ['lcov', 'text-summary'],
fixWebpackSourcePaths: true
},

webpack: {
mode: 'development',
devtool: 'inline-source-map',
module: {
rules: [
// instrument only testing sources with Istanbul
{
test: /\.js$/,
include: path.resolve('src/'),
exclude: path.resolve(__dirname, 'node_modules'),
use: [
{
loader: 'istanbul-instrumenter-loader',
options: { esModules: true }
}
]
}
]
},
plugins: [
new webpack.DefinePlugin({
Expand All @@ -65,31 +66,31 @@ module.exports = function(config) {
__USE_EME_DRM__: JSON.stringify(true)
})
]
},
},

// web server port
port: 9876,
// web server port
port: 9876,

// enable / disable colors in the output (reporters and logs)
colors: true,
// enable / disable colors in the output (reporters and logs)
colors: true,

// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['ChromeHeadless'],
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['ChromeHeadless'],

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
});
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
});
};
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@
"test:func": "cross-env BABEL_ENV=test mocha --compilers js:babel-register tests/functional/auto/setup.js --timeout 40000"
},
"dependencies": {
"chai": "^4.2.0",
"url-toolkit": "^2.1.2"
},
"devDependencies": {
"arraybuffer-equal": "^1.0.4",
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-plugin-transform-object-assign": "^6.22.0",
"babel-preset-env": "^1.7.0",
"babel-register": "^6.24.0",
"chromedriver": "^2.38.3",
"cross-env": "^5.0.2",
"deep-strict-equal": "^0.2.0",
"esdoc": "^1.1.0",
"esdoc-standard-plugin": "^1.0.0",
"eslint": "^4.13.1",
Expand All @@ -56,18 +54,20 @@
"http-server": "^0.11.0",
"husky": "^0.14.3",
"istanbul-instrumenter-loader": "^3.0.1",
"karma": "^2.0.0",
"karma": "^3.1.1",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage-istanbul-reporter": "^1.3.0",
"karma-coverage-istanbul-reporter": "^2.0.4",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.3",
"karma-sinon": "^1.0.5",
"karma-sinon-chai": "^2.0.2",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^2.0.4",
"mocha": "^3.0.2",
"karma-webpack": "^3.0.5",
"mocha": "^5.2.0",
"mversion": "^1.10.1",
"selenium-webdriver": "^3.1.0",
"sinon": "^4.1.3",
"sinon": "^7.1.1",
"sinon-chai": "^3.2.0",
"webpack": "^4.19.1",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^2.7.1",
Expand Down
3 changes: 2 additions & 1 deletion src/controller/timeline-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ class TimelineController extends EventHandler {
if (reuseVttTextTrack(inUseTrack, track)) {
textTrack = inUseTrack;
}
} if (!textTrack) {
}
if (!textTrack) {
textTrack = this.createTextTrack('subtitles', track.name, track.lang);
}

Expand Down
7 changes: 5 additions & 2 deletions tests/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
"node": true,
"commonjs": true,
"es6": true,
"mocha": true
},
"globals": {
// Test globals
Expand All @@ -12,9 +13,11 @@ module.exports = {
"before": false,
"beforeEach": false,
"describe": false,
"expect": false,
"it": false,
"expect": true,
"sinon": false,
"xit": false
},
"rules": {
"no-unused-expressions": 0
}
};
4 changes: 4 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const testsContext = require.context('./unit', true);
testsContext.keys().forEach(testsContext);

export default testsContext;
8 changes: 3 additions & 5 deletions tests/unit/controller/abr-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import AbrController from '../../../src/controller/abr-controller';
import EwmaBandWidthEstimator from '../../../src/utils/ewma-bandwidth-estimator';
import Hls from '../../../src/hls';

const assert = require('assert');

describe('AbrController', () => {
it('should return correct next auto level', () => {
describe('AbrController', function () {
it('should return correct next auto level', function () {
let hls = new Hls({ maxStarvationDelay: 4 });
hls.levelController._levels = [
{ bitrate: 105000, name: '144', details: { totalduration: 4, fragments: [{}] } },
Expand All @@ -17,6 +15,6 @@ describe('AbrController', () => {
];
let abrController = new AbrController(hls);
abrController.bwEstimator = new EwmaBandWidthEstimator(hls, 15, 4, 5e5);
assert.equal(abrController.nextAutoLevel, 0);
expect(abrController.nextAutoLevel).to.equal(0);
});
});
Loading

0 comments on commit 2d60108

Please sign in to comment.