Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
chore(tools/dev-server): run unit tests, integration tests together (s…
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi authored Jun 12, 2021
1 parent d346d80 commit 637ca3d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 96 deletions.
65 changes: 35 additions & 30 deletions tests/karma.conf.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -87,5 +90,7 @@ module.exports = config => {
options.plugins = ["karma-*"].concat(localPlugins);
}

config.set(options);
return options;
};
module.exports.files = files;
22 changes: 14 additions & 8 deletions tests/spec/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
22 changes: 14 additions & 8 deletions tests/unit/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
75 changes: 25 additions & 50 deletions tools/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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: {
Expand Down Expand Up @@ -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 =>
Expand All @@ -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);

Expand All @@ -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
Expand All @@ -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();
Expand All @@ -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 {
Expand All @@ -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...`,
Expand Down

0 comments on commit 637ca3d

Please sign in to comment.