Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 68 additions & 5 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,86 @@
// Karma configuration
// https://karma-runner.github.io/5.2/config/configuration-file.html
module.exports = 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', 'chai', 'commonjs'],
reporters: ['mocha'],
mochaReporter: {
showDiff: true
},

// list of files / patterns to load in the browser
files: [
'dist/htmlparser2.js',
'lib/*.js',
'test/cases/html.js',
'test/client/*.js',
'test/helpers/*.js'
],

// list of files / patterns to exclude
exclude: ['lib/html-to-dom-server.js'],
browsers: ['Chrome'],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'dist/**/*.js': ['commonjs'],
'lib/**/*.js': ['commonjs'],
'test/**/*.js': ['commonjs']
},

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

// web server port
port: 9876,

// 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,

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

// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera (has to be installed with `npm install karma-opera-launcher`)
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
// - PhantomJS
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],

// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,

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

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,

// Client options
client: {
mocha: {
// change Karma's `debug.html` to the Mocha web reporter
reporter: 'html'
}
},

// Mocha reporter options
// https://www.npmjs.com/package/karma-mocha-reporter
mochaReporter: {
showDiff: true
}
});
};
28 changes: 15 additions & 13 deletions test/client/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
var assert = window.chai.assert;
var htmlCases = require('../cases/html');
var htmlparser = require('../../dist/htmlparser2');
var parser = require('../../lib/html-to-dom-client');
var serverParser = require('../../dist/htmlparser2').parseDOM;
var clientParser = require('../../lib/html-to-dom-client');
var helpers = require('../helpers');

describe('client parser', function () {
helpers.throwsError(parser, assert);
helpers.runTests(htmlCases, parser, htmlparser.parseDOM, assert);
helpers.throwsError(clientParser, assert);
helpers.runTests(htmlCases, clientParser, serverParser, assert);

it.skip('performance', function () {
var html = '<div>test</div>';
var start = performance.now();
var times = 1000;
while (--times) {
parser(html);
}
var end = performance.now();
console.log('performance: ' + (end - start) + ' milliseconds'); // eslint-disable-line no-console
describe('performance', function () {
it('executes 1000 times in less than 50ms', function () {
var times = 1000;
var start = performance.now();
while (--times) {
clientParser('<div>test</div>');
}
var end = performance.now();
var elapsed = end - start;
assert.isBelow(elapsed, 50);
});
});
});
29 changes: 14 additions & 15 deletions test/helpers/run-tests.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
/**
* Runs tests.
*
* @param {Object} testCases - The test cases.
* @param {Function} expectedParser - The expected parser.
* @param {Function} actualParser - The actual parser.
* @param {Function} [assert] - The assertion module.
* @param {Object} testCases - Test cases.
* @param {Function} expectedParser - Expected parser.
* @param {Function} actualParser - Actual parser.
* @param {Function} [assert] - Assertion module.
*/
module.exports = function runTests(
testCases,
expectedParser,
actualParser,
assert
) {
function runTests(testCases, expectedParser, actualParser, assert) {
if (typeof assert !== 'function') {
assert = require('assert');
}
Expand All @@ -24,16 +19,20 @@ module.exports = function runTests(
throw new TypeError('Missing or invalid actual parser');
}

// enable `decodeEntities` for both parsers
// because entities are decoded on the browser
var parserOptions = { decodeEntities: true };

testCases.forEach(function (testCase) {
var _it = testCase.only ? it.only : testCase.skip ? it.skip : it;

_it('parses ' + testCase.name, function () {
// enable decodeEntities for both parsers because
// entities are decoded by client parser in jsdom
assert.deepEqual(
expectedParser(testCase.data, { decodeEntities: true }),
actualParser(testCase.data, { decodeEntities: true })
actualParser(testCase.data, parserOptions),
expectedParser(testCase.data, parserOptions)
);
});
});
};
}

module.exports = runTests;