Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix tasks to run only if their dependent opt packages are installed #1753

Merged
merged 7 commits into from
Nov 13, 2020
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
75 changes: 75 additions & 0 deletions packages/xarc-app-dev/src/config/karma/entry-xarc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"use strict";

/* eslint-disable no-var */

/**
* All requires in this file will be processed by webpack, which is unforgiving
* about missing dependencies and will generate hard module not found errors,
* and try/catch doesn't work. Further, these module not found issues could
* cause very weird and unexpected errors from webpack.
*/

require("core-js");
require("regenerator-runtime/runtime");

/**
* Test setup for client-side tests.
*
* Intended for:
* - Karma tests: `npm run test-client`
* - Browser tests: `http://localhost:3000/test/client/test.html`
*/
/*globals window:false*/

/**
* Install enzyme along with an Adapter corresponding to React 16
* Configure enzyme to use the adapter using the top level configure(...) API
*/
var enzyme = require("@xarc/opt-karma/lib/enzyme");
var Adapter = require("@xarc/opt-karma/lib/enzyme-adapter-react-16");
enzyme.configure({ adapter: new Adapter() });

/*
* We need a global sinon to maintain compatibility
* with existing test suites. However, this will be
* removed in the future.
*/
var sinon = require("@xarc/opt-karma/lib/sinon");
window.sinon = sinon;

// --------------------------------------------------------------------------
// Chai / Sinon / Mocha configuration.
// --------------------------------------------------------------------------
// Exports
var chai = require("@xarc/opt-karma/lib/chai");
window.expect = chai.expect;

var sinonChai = require("@xarc/opt-karma/lib/sinon-chai");
chai.use(sinonChai);

var chaiShallowly = require("@xarc/opt-karma/lib/chai-shallowly");
chai.use(chaiShallowly);

// Mocha (part of static include).
window.mocha.setup({
ui: "bdd",
bail: false
});

// --------------------------------------------------------------------------
// Bootstrap
// --------------------------------------------------------------------------
// Use webpack to include all app code _except_ the entry point so we can get
// code coverage in the bundle, whether tested or not.
// NOTE: No need to specify src even in src mode since webpack should handle that already
var srcReq = require.context("client", true, /^((?!app).)(!(spec|test))*\.(jsx|js)?$/);
srcReq.keys().map(srcReq);

// Use webpack to infer and `require` tests automatically only for test/client
var testsReq = require.context("test/client", true, /\.spec.jsx?$/);
testsReq.keys().map(testsReq);

// Only start mocha in browser.
if (!window.__karma__) {
window.mocha.run();
}
72 changes: 55 additions & 17 deletions packages/xarc-app-dev/src/config/karma/karma.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,60 @@ const loadUserConfig = require("./util/load-user-config");
const browserSettings = require("./browser-settings");
const loadElectrodeDll = require("./util/load-electrode-dll");

function getXarcOptPlugins() {
try {
require.resolve("@xarc/opt-karma");
return [
"@xarc/opt-karma/plugins/chrome-launcher",
"@xarc/opt-karma/plugins/coverage",
"@xarc/opt-karma/plugins/firefox-launcher",
"@xarc/opt-karma/plugins/ie-launcher",
"@xarc/opt-karma/plugins/intl-shim",
"@xarc/opt-karma/plugins/mocha",
"@xarc/opt-karma/plugins/mocha-reporter",
"@xarc/opt-karma/plugins/safari-launcher",
"@xarc/opt-karma/plugins/sonarqube-unit-reporter",
"@xarc/opt-karma/plugins/sourcemap-loader",
"@xarc/opt-karma/plugins/spec-reporter",
"@xarc/opt-karma/plugins/webpack"
];
} catch (err) {
return false;
}
}

