forked from redfin/react-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
11 changed files
with
141 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
target | ||
test/**/tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
4 changes: 4 additions & 0 deletions
4
packages/react-server-cli/test/fixtures/commands/start-basic/in-files/routes.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"middleware": [], | ||
"routes": {} | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/react-server-cli/test/fixtures/commands/start-basic/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"args": ["start"], | ||
"stdoutIncludes": "Started HTML server over HTTP on" | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/react-server-cli/test/fixtures/commands/start-missing-routes/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"args": ["start"], | ||
"stderrIncludes": "Cannot find module" | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/react-server-cli/test/fixtures/commands/start-reactserverrc/in-files/.reactserverrc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"routesFile": "customRoutes.js", | ||
"port": 4000, | ||
"jsPort": 4001 | ||
} |
3 changes: 3 additions & 0 deletions
3
...ages/react-server-cli/test/fixtures/commands/start-reactserverrc/in-files/customRoutes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
routes: {} | ||
}; |
4 changes: 4 additions & 0 deletions
4
packages/react-server-cli/test/fixtures/commands/start-reactserverrc/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"args": ["start"], | ||
"stdoutIncludes": "Started HTML server over HTTP on" | ||
} |
1 change: 1 addition & 0 deletions
1
packages/react-server-cli/test/fixtures/commands/start-routes-with-error/in-files/routes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
undeclaredVariable(); |
4 changes: 4 additions & 0 deletions
4
packages/react-server-cli/test/fixtures/commands/start-routes-with-error/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |