Skip to content

Commit 9c9edd3

Browse files
Enoah NetzachEnoahNetzach
authored andcommitted
Add "development" tests
1 parent e142b7c commit 9c9edd3

File tree

6 files changed

+190
-144
lines changed

6 files changed

+190
-144
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# IMPORTANT
2+
3+
Until further investigations, there is a max limit of `initDOM` in one single file.
4+
Empirically they should not exceed 10 tests per file.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const fs = require('fs')
2+
const http = require('http')
3+
const jsdom = require('jsdom')
4+
const path = require('path')
5+
6+
let getMarkup
7+
let resourceLoader
8+
// this value could be tweaked in order to let the resource
9+
// retriever get every file and jsdom execute react
10+
let timeToWaitForJsToExecute
11+
12+
if (process.env.E2E_FILE) {
13+
const file = path.isAbsolute(process.env.E2E_FILE)
14+
? process.env.E2E_FILE
15+
: path.join(process.cwd(), process.env.E2E_FILE)
16+
17+
const markup = fs.readFileSync(file, 'utf8')
18+
getMarkup = () => markup
19+
20+
resourceLoader = (resource, callback) => callback(
21+
null,
22+
fs.readFileSync(path.join(path.dirname(file), resource.url.pathname), 'utf8')
23+
)
24+
25+
timeToWaitForJsToExecute = 0
26+
} else if (process.env.E2E_URL) {
27+
getMarkup = () => new Promise(resolve => {
28+
http.get(process.env.E2E_URL, (res) => {
29+
let rawData = ''
30+
res.on('data', chunk => rawData += chunk)
31+
res.on('end', () => resolve(rawData))
32+
})
33+
})
34+
35+
resourceLoader = (resource, callback) => {
36+
return resource.defaultFetch(callback)
37+
}
38+
39+
timeToWaitForJsToExecute = 100
40+
} else {
41+
it.only('can run jsdom (at least one of "E2E_FILE" or "E2E_URL" environment variables must be provided)', () => {
42+
expect(new Error('This isn\'t the error you are looking for.')).toBeUndefined()
43+
})
44+
}
45+
46+
export default feature => new Promise(async resolve => {
47+
const markup = await getMarkup()
48+
const host = process.env.E2E_URL || 'http://localhost:3000'
49+
const doc = jsdom.jsdom(markup, {
50+
features : {
51+
FetchExternalResources : ['script'],
52+
ProcessExternalResources : ['script'],
53+
},
54+
resourceLoader,
55+
url: `${host}#${feature}`,
56+
virtualConsole: jsdom.createVirtualConsole().sendTo(console),
57+
})
58+
59+
doc.defaultView.addEventListener('load', () => {
60+
setTimeout(() => resolve(doc), timeToWaitForJsToExecute)
61+
}, false)
62+
})
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import initDOM from './initDOM'
2+
3+
// eslint-disable-next-line no-undef
4+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000
5+
6+
describe('Integration', () => {
7+
describe('Language syntax', () => {
8+
it('array-destructuring', async () => {
9+
const doc = await initDOM('array-destructuring')
10+
11+
expect(doc.getElementById('feature-array-destructuring').childElementCount).toBe(4)
12+
})
13+
14+
it('array-spread', async () => {
15+
const doc = await initDOM('array-spread')
16+
17+
expect(doc.getElementById('feature-array-spread').childElementCount).toBe(4)
18+
})
19+
20+
it('async/await', async () => {
21+
const doc = await initDOM('async-await')
22+
23+
expect(doc.getElementById('feature-async-await').childElementCount).toBe(4)
24+
})
25+
26+
it('class properties', async () => {
27+
const doc = await initDOM('class-properties')
28+
29+
expect(doc.getElementById('feature-class-properties').childElementCount).toBe(4)
30+
})
31+
32+
it('computed properties', async () => {
33+
const doc = await initDOM('computed-properties')
34+
35+
expect(doc.getElementById('feature-computed-properties').childElementCount).toBe(4)
36+
})
37+
38+
it('custom interpolation', async () => {
39+
const doc = await initDOM('custom-interpolation')
40+
41+
expect(doc.getElementById('feature-custom-interpolation').childElementCount).toBe(4)
42+
})
43+
44+
it('default parameters', async () => {
45+
const doc = await initDOM('default-parameters')
46+
47+
expect(doc.getElementById('feature-default-parameters').childElementCount).toBe(4)
48+
})
49+
50+
it('destructuring and await', async () => {
51+
const doc = await initDOM('destructuring-and-await')
52+
53+
expect(doc.getElementById('feature-destructuring-and-await').childElementCount).toBe(4)
54+
})
55+
56+
it('generators', async () => {
57+
const doc = await initDOM('generators')
58+
59+
expect(doc.getElementById('feature-generators').childElementCount).toBe(4)
60+
})
61+
62+
it('object-destructuring', async () => {
63+
const doc = await initDOM('object-destructuring')
64+
65+
expect(doc.getElementById('feature-object-destructuring').childElementCount).toBe(4)
66+
})
67+
})
68+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import initDOM from './initDOM'
2+
3+
// eslint-disable-next-line no-undef
4+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000
5+
6+
describe('Integration', () => {
7+
describe('Language syntax', () => {
8+
it('object-spread', async () => {
9+
const doc = await initDOM('object-spread')
10+
11+
expect(doc.getElementById('feature-object-spread').childElementCount).toBe(4)
12+
})
13+
14+
it('promises', async () => {
15+
const doc = await initDOM('promises')
16+
17+
expect(doc.getElementById('feature-promises').childElementCount).toBe(4)
18+
})
19+
20+
it('rest + default', async () => {
21+
const doc = await initDOM('rest-and-default')
22+
23+
expect(doc.getElementById('feature-rest-and-default').childElementCount).toBe(4)
24+
})
25+
26+
it('rest parameters', async () => {
27+
const doc = await initDOM('rest-parameters')
28+
29+
expect(doc.getElementById('feature-rest-parameters').childElementCount).toBe(4)
30+
})
31+
32+
it('template interpolation', async () => {
33+
const doc = await initDOM('template-interpolation')
34+
35+
expect(doc.getElementById('feature-template-interpolation').childElementCount).toBe(4)
36+
})
37+
})
38+
})

packages/react-scripts/templates/kitchensink/tests/features.test.js

Lines changed: 0 additions & 137 deletions
This file was deleted.

tasks/e2e.sh

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,15 @@ test -e build/static/css/main.*.css
157157
# test -e build/static/media/*.svg # TODO uncomment this line
158158
# test -e build/favicon.ico # TODO uncomment this line
159159

160-
# Run tests with CI flag
161-
E2E_FILE=./build/index.html CI=true NODE_PATH=src npm test
160+
# Unit tests
161+
CI=true NODE_PATH=src npm test -- --no-cache --testPathPattern="/src"
162+
# Test "development" environment
163+
tmp_server_log=`mktemp`
164+
PORT=3001 nohup npm start &>$tmp_server_log &
165+
grep -q 'The app is running at:' <(tail -f $tmp_server_log)
166+
E2E_URL="http://localhost:3001" CI=true NODE_PATH=src npm test -- --no-cache --testPathPattern="/integration/"
167+
# Test "production" environment
168+
E2E_FILE=./build/index.html CI=true NODE_PATH=src npm test -- --no-cache --testPathPattern="/integration/"
162169
# Uncomment when snapshot testing is enabled by default:
163170
# test -e src/__snapshots__/App.test.js.snap
164171

@@ -187,11 +194,15 @@ test -e build/static/css/main.*.css
187194
# test -e build/static/media/*.svg # TODO uncomment this line
188195
# test -e build/favicon.ico # TODO uncomment this line
189196

190-
# Run tests, overring the watch option to disable it.
191-
# `CI=true npm test` won't work here because `npm test` becomes just `jest`.
192-
# We should either teach Jest to respect CI env variable, or make
193-
# `scripts/test.js` survive ejection (right now it doesn't).
194-
E2E_FILE=./build/index.html NODE_PATH=src npm test -- --watch=no
197+
# Unit tests
198+
CI=true NODE_PATH=src npm test -- --no-cache --testPathPattern="/src"
199+
# Test "development" environment
200+
tmp_server_log=`mktemp`
201+
PORT=3002 nohup npm start &>$tmp_server_log &
202+
grep -q 'The app is running at:' <(tail -f $tmp_server_log)
203+
E2E_URL="http://localhost:3002" CI=true NODE_PATH=src npm test -- --no-cache --testPathPattern="/integration/"
204+
# Test "production" environment
205+
E2E_FILE=./build/index.html CI=true NODE_PATH=src npm test -- --no-cache --testPathPattern="/integration/"
195206
# Uncomment when snapshot testing is enabled by default:
196207
# test -e src/__snapshots__/App.test.js.snap
197208

0 commit comments

Comments
 (0)