-
Notifications
You must be signed in to change notification settings - Fork 184
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
Improve error messaging when requiring routes, add CLI tests #828
Changes from 9 commits
1d16729
544ca6b
d9141e0
dec327b
c44107c
f1bf63b
74513c1
8e73edc
4758224
aeef9cb
9d596cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
target | ||
test/**/tmp |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to match the rest of the project: "react-dom": "~0.14.2 || ^15.1.0" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch @drewpc. Wonder how we could automate this check... 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would peer dependencies help? |
||
"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" | ||
] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
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; | ||
|
||
// 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. | ||
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; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"middleware": [], | ||
"routes": {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"args": ["start"], | ||
"stdoutIncludes": "Started HTML server over HTTP on" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"args": ["start"], | ||
"stderrIncludes": "Failed to load routes file at" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"routesFile": "customRoutes.js", | ||
"port": 4000, | ||
"jsPort": 4001 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
routes: {} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"args": ["start"], | ||
"stdoutIncludes": "Started HTML server over HTTP on" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
undeclaredVariable(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"args": ["start", "--routes-file", "routes.js"], | ||
"stderrIncludes": "ReferenceError: undeclaredVariable is not defined" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to match the rest of the project: "react": "~0.14.2 || ^15.1.0"