From 1d16729f3480a3f182b1bde00c1eab151418bb34 Mon Sep 17 00:00:00 2001 From: Kaitlin Rathwell Date: Wed, 11 Jan 2017 13:37:22 +0100 Subject: [PATCH 01/10] Output errors from the routes --- packages/react-server-cli/src/run.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-server-cli/src/run.js b/packages/react-server-cli/src/run.js index 53a4b68e7..d92f43abf 100644 --- a/packages/react-server-cli/src/run.js +++ b/packages/react-server-cli/src/run.js @@ -27,7 +27,7 @@ export default function run(options = {}) { try { options.routes = require(options.routesPath); } catch (e) { - // Pass. Commands need to check for routes themselves. + console.error(e); } options.outputUrl = jsUrl || `${httpsOptions ? "https" : "http"}://${host}:${jsPort}/`; From 544ca6bc2eed7b221a3c9622320cc9b93a2b5198 Mon Sep 17 00:00:00 2001 From: Kaitlin Rathwell Date: Wed, 11 Jan 2017 18:06:01 +0100 Subject: [PATCH 02/10] Remove try catch completely --- packages/react-server-cli/src/run.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/react-server-cli/src/run.js b/packages/react-server-cli/src/run.js index d92f43abf..ace78d05d 100644 --- a/packages/react-server-cli/src/run.js +++ b/packages/react-server-cli/src/run.js @@ -23,12 +23,7 @@ export default function run(options = {}) { options.routesPath = path.resolve(process.cwd(), routesFile); options.routesDir = path.dirname(options.routesPath); - - try { - options.routes = require(options.routesPath); - } catch (e) { - console.error(e); - } + options.routes = require(options.routesPath); options.outputUrl = jsUrl || `${httpsOptions ? "https" : "http"}://${host}:${jsPort}/`; From d9141e06e7bd3b6e844a7eb56c06e322bf03570a Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Thu, 12 Jan 2017 17:26:28 +0100 Subject: [PATCH 03/10] Disable routes require when command is 'init' --- packages/react-server-cli/src/run.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-server-cli/src/run.js b/packages/react-server-cli/src/run.js index ace78d05d..fe614da2c 100644 --- a/packages/react-server-cli/src/run.js +++ b/packages/react-server-cli/src/run.js @@ -23,7 +23,7 @@ export default function run(options = {}) { options.routesPath = path.resolve(process.cwd(), routesFile); options.routesDir = path.dirname(options.routesPath); - options.routes = require(options.routesPath); + if (options.command !== 'init') options.routes = require(options.routesPath); options.outputUrl = jsUrl || `${httpsOptions ? "https" : "http"}://${host}:${jsPort}/`; From dec327bb3a79b49e398370b2795586d61698b74c Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Thu, 12 Jan 2017 17:29:13 +0100 Subject: [PATCH 04/10] Conform to code style, add comment --- packages/react-server-cli/src/run.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/react-server-cli/src/run.js b/packages/react-server-cli/src/run.js index fe614da2c..3d138ce38 100644 --- a/packages/react-server-cli/src/run.js +++ b/packages/react-server-cli/src/run.js @@ -23,7 +23,11 @@ export default function run(options = {}) { options.routesPath = path.resolve(process.cwd(), routesFile); options.routesDir = path.dirname(options.routesPath); - if (options.command !== 'init') options.routes = require(options.routesPath); + + // No routes file available when performing init command + if (options.command !== 'init') { + options.routes = require(options.routesPath); + } options.outputUrl = jsUrl || `${httpsOptions ? "https" : "http"}://${host}:${jsPort}/`; From c44107cd59415f54711ebbff6919ec232f9ec2f1 Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Thu, 26 Jan 2017 19:07:11 +0100 Subject: [PATCH 05/10] Add integration tests for react-server-cli (#1) * Start setup of react-server-cli integration tests * Run tests in temp directories * Ignore temp directories generated by tests --- packages/react-server-cli/.gitignore | 1 + packages/react-server-cli/package.json | 17 +++- packages/react-server-cli/test/commands.js | 97 +++++++++++++++++++ .../commands/start-basic/in-files/routes.json | 4 + .../commands/start-basic/options.json | 4 + .../start-missing-routes/options.json | 4 + .../in-files/.reactserverrc | 5 + .../in-files/customRoutes.js | 3 + .../commands/start-reactserverrc/options.json | 4 + .../in-files/routes.js | 1 + .../start-routes-with-error/options.json | 4 + 11 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 packages/react-server-cli/test/commands.js create mode 100644 packages/react-server-cli/test/fixtures/commands/start-basic/in-files/routes.json create mode 100644 packages/react-server-cli/test/fixtures/commands/start-basic/options.json create mode 100644 packages/react-server-cli/test/fixtures/commands/start-missing-routes/options.json create mode 100644 packages/react-server-cli/test/fixtures/commands/start-reactserverrc/in-files/.reactserverrc create mode 100644 packages/react-server-cli/test/fixtures/commands/start-reactserverrc/in-files/customRoutes.js create mode 100644 packages/react-server-cli/test/fixtures/commands/start-reactserverrc/options.json create mode 100644 packages/react-server-cli/test/fixtures/commands/start-routes-with-error/in-files/routes.js create mode 100644 packages/react-server-cli/test/fixtures/commands/start-routes-with-error/options.json diff --git a/packages/react-server-cli/.gitignore b/packages/react-server-cli/.gitignore index 2c085d1d2..7a4a3bb01 100644 --- a/packages/react-server-cli/.gitignore +++ b/packages/react-server-cli/.gitignore @@ -1,2 +1,3 @@ node_modules target +test/**/tmp diff --git a/packages/react-server-cli/package.json b/packages/react-server-cli/package.json index b6132d697..0ecd7ec4e 100644 --- a/packages/react-server-cli/package.json +++ b/packages/react-server-cli/package.json @@ -7,7 +7,8 @@ "prepublish": "gulp", "lint": "gulp eslint", "test": "npm run ava && gulp test && nsp check", - "ava": "ava test", + "ava": "ava", + "ava-watch": "ava --watch", "clean": "rimraf target npm-debug.log*" }, "author": "Sasha Aickin", @@ -56,18 +57,28 @@ "babel-plugin-add-module-exports": "^0.2.1", "babel-preset-react-server": "^0.4.10", "eslint-plugin-react": "^6.4.1", + "fs-readdir-recursive": "^1.0.0", "gulp": "^3.9.1", "gulp-babel": "^6.1.2", "gulp-eslint": "^3.0.1", "nsp": "^2.6.2", + "output-file-sync": "^1.1.2", + "react": "^15.4.2", + "react-dom": "^15.4.2", "react-hot-loader": "^1.3.1", + "react-server": "^0.5.1", "react-server-gulp-module-tagger": "^0.4.10", - "rimraf": "^2.5.4" + "rimraf": "^2.5.4", + "superagent": "^1.8.4" }, "ava": { "require": [ "babel-core/register" ], - "tap": true + "tap": true, + "files": [ + "test/**/*.js", + "!test/fixtures/*.js" + ] } } diff --git a/packages/react-server-cli/test/commands.js b/packages/react-server-cli/test/commands.js new file mode 100644 index 000000000..df5c86c2f --- /dev/null +++ b/packages/react-server-cli/test/commands.js @@ -0,0 +1,97 @@ +import path from 'path'; +import fs from 'fs'; +import readdirSyncRecursive from 'fs-readdir-recursive'; +import outputFileSync from 'output-file-sync'; +import child_process from 'child_process'; +import test from 'ava'; +import rimraf from 'rimraf'; + +const fixturesPath = path.join(__dirname, 'fixtures', 'commands'); + +fs.readdirSync(fixturesPath).forEach(testName => { + if (testName[0] === '.') return; + + const [, command, testType] = testName.match(/([^-]+)-(.+)/); + + test(`${command} command: ${testType}`, async t => { + const testPath = path.join(fixturesPath, testName); + const tmpPath = path.join(testPath, 'tmp'); + createAndChangeToTempDir(tmpPath); + + // Write files to temporary location + Object.entries(readDir(path.join(testPath, 'in-files'))) + .forEach(([filename, content]) => + outputFileSync(filename, content) + ); + + const { + args, + stdoutIncludes, + stderrIncludes + } = JSON.parse(fs.readFileSync(path.join(testPath, 'options.json'))); + + const server = child_process.spawn( + process.execPath, + [ + path.join(__dirname, '..', 'bin', 'react-server-cli'), + ...args + ] + ); + + let stdout = ''; + let stderr = ''; + + server.stdout.on('data', chunk => stdout += chunk); + server.stderr.on('data', chunk => stderr += chunk); + + const frequency = 100; + let elapsed = 0; + + await new Promise(resolve => { + const checkForExpectedOutput = setInterval(() => { + // Increment the elapsed time if neither stdout nor stderr includes the expected content and the time limit hasn't been reached. + if ( + ( + (stdoutIncludes && !stdout.includes(stdoutIncludes)) || + (stderrIncludes && !stderr.includes(stderrIncludes)) + ) && + elapsed < 5000 + ) { + elapsed += frequency; + return; + } + + clearInterval(checkForExpectedOutput); + resolve(); + }, frequency); + }); + + if (stdoutIncludes) t.true(stdout.includes(stdoutIncludes), 'stdout includes expected output'); + if (stderrIncludes) t.true(stderr.includes(stderrIncludes), 'stderr includes expected output'); + + server.kill(); + + // Remove temporary directory after the test + rimraf.sync(tmpPath); + }); +}); + +function createAndChangeToTempDir(tmpPath){ + if (fs.existsSync(tmpPath)) rimraf.sync(tmpPath); + fs.mkdirSync(tmpPath); + process.chdir(tmpPath); +} + +function noDotDirectory(x){ + return x !== '.'; +} + +function readDir(dirPath){ + const files = {}; + if (fs.existsSync(dirPath)) { + readdirSyncRecursive(dirPath, noDotDirectory).forEach(filename => { + files[filename] = fs.readFileSync(path.join(dirPath, filename)); + }); + } + return files; +}; diff --git a/packages/react-server-cli/test/fixtures/commands/start-basic/in-files/routes.json b/packages/react-server-cli/test/fixtures/commands/start-basic/in-files/routes.json new file mode 100644 index 000000000..c612b7739 --- /dev/null +++ b/packages/react-server-cli/test/fixtures/commands/start-basic/in-files/routes.json @@ -0,0 +1,4 @@ +{ + "middleware": [], + "routes": {} +} diff --git a/packages/react-server-cli/test/fixtures/commands/start-basic/options.json b/packages/react-server-cli/test/fixtures/commands/start-basic/options.json new file mode 100644 index 000000000..78992f424 --- /dev/null +++ b/packages/react-server-cli/test/fixtures/commands/start-basic/options.json @@ -0,0 +1,4 @@ +{ + "args": ["start"], + "stdoutIncludes": "Started HTML server over HTTP on" +} diff --git a/packages/react-server-cli/test/fixtures/commands/start-missing-routes/options.json b/packages/react-server-cli/test/fixtures/commands/start-missing-routes/options.json new file mode 100644 index 000000000..3ce125765 --- /dev/null +++ b/packages/react-server-cli/test/fixtures/commands/start-missing-routes/options.json @@ -0,0 +1,4 @@ +{ + "args": ["start"], + "stderrIncludes": "Cannot find module" +} diff --git a/packages/react-server-cli/test/fixtures/commands/start-reactserverrc/in-files/.reactserverrc b/packages/react-server-cli/test/fixtures/commands/start-reactserverrc/in-files/.reactserverrc new file mode 100644 index 000000000..816d3e538 --- /dev/null +++ b/packages/react-server-cli/test/fixtures/commands/start-reactserverrc/in-files/.reactserverrc @@ -0,0 +1,5 @@ +{ + "routesFile": "customRoutes.js", + "port": 4000, + "jsPort": 4001 +} diff --git a/packages/react-server-cli/test/fixtures/commands/start-reactserverrc/in-files/customRoutes.js b/packages/react-server-cli/test/fixtures/commands/start-reactserverrc/in-files/customRoutes.js new file mode 100644 index 000000000..3e8c13173 --- /dev/null +++ b/packages/react-server-cli/test/fixtures/commands/start-reactserverrc/in-files/customRoutes.js @@ -0,0 +1,3 @@ +module.exports = { + routes: {} +}; diff --git a/packages/react-server-cli/test/fixtures/commands/start-reactserverrc/options.json b/packages/react-server-cli/test/fixtures/commands/start-reactserverrc/options.json new file mode 100644 index 000000000..78992f424 --- /dev/null +++ b/packages/react-server-cli/test/fixtures/commands/start-reactserverrc/options.json @@ -0,0 +1,4 @@ +{ + "args": ["start"], + "stdoutIncludes": "Started HTML server over HTTP on" +} diff --git a/packages/react-server-cli/test/fixtures/commands/start-routes-with-error/in-files/routes.js b/packages/react-server-cli/test/fixtures/commands/start-routes-with-error/in-files/routes.js new file mode 100644 index 000000000..60245c063 --- /dev/null +++ b/packages/react-server-cli/test/fixtures/commands/start-routes-with-error/in-files/routes.js @@ -0,0 +1 @@ +undeclaredVariable(); diff --git a/packages/react-server-cli/test/fixtures/commands/start-routes-with-error/options.json b/packages/react-server-cli/test/fixtures/commands/start-routes-with-error/options.json new file mode 100644 index 000000000..6448c3520 --- /dev/null +++ b/packages/react-server-cli/test/fixtures/commands/start-routes-with-error/options.json @@ -0,0 +1,4 @@ +{ + "args": ["start", "--routes-file", "routes.js"], + "stderrIncludes": "ReferenceError: undeclaredVariable is not defined" +} From f1bf63bc8c72d615960fd6e58f37ec62128828eb Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Fri, 27 Jan 2017 11:40:10 +0100 Subject: [PATCH 06/10] Improve error message --- packages/react-server-cli/src/run.js | 10 +++++++++- .../commands/start-missing-routes/options.json | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/react-server-cli/src/run.js b/packages/react-server-cli/src/run.js index 3d138ce38..32624546e 100644 --- a/packages/react-server-cli/src/run.js +++ b/packages/react-server-cli/src/run.js @@ -26,7 +26,15 @@ export default function run(options = {}) { // No routes file available when performing init command if (options.command !== 'init') { - options.routes = require(options.routesPath); + try { + options.routes = require(options.routesPath); + } catch (e) { + if (e.code === 'MODULE_NOT_FOUND') { + console.error(chalk.red(`Failed to load routes file at ${options.routesPath}`)); + } + + throw e; + } } options.outputUrl = jsUrl || `${httpsOptions ? "https" : "http"}://${host}:${jsPort}/`; diff --git a/packages/react-server-cli/test/fixtures/commands/start-missing-routes/options.json b/packages/react-server-cli/test/fixtures/commands/start-missing-routes/options.json index 3ce125765..b97129ac3 100644 --- a/packages/react-server-cli/test/fixtures/commands/start-missing-routes/options.json +++ b/packages/react-server-cli/test/fixtures/commands/start-missing-routes/options.json @@ -1,4 +1,4 @@ { "args": ["start"], - "stderrIncludes": "Cannot find module" + "stderrIncludes": "Failed to load routes file at" } From 74513c1ab5c81690f5597ed6db32bd96d09cad54 Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Fri, 27 Jan 2017 11:40:26 +0100 Subject: [PATCH 07/10] Add comment --- packages/react-server-cli/test/commands.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/react-server-cli/test/commands.js b/packages/react-server-cli/test/commands.js index df5c86c2f..f27f3673a 100644 --- a/packages/react-server-cli/test/commands.js +++ b/packages/react-server-cli/test/commands.js @@ -47,6 +47,7 @@ fs.readdirSync(fixturesPath).forEach(testName => { const frequency = 100; let elapsed = 0; + // Wait for the expected output or the timeout await new Promise(resolve => { const checkForExpectedOutput = setInterval(() => { // Increment the elapsed time if neither stdout nor stderr includes the expected content and the time limit hasn't been reached. From 8e73edc481dc89a024d04dff2dabe5ba30e2e5f8 Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Fri, 27 Jan 2017 15:53:01 +0100 Subject: [PATCH 08/10] Lock dependency --- packages/react-server-cli/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-server-cli/package.json b/packages/react-server-cli/package.json index 0ecd7ec4e..711a67a4a 100644 --- a/packages/react-server-cli/package.json +++ b/packages/react-server-cli/package.json @@ -69,7 +69,7 @@ "react-server": "^0.5.1", "react-server-gulp-module-tagger": "^0.4.10", "rimraf": "^2.5.4", - "superagent": "^1.8.4" + "superagent": "1.8.4" }, "ava": { "require": [ From 47582244742f1b8183e6d41405c1504de583d6ba Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Fri, 27 Jan 2017 16:06:28 +0100 Subject: [PATCH 09/10] Fix linting errors --- packages/react-server-cli/test/commands.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react-server-cli/test/commands.js b/packages/react-server-cli/test/commands.js index f27f3673a..460ed1769 100644 --- a/packages/react-server-cli/test/commands.js +++ b/packages/react-server-cli/test/commands.js @@ -27,14 +27,14 @@ fs.readdirSync(fixturesPath).forEach(testName => { const { args, stdoutIncludes, - stderrIncludes + stderrIncludes, } = JSON.parse(fs.readFileSync(path.join(testPath, 'options.json'))); const server = child_process.spawn( process.execPath, [ path.join(__dirname, '..', 'bin', 'react-server-cli'), - ...args + ...args, ] ); From aeef9cbb4c1bc50277e4121efa7bf2202e1fa7bc Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Sat, 28 Jan 2017 10:43:46 +0100 Subject: [PATCH 10/10] Fix react and react-dom versions --- packages/react-server-cli/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react-server-cli/package.json b/packages/react-server-cli/package.json index 711a67a4a..c32178ff8 100644 --- a/packages/react-server-cli/package.json +++ b/packages/react-server-cli/package.json @@ -63,8 +63,8 @@ "gulp-eslint": "^3.0.1", "nsp": "^2.6.2", "output-file-sync": "^1.1.2", - "react": "^15.4.2", - "react-dom": "^15.4.2", + "react": "~0.14.2 || ^15.1.0", + "react-dom": "~0.14.2 || ^15.1.0", "react-hot-loader": "^1.3.1", "react-server": "^0.5.1", "react-server-gulp-module-tagger": "^0.4.10",