From c749a760bcec27136035d9cfdca0a3de6768a363 Mon Sep 17 00:00:00 2001 From: PC Drew Date: Tue, 17 Jan 2017 20:07:36 -0700 Subject: [PATCH] Restore callerDependency to it's original state. Create a mock callerDependency and add mockery to handle properly requiring the mock instead of the original callerDependency. --- packages/react-server-cli/gulpfile.babel.js | 1 - packages/react-server-cli/package.json | 1 + .../compileClient/compileClientSpec.js | 20 ++++++++++++++++--- .../src/callerDependency-Mock.js | 8 ++++++++ .../react-server-cli/src/callerDependency.js | 13 ++---------- 5 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 packages/react-server-cli/src/callerDependency-Mock.js diff --git a/packages/react-server-cli/gulpfile.babel.js b/packages/react-server-cli/gulpfile.babel.js index 8342393a5..57eedff7a 100644 --- a/packages/react-server-cli/gulpfile.babel.js +++ b/packages/react-server-cli/gulpfile.babel.js @@ -37,7 +37,6 @@ gulp.task("eslint", [], () => { }); gulp.task("test", ["default", "eslint"], () => { - process.env.NODE_ENV = "__react-server-cli-unit-test__"; // eslint-disable-line no-process-env return gulp.src(getSpecGlob("target/__tests__/**/")) .pipe(jasmine({})); }); diff --git a/packages/react-server-cli/package.json b/packages/react-server-cli/package.json index 28351aeaa..be7b32bdb 100644 --- a/packages/react-server-cli/package.json +++ b/packages/react-server-cli/package.json @@ -61,6 +61,7 @@ "gulp-eslint": "^3.0.1", "gulp-jasmine": "^2.4.2", "memory-stream": "0.0.3", + "mockery": "^2.0.0", "nsp": "^2.6.2", "react-hot-loader": "^1.3.1", "react-server-gulp-module-tagger": "^0.4.10", diff --git a/packages/react-server-cli/src/__tests__/compileClient/compileClientSpec.js b/packages/react-server-cli/src/__tests__/compileClient/compileClientSpec.js index b5e63e016..2d2627d7a 100644 --- a/packages/react-server-cli/src/__tests__/compileClient/compileClientSpec.js +++ b/packages/react-server-cli/src/__tests__/compileClient/compileClientSpec.js @@ -1,11 +1,25 @@ import fs from "fs"; const MemoryStream = require('memory-stream'); import path from "path"; - -import { writeWebpackCompatibleRoutesFile } from "../../compileClient"; +import mockery from "mockery"; describe("compileClient", () => { - let mockFs; + let mockFs, + writeWebpackCompatibleRoutesFile; + + beforeAll(() => { + mockery.registerSubstitute('./callerDependency', './callerDependency-Mock'); + mockery.enable({ + useCleanCache: true, + warnOnUnregistered: false, + }); + + writeWebpackCompatibleRoutesFile = require("../../compileClient").writeWebpackCompatibleRoutesFile; + }); + + afterAll(() => { + mockery.disable(); + }); describe("writes client routes file for Webpack", () => { const pathStringTests = [ diff --git a/packages/react-server-cli/src/callerDependency-Mock.js b/packages/react-server-cli/src/callerDependency-Mock.js new file mode 100644 index 000000000..f2ca7892b --- /dev/null +++ b/packages/react-server-cli/src/callerDependency-Mock.js @@ -0,0 +1,8 @@ +import lookup from "look-up"; +import path from "path"; + +export default function callerDependency(dep) { + // TODO: We should grab stuff based on what the routes file would get out + // of `require.resolve(dep)`. Using `process.cwd()` instead for now. + return lookup("packages/" + dep, {cwd: path.resolve(process.cwd() + '/..')}); +} diff --git a/packages/react-server-cli/src/callerDependency.js b/packages/react-server-cli/src/callerDependency.js index 0daefe155..caf5684bd 100644 --- a/packages/react-server-cli/src/callerDependency.js +++ b/packages/react-server-cli/src/callerDependency.js @@ -1,17 +1,8 @@ import lookup from "look-up"; -import path from "path"; +// NOTE: if this function changes, make sure it also changes in the 'callerDependency-Mock.js' file as well. export default function callerDependency(dep) { // TODO: We should grab stuff based on what the routes file would get out // of `require.resolve(dep)`. Using `process.cwd()` instead for now. - let cwd = process.cwd(), - lookupResult; - - if (process.env.NODE_ENV === '__react-server-cli-unit-test__') { // eslint-disable-line no-process-env - cwd = path.resolve(cwd + '/..'); - lookupResult = lookup("packages/" + dep, {cwd: cwd}); - } else { - lookupResult = lookup("node_modules/" + dep, {cwd: cwd}); - } - return lookupResult; + return lookup("node_modules/" + dep, {cwd: process.cwd()}); }