From 637ca3d749cf7e79ab903977a50a7a3fbb425511 Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Sat, 12 Jun 2021 20:08:56 +0530 Subject: [PATCH] chore(tools/dev-server): run unit tests, integration tests together (#3621) --- tests/karma.conf.base.js | 65 ++++++++++++++++++---------------- tests/spec/karma.conf.js | 22 +++++++----- tests/unit/karma.conf.js | 22 +++++++----- tools/dev-server.js | 75 ++++++++++++++-------------------------- 4 files changed, 88 insertions(+), 96 deletions(-) diff --git a/tests/karma.conf.base.js b/tests/karma.conf.base.js index 42fab15e26..c2806b18b5 100644 --- a/tests/karma.conf.base.js +++ b/tests/karma.conf.base.js @@ -2,42 +2,45 @@ /* eslint-env node */ const path = require("path"); +/** @type {import("karma").ConfigOptions["files"]} */ +const files = [ + { + pattern: "builds/**/*.*", + included: false, + }, + { + pattern: "@(src|js)/**/*", + included: false, + }, + { + pattern: "worker/*.js", + included: false, + }, + { + pattern: "node_modules/@(idb|hyperhtml|marked|webidl2)/**/*.js", + included: false, + }, + { + pattern: "tests/@(data|support-files)/**/*", + included: false, + }, + { + pattern: "tests/**/*.html", + included: false, + }, + { + pattern: "tests/test-main.js", + type: "module", + }, +]; + /** @param {import("karma").Config} config */ module.exports = config => { /** @type {import("karma").ConfigOptions} */ const options = { basePath: path.join(__dirname, ".."), frameworks: ["jasmine"], - files: [ - { - pattern: "builds/**/*.*", - included: false, - }, - { - pattern: "@(src|js)/**/*", - included: false, - }, - { - pattern: "worker/*.js", - included: false, - }, - { - pattern: "node_modules/@(idb|hyperhtml|marked|webidl2)/**/*.js", - included: false, - }, - { - pattern: "tests/@(data|support-files)/**/*", - included: false, - }, - { - pattern: "tests/**/*.html", - included: false, - }, - { - pattern: "tests/test-main.js", - type: "module", - }, - ], + files, exclude: ["**/*.swp", "*.swp", ".DS_Store"], proxies: { @@ -87,5 +90,7 @@ module.exports = config => { options.plugins = ["karma-*"].concat(localPlugins); } + config.set(options); return options; }; +module.exports.files = files; diff --git a/tests/spec/karma.conf.js b/tests/spec/karma.conf.js index 9bc72ae120..2aa796d03c 100644 --- a/tests/spec/karma.conf.js +++ b/tests/spec/karma.conf.js @@ -2,20 +2,26 @@ /* eslint-env node */ const baseConfig = require("../karma.conf.base.js"); -/** @param {import("karma").Config} config */ -module.exports = config => { - const options = baseConfig(config); - - options.files.push({ +/** @type {import("karma").ConfigOptions["files"]} */ +const additionalFiles = [ + { pattern: "tests/spec/SpecHelper.js", type: "module", included: false, - }); - options.files.push({ + }, + { pattern: "tests/spec/**/*-spec.js", type: "module", included: false, - }); + }, +]; + +/** @param {import("karma").Config} config */ +module.exports = config => { + const options = baseConfig(config); + options.files.push(...additionalFiles); config.set(options); }; + +module.exports.additionalFiles = additionalFiles; diff --git a/tests/unit/karma.conf.js b/tests/unit/karma.conf.js index 39b9837f81..db38b8414a 100644 --- a/tests/unit/karma.conf.js +++ b/tests/unit/karma.conf.js @@ -2,20 +2,26 @@ /* eslint-env node */ const baseConfig = require("../karma.conf.base.js"); -/** @param {import("karma").Config} config */ -module.exports = config => { - const options = baseConfig(config); - - options.files.push({ +/** @type {import("karma").ConfigOptions["files"]} */ +const additionalFiles = [ + { pattern: "tests/unit/SpecHelper.js", type: "module", included: false, - }); - options.files.push({ + }, + { pattern: "tests/unit/**/*-spec.js", type: "module", included: false, - }); + }, +]; + +/** @param {import("karma").Config} config */ +module.exports = config => { + const options = baseConfig(config); + options.files.push(...additionalFiles); config.set(options); }; + +module.exports.additionalFiles = additionalFiles; diff --git a/tools/dev-server.js b/tools/dev-server.js index 9d9255b6eb..45fdf1ab0e 100644 --- a/tools/dev-server.js +++ b/tools/dev-server.js @@ -2,8 +2,7 @@ * This tools lets one effectively run: * $ npm run build:w3c * $ npm run server - * $ npm run test:unit - * $ npm run test:integration + * $ npm run test:unit + test:integration * with ability to: * - build/test when a file changes * - build/test on a single keypress (interactive mode) @@ -22,24 +21,29 @@ const sade = require("sade"); const serveConfig = require("../serve.json"); const { Builder } = require("./builder.js"); -const KARMA_PORT_UNIT_TESTS = 9876; -const KARMA_PORT_INTEGRATION_TESTS = 9877; +const KARMA_PORT = 9876; const SERVE_PORT = 5000; -const TESTS_DIR = path.join(__dirname, "..", "tests"); class KarmaServer { /** - * @param {string} configFile * @param {number} port - * @param {string} label * @param {string} [browser] */ - constructor(configFile, port, label, browser, grep = "") { - this._label = label; + constructor(port, browser, grep = "") { const browsers = browser ? [browser] : undefined; + + const files = [ + ...require("../tests/karma.conf.base.js").files, + ...require("../tests/unit/karma.conf.js").additionalFiles, + ...require("../tests/spec/karma.conf.js").additionalFiles, + ]; + const configFile = require.resolve("../tests/karma.conf.base.js"); + this._karmaConfig = karma.config.parseConfig(configFile, { browsers, port, + files, + basePath: path.join(__dirname, ".."), autoWatch: false, logLevel: karma.constants.LOG_WARN, client: { @@ -67,8 +71,6 @@ class KarmaServer { async run() { if (this._isActive) return; - process.stdout.write(boxen(`Running ${this._label}`, { dimBorder: true })); - this._isActive = true; karma.runner.run(this._karmaConfig, () => {}); await new Promise(resolve => @@ -90,20 +92,7 @@ sade("./tools/dev-server.js", true) async function run(args) { let isActive = false; - const unitTestServer = new KarmaServer( - path.join(TESTS_DIR, "unit/karma.conf.js"), - KARMA_PORT_UNIT_TESTS, - "Unit Tests", - args.browser, - args.grep - ); - const integrationTestServer = new KarmaServer( - path.join(TESTS_DIR, "spec/karma.conf.js"), - KARMA_PORT_INTEGRATION_TESTS, - "Integration Tests", - args.browser, - args.grep - ); + const karmaServer = new KarmaServer(KARMA_PORT, args.browser, args.grep); const devServer = createServer((req, res) => serve(req, res, serveConfig)); devServer.on("error", onError); @@ -122,8 +111,11 @@ async function run(args) { devServer.listen(SERVE_PORT); printWelcomeMessage(args); - await Promise.all([unitTestServer.start(), integrationTestServer.start()]); - await buildAndTest({ profile: args.profile }); + + await karmaServer.start(); + if (!args.interactive) { + await buildAndTest(); + } function registerStdinHandler() { // https://stackoverflow.com/a/12506613 @@ -141,8 +133,7 @@ async function run(args) { switch (key) { case "\u0003": // ctrl-c (end of text) case "q": - await unitTestServer.stop(); - await integrationTestServer.stop(); + await karmaServer.stop(); return process.exit(0); case "t": return await buildAndTest(); @@ -156,20 +147,14 @@ async function run(args) { }); } - async function buildAndTest(options = {}) { - const { - preventBuild = false, - unitTests = true, - integrationTests = true, - } = options; + async function buildAndTest({ preventBuild = false } = {}) { if (isActive) return; try { isActive = true; if (!preventBuild) { await Builder.build({ name: args.profile, debug: true }); } - if (unitTests) await unitTestServer.run(); - if (integrationTests) await integrationTestServer.run(); + await karmaServer.run(); } catch (err) { console.error(colors.red(err.stack)); } finally { @@ -179,30 +164,20 @@ async function run(args) { async function onError(err) { console.error(colors.red(err.stack)); - await unitTestServer.stop(); - await integrationTestServer.stop(); + await karmaServer.stop(); process.exit(1); } async function onFileChange(_event, file) { const preventBuild = file.startsWith("tests"); - const unitTests = - !file.startsWith("tests") || file.startsWith("tests/unit"); - const integrationTests = - !file.startsWith("tests") || file.startsWith("tests/spec"); - - await buildAndTest({ preventBuild, unitTests, integrationTests }); + await buildAndTest({ preventBuild }); } } function printWelcomeMessage(args) { const messages = [ ["dev server", `http://localhost:${SERVE_PORT}/examples/`], - ["karma (unit tests)", `http://localhost:${KARMA_PORT_UNIT_TESTS}`], - [ - "karma (integration tests)", - `http://localhost:${KARMA_PORT_INTEGRATION_TESTS}`, - ], + ["karma server", `http://localhost:${KARMA_PORT}`], [ "file watcher", `${args.interactive ? "NOT " : ""}watching for changes...`,