function getArchetypeOptPlugins() {
try {
require.resolve("electrode-archetype-opt-karma");
return [
"karma-chrome-launcher",
"karma-coverage",
"karma-firefox-launcher",
"karma-ie-launcher",
"karma-intl-shim",
"karma-mocha",
"karma-mocha-reporter",
"karma-safari-launcher",
"karma-sonarqube-unit-reporter",
"karma-sourcemap-loader",
"karma-spec-reporter",
"karma-webpack"
];
} catch (err) {
return false;
}
}

let MAIN_PATH;

try {
MAIN_PATH = require.resolve(Path.resolve("test/karma-entry"));
} catch (err) {
MAIN_PATH = require.resolve("./entry.js");
if (getXarcOptPlugins()) {
MAIN_PATH = require.resolve("./entry-xarc.js");
} else {
MAIN_PATH = require.resolve("./entry.js");
}
}

console.log(`KARMA will use entry file ${MAIN_PATH}`);
Expand All @@ -36,26 +84,16 @@ function loadWebpackConfig() {
}

module.exports = function(config) {
let plugins = getXarcOptPlugins() || getArchetypeOptPlugins();
if (!plugins) {
console.error("ERROR: @xarc/opt-karma not found - running karma tests is not possible");
plugins = [];
}
const settings = {
basePath: process.cwd(),
frameworks: ["mocha", "intl-shim"],
files: DLL_PATHS.concat(MAIN_PATH),
plugins: [
"karma-chrome-launcher",
"karma-coverage",
"karma-firefox-launcher",
"karma-ie-launcher",
"karma-intl-shim",
"karma-mocha",
"karma-mocha-reporter",
"karma-phantomjs-shim",
"karma-phantomjs-launcher",
"karma-safari-launcher",
"karma-sonarqube-unit-reporter",
"karma-sourcemap-loader",
"karma-spec-reporter",
"karma-webpack"
],
plugins,
preprocessors: PREPROCESSORS,
webpack: loadWebpackConfig(),
webpackServer: {
Expand Down
2 changes: 0 additions & 2 deletions packages/xarc-app-dev/src/config/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ export function checkOptArchetypeInAppDep(dependencies, isDev = undefined) {
export const getUserConfigOptions = (packageNames, devPackageNames) => {
return {
reactLib: "react",
karma: true,
sass: false,
...optionalRequire(Path.resolve("archetype/config"), { default: {} }).options,
//
// Check for any optional archetype in application's devDependencies or dependencies
Expand Down
59 changes: 27 additions & 32 deletions packages/xarc-app-dev/src/lib/dev-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const getArchetype = require("../config/archetype");
const ck = require("chalker");
const xaa = require("xaa");
const { psChildren } = require("ps-get");
const detectCssModule = require("@xarc/webpack/lib/util/detect-css-module");
const optFlow = optionalRequire("electrode-archetype-opt-flow");
const { getWebpackStartConfig, setWebpackProfile } = require("@xarc/webpack/lib/util/custom-check");
const chokidar = require("chokidar");
Expand Down Expand Up @@ -1022,7 +1021,7 @@ You only need to run this if you are doing something not through the xarc tasks.
});
}

if (archetype.options.eslint !== false) {
if (archetype.options.eslint) {
Object.assign(tasks, {
lint: xclap2.concurrent(
"lint-client",
Expand Down Expand Up @@ -1068,25 +1067,20 @@ You only need to run this if you are doing something not through the xarc tasks.
}
});
} else {
const lintDisabled = () => {
logger.info(`eslint tasks are disabled because @xarc/opt-eslint is not installed.
Please add it to your devDependencies to enable eslint.`);
};
Object.assign(tasks, {
lint: () => {
logger.info("Disabling ESLint tasks since archetype config options.eslint === false");
},
"lint-server": () => {
logger.info(
"Disabling ESLint task 'lint-server' since archetype config options.eslint === false"
);
},
"lint-server-test": () => {
logger.info(
"Disabling ESLint task 'lint-server-test' since archetype config options.eslint === false"
);
}
lint: lintDisabled,
"lint-server": lintDisabled,
"lint-server-test": lintDisabled
});
}

if (archetype.options.karma !== false && archetype.options.mocha !== false) {
if (archetype.options.karma) {
const noSingleRun = process.argv.indexOf("--no-single-run") >= 0 ? "--no-single-run" : "";

Object.assign(tasks, {
".karma.test-frontend": {
desc: false,
Expand Down Expand Up @@ -1145,9 +1139,8 @@ You only need to run this if you are doing something not through the xarc tasks.
});
} else {
const karmaTasksDisabled = () => {
logger.info(
"Disabling karma test tasks since archetype config options.karma === false or options.mocha === false"
);
logger.info(`Karma tests disabled because @xarc/opt-karma is not installed.
Please add it to your devDependencies to enable running karma tests`);
};
Object.assign(tasks, {
".karma.test-frontend": karmaTasksDisabled,
Expand All @@ -1157,7 +1150,8 @@ You only need to run this if you are doing something not through the xarc tasks.
".karma.test-frontend-dev-watch": karmaTasksDisabled
});
}
if (archetype.options.jest !== false) {

if (archetype.options.jest) {
Object.assign(tasks, {
jest: {
desc: "Run jest tests (--inspect-brk to start debugger)",
Expand Down Expand Up @@ -1206,10 +1200,15 @@ You only need to run this if you are doing something not through the xarc tasks.
}
});
} else {
logger.info("Disabling jest test tasks since archetype config options.jest === false");
Object.assign(tasks, {
jest: () => {
logger.info(`Running jest tests is not enabled because @xarc/opt-jest is not installed.
Please add it to your devDependencies to enable running jest tests.`);
}
});
}

if (archetype.options.mocha !== false) {
if (archetype.options.mocha) {
Object.assign(tasks, {
"test-server-cov": () => {
if (shell.test("-d", "test/server")) {
Expand All @@ -1233,17 +1232,13 @@ You only need to run this if you are doing something not through the xarc tasks.
}
});
} else {
const mochaTestDisabled = () => {
logger.info(`Running tests with mocha disabled because @xarc/opt-mocha is not installed
Please add it to your devDependencies to enable running tests with mocha`);
};
Object.assign(tasks, {
"test-server-cov": () => {
logger.info(
"Disabling Mocha task 'test-server-cov', since archetype config options.mocha === false"
);
},
"test-server-dev": () => {
logger.info(
"Disabling Mocha task 'test-server-dev', since archetype config options.mocha === false"
);
}
"test-server-cov": mochaTestDisabled,
"test-server-dev": mochaTestDisabled
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/lib/chai-shallowly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("chai-shallowly");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/lib/chai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("chai");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/lib/enzyme-adapter-react-16.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("enzyme-adapter-react-16");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/lib/enzyme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("enzyme");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/lib/sinon-chai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("sinon-chai");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/lib/sinon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("sinon");
4 changes: 3 additions & 1 deletion packages/xarc-opt-karma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"url": "https://github.com/electrode-io/electrode/issues"
},
"files": [
"xarc-opt-check.js"
"xarc-opt-check.js",
"plugins",
"lib"
],
"publishConfig": {
"registry": "https://registry.npmjs.org/",
Expand Down
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/plugins/chrome-launcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("karma-chrome-launcher");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/plugins/coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("karma-coverage");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/plugins/firefox-launcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("karma-firefox-launcher");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/plugins/ie-launcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("karma-ie-launcher");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/plugins/intl-shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("karma-intl-shim");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/plugins/mocha-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("karma-mocha-reporter");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/plugins/mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("karma-mocha");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/plugins/safari-launcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("karma-safari-launcher");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("karma-sonarqube-unit-reporter");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/plugins/sourcemap-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("karma-sourcemap-loader");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/plugins/spec-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("karma-spec-reporter");
1 change: 1 addition & 0 deletions packages/xarc-opt-karma/plugins/webpack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("karma-webpack");
Loading