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 multiple aliases for src + assets.
Browse files Browse the repository at this point in the history
Filter to only run .test.js files.
Add lots of comments around the wackier bits.
  • Loading branch information
threehams committed May 3, 2016
1 parent 92bfa56 commit 5385f11
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
},
"peerDependencies": {},
"devDependencies": {
"app-module-path": "1.0.6",
"babel-eslint": "6.0.4",
"eslint": "2.9.0",
"eslint-plugin-react": "5.0.1",
Expand Down
13 changes: 10 additions & 3 deletions src/commands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ 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'});
const testPath = `${CWD}/test/**/*.test.js`;

// In order to support the Webpack `assets` alias and `src` root, the node path needs to be set up to be
// at the base directory. This allows all module resolves to be relative to the project, rather than to
// Gluestick's CWD.
const mochaEnv = Object.assign({}, process.env, { NODE_PATH: CWD, NODE_ENV: 'test' });

// Currently defaulting to "dot" reporter and watch. This can be improved to allow a reporter override
// and single-run.
spawn(
path.join(__dirname, "..", "..", "node_modules", ".bin", "mocha"),
["--require", helperPath, "--reporter", "dot", `${testPath}`, "--watch"],
["--require", helperPath, "--reporter", "dot", "debug", `${testPath}`, "--watch"],
{ stdio: "inherit", env: mochaEnv }
);
return;
Expand Down
37 changes: 30 additions & 7 deletions src/lib/testHelperMocha.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
/* global global */
require('babel-register');

function noop() {
return null;
}
const CWD = process.cwd();
const IS_WINDOWS = process.platform === "win32";

// Because we have multiple resolves and an alias set up in Webpack, we need to modify require.main.paths at runtime to
// deal with that without the use of a bundler. Unlike require.paths, this is documented and should be safe. However,
// modifying it after anything has been require()d causes cache issues.
// Therefore:
//
// **ACHTUNG** THIS SHOULD ALWAYS BE RUN BEFORE ANY OTHER REQUIRE()s.
// Cannot use path.join here since it would take a require()!
require('app-module-path').addPath(CWD + (IS_WINDOWS ? '\'' : '/') + 'assets');
require('app-module-path').addPath(CWD + (IS_WINDOWS ? '\'' : '/') + 'src');

require('babel-register');

// Set up jsdom for component rendering through Enzyme.
// This code is directly from Enzyme's docs.
const jsdom = require("jsdom").jsdom;

global.document = jsdom("");
Expand All @@ -19,9 +31,20 @@ global.navigator = {
userAgent: "node.js"
};

require.extensions[".scss"] = noop;
require.extensions[".png"] = noop;
require.extensions[".svg"] = noop;
function noop() {
return null;
}

// Note: require.extensions is deprecated, but is currently the only way to filter out require() of non-JS modules
// without the expense and complication of a pre-compilation step.
//
// From the docs:
//
// "Since the Module system is locked, this feature will probably never go away. However, it may have subtle bugs
// and complexities that are best left untouched."
[".css", ".jpg", ".png", ".scss", ".svg"].forEach((extension) => {
require.extensions[extension] = noop;
});

require('./testHelperShared');

Expand Down

0 comments on commit 5385f11

Please sign in to comment.