Skip to content
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

Use a more sophisticated template for end-to-end testing. #1187

Merged
merged 22 commits into from
Jan 23, 2017
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/create-react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ var program = require('commander')
})
.option('--verbose', 'print additional logs')
.option('--scripts-version <alternative-package>', 'use a non-standard version of react-scripts')
.option('--template <path-to-variant>', 'to select a template variant')
Copy link
Contributor

@gaearon gaearon Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change to '--template <path-to-template>', 'use a non-standard application template'.

.on('--help', function () {
console.log(' Only ' + chalk.green('<project-directory>') + ' is required.');
console.log();
Expand All @@ -82,7 +83,7 @@ var program = require('commander')
console.log(' ' + chalk.cyan('https://github.com/facebookincubator/create-react-app/issues/new'));
console.log();
})
.parse(process.argv)
.parse(process.argv);

if (typeof projectName === 'undefined') {
console.error('Please specify the project directory:');
Expand All @@ -95,9 +96,9 @@ if (typeof projectName === 'undefined') {
process.exit(1);
}

createApp(projectName, program.verbose, program.scriptsVersion);
createApp(projectName, program.verbose, program.scriptsVersion, program.template);

function createApp(name, verbose, version) {
function createApp(name, verbose, version, template) {
var root = path.resolve(name);
var appName = path.basename(root);

Expand Down Expand Up @@ -130,7 +131,7 @@ function createApp(name, verbose, version) {
console.log('Installing ' + chalk.cyan('react-scripts') + '...');
console.log();

run(root, appName, version, verbose, originalDirectory);
run(root, appName, version, verbose, originalDirectory, template);
}

function shouldUseYarn() {
Expand Down Expand Up @@ -163,7 +164,7 @@ function install(packageToInstall, verbose, callback) {
});
}

function run(root, appName, version, verbose, originalDirectory) {
function run(root, appName, version, verbose, originalDirectory, template) {
var packageToInstall = getInstallPackage(version);
var packageName = getPackageName(packageToInstall);

Expand All @@ -183,7 +184,7 @@ function run(root, appName, version, verbose, originalDirectory) {
'init.js'
);
var init = require(scriptsPath);
init(root, appName, verbose, originalDirectory);
init(root, appName, verbose, originalDirectory, template);
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/react-scripts/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/fixtures
3 changes: 3 additions & 0 deletions packages/react-scripts/fixtures/kitchensink/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["latest"]
}
1 change: 1 addition & 0 deletions packages/react-scripts/fixtures/kitchensink/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_FILE_ENV_MESSAGE=fromtheenvfile
8 changes: 8 additions & 0 deletions packages/react-scripts/fixtures/kitchensink/.flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[ignore]
<PROJECT_ROOT>/node_modules/fbjs/.*

[include]

[libs]

[options]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"dependencies": {
"babel-preset-latest": "6.16.0",
"babel-register": "6.18.0",
"babel-polyfill": "6.20.0",
"chai": "3.5.0",
"jsdom": "9.8.3",
"mocha": "3.2.0"
}
}
15 changes: 15 additions & 0 deletions packages/react-scripts/fixtures/kitchensink/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
node_modules

# testing
coverage

# production
build

# misc
.DS_Store
.env
npm-debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from 'chai'
import initDOM from './initDOM'

describe('Integration', () => {
describe('Environment variables', () => {
it('NODE_PATH', async () => {
const doc = await initDOM('node-path')

expect(doc.getElementById('feature-node-path').childElementCount).to.equal(4)
})

it('shell env variables', async () => {
const doc = await initDOM('shell-env-variables')

expect(doc.getElementById('feature-shell-env-variables').textContent).to.equal('fromtheshell.')
})

it('file env variables', async () => {
const doc = await initDOM('file-env-variables')

expect(doc.getElementById('feature-file-env-variables').textContent).to.equal('fromtheenvfile.')
})
})
})
62 changes: 62 additions & 0 deletions packages/react-scripts/fixtures/kitchensink/integration/initDOM.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const fs = require('fs')
const http = require('http')
const jsdom = require('jsdom')
const path = require('path')

let getMarkup
let resourceLoader
// this value could be tweaked in order to let the resource
// retriever get every file and jsdom execute react
let timeToWaitForJsToExecute

if (process.env.E2E_FILE) {
const file = path.isAbsolute(process.env.E2E_FILE)
? process.env.E2E_FILE
: path.join(process.cwd(), process.env.E2E_FILE)

const markup = fs.readFileSync(file, 'utf8')
getMarkup = () => markup

resourceLoader = (resource, callback) => callback(
null,
fs.readFileSync(path.join(path.dirname(file), resource.url.pathname), 'utf8')
)

timeToWaitForJsToExecute = 0
} else if (process.env.E2E_URL) {
getMarkup = () => new Promise(resolve => {
http.get(process.env.E2E_URL, (res) => {
let rawData = ''
res.on('data', chunk => rawData += chunk)
res.on('end', () => resolve(rawData))
})
})

resourceLoader = (resource, callback) => {
return resource.defaultFetch(callback)
}

timeToWaitForJsToExecute = 100
} else {
it.only('can run jsdom (at least one of "E2E_FILE" or "E2E_URL" environment variables must be provided)', () => {
expect(new Error('This isn\'t the error you are looking for.')).toBeUndefined()
})
}

export default feature => new Promise(async resolve => {
const markup = await getMarkup()
const host = process.env.E2E_URL || 'http://localhost:3000'
const doc = jsdom.jsdom(markup, {
features : {
FetchExternalResources : ['script', 'css'],
ProcessExternalResources : ['script'],
},
resourceLoader,
url: `${host}#${feature}`,
virtualConsole: jsdom.createVirtualConsole().sendTo(console),
})

doc.defaultView.addEventListener('load', () => {
setTimeout(() => resolve(doc), timeToWaitForJsToExecute)
}, false)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { expect } from 'chai'
import initDOM from './initDOM'

describe('Integration', () => {
describe('Language syntax', () => {
it('array destructuring', async () => {
const doc = await initDOM('array-destructuring')

expect(doc.getElementById('feature-array-destructuring').childElementCount).to.equal(4)
})

it('array spread', async () => {
const doc = await initDOM('array-spread')

expect(doc.getElementById('feature-array-spread').childElementCount).to.equal(4)
})

it('async/await', async () => {
const doc = await initDOM('async-await')

expect(doc.getElementById('feature-async-await').childElementCount).to.equal(4)
})

it('class properties', async () => {
const doc = await initDOM('class-properties')

expect(doc.getElementById('feature-class-properties').childElementCount).to.equal(4)
})

it('computed properties', async () => {
const doc = await initDOM('computed-properties')

expect(doc.getElementById('feature-computed-properties').childElementCount).to.equal(4)
})

it('custom interpolation', async () => {
const doc = await initDOM('custom-interpolation')

expect(doc.getElementById('feature-custom-interpolation').childElementCount).to.equal(4)
})

it('default parameters', async () => {
const doc = await initDOM('default-parameters')

expect(doc.getElementById('feature-default-parameters').childElementCount).to.equal(4)
})

it('destructuring and await', async () => {
const doc = await initDOM('destructuring-and-await')

expect(doc.getElementById('feature-destructuring-and-await').childElementCount).to.equal(4)
})

it('generators', async () => {
const doc = await initDOM('generators')

expect(doc.getElementById('feature-generators').childElementCount).to.equal(4)
})

it('object destructuring', async () => {
const doc = await initDOM('object-destructuring')

expect(doc.getElementById('feature-object-destructuring').childElementCount).to.equal(4)
})

it('object spread', async () => {
const doc = await initDOM('object-spread')

expect(doc.getElementById('feature-object-spread').childElementCount).to.equal(4)
})

it('promises', async () => {
const doc = await initDOM('promises')

expect(doc.getElementById('feature-promises').childElementCount).to.equal(4)
})

it('rest + default', async () => {
const doc = await initDOM('rest-and-default')

expect(doc.getElementById('feature-rest-and-default').childElementCount).to.equal(4)
})

it('rest parameters', async () => {
const doc = await initDOM('rest-parameters')

expect(doc.getElementById('feature-rest-parameters').childElementCount).to.equal(4)
})

it('template interpolation', async () => {
const doc = await initDOM('template-interpolation')

expect(doc.getElementById('feature-template-interpolation').childElementCount).to.equal(4)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { expect } from 'chai'
import initDOM from './initDOM'

describe('Integration', () => {
describe('Webpack plugins', () => {
it('css inclusion', async () => {
const doc = await initDOM('css-inclusion')

expect(doc.getElementsByTagName('style')[0].textContent.replace(/\s/g, ''))
.to.match(/#feature-css-inclusion\{background:.+;color:.+}/)
})

it('image inclusion', async () => {
const doc = await initDOM('image-inclusion')

expect(doc.getElementById('feature-image-inclusion').src).to.match(/^data:image\/jpeg;base64.+==$/)
})

it('no ext inclusion', async () => {
const doc = await initDOM('no-ext-inclusion')

expect(doc.getElementById('feature-no-ext-inclusion').textContent)
.to.equal('This is just a file without an extension.')
})

it('json inclusion', async () => {
const doc = await initDOM('json-inclusion')

expect(doc.getElementById('feature-json-inclusion').textContent).to.equal('This is an abstract.')
})

it('svg inclusion', async () => {
const doc = await initDOM('svg-inclusion')

expect(doc.getElementById('feature-svg-inclusion').src).to.match(/\/static\/media\/logo\..+\.svg$/)
})

it('unknown ext inclusion', async () => {
const doc = await initDOM('unknown-ext-inclusion')

expect(doc.getElementById('feature-unknown-ext-inclusion').textContent).to.equal('Whoooo, spooky!.')
})
})
})
Binary file not shown.
12 changes: 12 additions & 0 deletions packages/react-scripts/fixtures/kitchensink/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<title>React App</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
Loading