From 04cf01cebcfb06f4ab64ea8e3def73af8a533f51 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 8 Dec 2020 22:01:15 -0500 Subject: [PATCH 1/4] test(karma): tidy Karma configuration and add comments --- karma.conf.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index fcdf9beb..ccaef28a 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -1,10 +1,15 @@ +// 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', @@ -12,12 +17,62 @@ module.exports = config => { '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, + + // Mocha reporter options + // https://www.npmjs.com/package/karma-mocha-reporter + mochaReporter: { + showDiff: true } }); }; From 63caca1dce6bb08cdbb04ba54b2fa68fd962865a Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 8 Dec 2020 22:16:08 -0500 Subject: [PATCH 2/4] test(karma): change Karma's `debug.html` to the Mocha web reporter --- karma.conf.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/karma.conf.js b/karma.conf.js index ccaef28a..3a3ef675 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -69,6 +69,14 @@ module.exports = config => { // 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: { From cb4bbd8e1f2f34f831138af31a26ed31f38045b6 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 8 Dec 2020 22:32:12 -0500 Subject: [PATCH 3/4] test(client): refactor Karma client tests and helpers --- test/client/index.js | 10 +++++----- test/helpers/run-tests.js | 29 ++++++++++++++--------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/test/client/index.js b/test/client/index.js index 15a049a7..1d2af969 100644 --- a/test/client/index.js +++ b/test/client/index.js @@ -1,19 +1,19 @@ 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 = '
test
'; var start = performance.now(); var times = 1000; while (--times) { - parser(html); + clientParser(html); } var end = performance.now(); console.log('performance: ' + (end - start) + ' milliseconds'); // eslint-disable-line no-console diff --git a/test/helpers/run-tests.js b/test/helpers/run-tests.js index 8e1f4bb8..24bff338 100644 --- a/test/helpers/run-tests.js +++ b/test/helpers/run-tests.js @@ -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'); } @@ -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; From ee059a58ad8f5861c534434260d529b1ebb5e60b Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 8 Dec 2020 22:43:00 -0500 Subject: [PATCH 4/4] test(client): enable performance test for client parser --- test/client/index.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/test/client/index.js b/test/client/index.js index 1d2af969..7d507951 100644 --- a/test/client/index.js +++ b/test/client/index.js @@ -8,14 +8,16 @@ describe('client parser', function () { helpers.throwsError(clientParser, assert); helpers.runTests(htmlCases, clientParser, serverParser, assert); - it.skip('performance', function () { - var html = '
test
'; - var start = performance.now(); - var times = 1000; - while (--times) { - clientParser(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('
test
'); + } + var end = performance.now(); + var elapsed = end - start; + assert.isBelow(elapsed, 50); + }); }); });