Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Commit

Permalink
SELA-301 Support Mocha without Karma as a test runner.
Browse files Browse the repository at this point in the history
  • Loading branch information
threehams committed May 3, 2016
1 parent 81f2c26 commit 92bfa56
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 18 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"babel-eslint": "6.0.4",
"eslint": "2.9.0",
"eslint-plugin-react": "5.0.1",
"jsdom": "8.5.0",
"temp": "0.8.3"
}
}
14 changes: 11 additions & 3 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,20 @@ const debugOption = {
description: "debug server side rendering with node-inspector"
};

const nodeTestOption = {
command: "-n, --mocha-only",
description: "run tests in Node.js"
};

commander
.command("start")
.description("start everything")
.option("-T, --no_tests", "ignore test hook")
.option(debugOption.command, debugOption.description)
.option(nodeTestOption.command, nodeTestOption.description)
.action(checkGluestickProject)
.action(() => notifyUpdates())
.action((options) => startAll(options.no_tests, options.debug))
.action((options) => startAll(options.no_tests, options.debug, options.mochaOnly))
.action(() => updateLastVersionUsed(currentGluestickVersion));

commander
Expand Down Expand Up @@ -126,6 +132,7 @@ commander
.command("start-test", null, {noHelp: true})
.option(firefoxOption.command, firefoxOption.description)
.option(singleRunOption.command, singleRunOption.description)
.option(nodeTestOption.command, nodeTestOption.description)
.description("start test")
.action(checkGluestickProject)
.action((options) => startTest(options))
Expand All @@ -135,6 +142,7 @@ commander
.command("test")
.option(firefoxOption.command, firefoxOption.description)
.option(singleRunOption.command, singleRunOption.description)
.option(nodeTestOption.command, nodeTestOption.description)
.description("start tests")
.action(checkGluestickProject)
.action(() => updateLastVersionUsed(currentGluestickVersion))
Expand Down Expand Up @@ -193,7 +201,7 @@ function spawnProcess (type, args=[]) {
return childProcess;
}

async function startAll(withoutTests=false, debug=false) {
async function startAll(withoutTests=false, debug=false, mochaOnly=false) {
try {
await autoUpgrade();
}
Expand All @@ -207,7 +215,7 @@ async function startAll(withoutTests=false, debug=false) {

// Start tests unless they asked us not to or we are in production mode
if (!isProduction && !withoutTests) {
spawnProcess("test");
spawnProcess("test", (mochaOnly ? ["--mocha-only"] : []));
}
}

Expand Down
27 changes: 21 additions & 6 deletions src/commands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,38 @@ const karma = require("karma");
const Server = karma.Server;
const runner = karma.runner;
const karmaConfig = require("../config/karma-config").default;

const config = karmaConfig;
const spawn = require("cross-spawn").spawn;
const mocha = require("mocha");
const path = require("path");
const CWD = process.cwd();

module.exports = function (options) {
// override to run tests in Node without Karma/Webpack
if (options.mochaOnly) {
const helperPath = path.join(__dirname, "..", "lib", "testHelperMocha.js");
const testPath = `${CWD}/test/**/*.js`;
const mochaEnv = Object.assign({}, process.env, { NODE_PATH: 'src', NODE_ENV: 'test'});
spawn(
path.join(__dirname, "..", "..", "node_modules", ".bin", "mocha"),
["--require", helperPath, "--reporter", "dot", `${testPath}`, "--watch"],
{ stdio: "inherit", env: mochaEnv }
);
return;
}

// override browser setting to use firefox instead of Chrome if specified
if (options.firefox) {
config.browsers = ["Firefox"];
karmaConfig.browsers = ["Firefox"];
}

if (options.single) {
config.singleRun = options.single;
karmaConfig.singleRun = options.single;
}

const server = new Server(config);
const server = new Server(karmaConfig);

server.start();
server.on("browsers_ready", function () {
runner.run(config, () => {});
runner.run(karmaConfig, () => {});
});
};
2 changes: 1 addition & 1 deletion src/config/karma-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const CWD = process.cwd();
const webpackIsomorphicToolsPlugin = new WebpackIsomorphicToolsPlugin(require("./webpack-isomorphic-tools-config")).development(true);

const preprocessors = {};
const helperPath = path.resolve(__dirname, "../lib/testHelper.js");
const helperPath = path.resolve(__dirname, "../lib/testHelperKarma.js");
preprocessors[helperPath] = ["webpack", "sourcemap"];

export default {
Expand Down
9 changes: 9 additions & 0 deletions src/lib/testHelperKarma.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* global TEST_PATH */
/* global SRC_PATH */
require('./testHelperShared');

const context = require.context(TEST_PATH, true, /\.test\.js$/);
context.keys().forEach(context);

const srcContext = require.context(SRC_PATH, true, /\.js$/);
srcContext.keys().forEach(srcContext);
29 changes: 29 additions & 0 deletions src/lib/testHelperMocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* global global */
require('babel-register');

function noop() {
return null;
}

const jsdom = require("jsdom").jsdom;

global.document = jsdom("");
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
if (typeof global[property] === "undefined") {
global[property] = document.defaultView[property];
}
});

global.navigator = {
userAgent: "node.js"
};

require.extensions[".scss"] = noop;
require.extensions[".png"] = noop;
require.extensions[".svg"] = noop;

require('./testHelperShared');

global.expect = require("chai").expect;
global.sinon = require("sinon");
8 changes: 0 additions & 8 deletions src/lib/testHelper.js → src/lib/testHelperShared.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
/*global TEST_PATH*/
/*global SRC_PATH*/
require("babel-polyfill");
const React = require("react");
const ReactDOM = require("react-dom");
const ReactTestUtils = require("react-addons-test-utils");
global.React = React;
global.ReactDOM = ReactDOM;
global.TestUtils = ReactTestUtils;

const context = require.context(TEST_PATH, true, /\.test\.js$/);
context.keys().forEach(context);

const srcContext = require.context(SRC_PATH, true, /\.js$/);
srcContext.keys().forEach(srcContext);

0 comments on commit 92bfa56

Please sign in to comment.