Skip to content
This repository was archived by the owner on Feb 27, 2019. It is now read-only.

Commit 6c15d6d

Browse files
author
Mehdy Dara
authored
Merge pull request #53 from FountainJS/jsdom
Angular 2.0 & tests with jsdom
2 parents 18f825d + cbd3398 commit 6c15d6d

15 files changed

+208
-31
lines changed

.travis.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ cache:
3939
addons:
4040
# Install Firefox
4141
firefox: '46.0'
42-
# Configure Sauce Labs fot e2e tests
43-
sauce_connect:
44-
username: "zckrs"
45-
access_key:
46-
secure: "GRCFdTA5lYr51KaW4UAMV/oDxnm0CoPeYs1hZ2fynvv24X9KJhpukCNZNfYBVRg31gMUxGSzPOllFaHNBoExwb7/F7nndpG2FtBMlp8K22iGYDr8flRFJhd+5KJJRO4ed7z7hfHrbIwq3E2anoByh0NIFn5YO4FDwnf2ObFlFLAtyGppcTvr/UWpqzovFZLlAwU89YRZIFh7nzFUT2IU2BEWlR0TvVIhhNflpw7oScQDNtptZbGVKrJAMWT5VO15gTd97LKjyluHedZ6xWsN8TlOG2PSykI+7YYbz5Sat9l6PfBV2G4Nk5MOXivvW5mHZmXKcdGTfAoDGu9VSYFC+JdeW6DfOvS/ALagDaqmqKUyUY9eWRmt0UEMNoKhiCCe8agW2J3SiVaGVcobtCoB01nEU9iBmaVKAoZu9HbirSyEyc4j3DuQl8gAHEm/JOoTRFVIB/R7t9DPOjuFN5tHnGwO1tjkQtjCVuIz+feC3+EL819zuYCjzJ39MbQJZIiDtrsuBTkWj/SU5ECJgLyTM+qsI87XGVhtsD4UaWTCehvjs915yyLmGwm/D7dRHtno624e0zJo2Y4HHFOcKj1YXf2DaJI8NsApcyfOSWHg/O3yTURcNC/hKQ1avPDtaLpPYG3b4bpU4ubfyZCjzgjELEC7SpiL3AV17ieBCr2h3tM="
4742

4843
before_install:
4944
- export DISPLAY=:99.0

generator-fountain-angular2

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"eslint-plugin-react": "^4.3.0",
3434
"fixture-stdout": "^0.2.1",
3535
"globby": "^4.0.0",
36+
"jsdom": "swiip/jsdom#zonejs-compat-usable",
3637
"lodash": "^4.6.1",
3738
"mkdirp-promise": "^2.0.0",
3839
"mocha": "^2.4.5",
@@ -48,7 +49,7 @@
4849
"yeoman-test": "^1.1.0"
4950
},
5051
"scripts": {
51-
"test": "mocha test/travis-techs",
52+
"test": "mocha test/test-jsdom",
5253
"localTest": "mocha test/local-test",
5354
"linkAll": "node scripts/links",
5455
"unlinkAll": "node scripts/unlinks"

test/helpers/gulp-helper.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@ let serveProcess = null;
99

1010
function execServe(task) {
1111
return new Promise(resolve => {
12-
let logs = '';
13-
if (serveProcess !== null) {
14-
console.warn('Server process still running !!!!');
15-
}
16-
serveProcess = exec('gulp', [task], {stdio: 'pipe'}).process;
17-
serveProcess.stderr.pipe(process.stderr);
18-
serveProcess.stdout.pipe(spy(chunk => {
19-
logs += chunk.toString();
20-
const result = regex.exec(logs);
21-
if (result !== null) {
22-
resolve(result[1]);
12+
try {
13+
let logs = '';
14+
if (serveProcess !== null) {
15+
console.warn('Server process still running !!!!');
2316
}
24-
})).pipe(process.stdout);
17+
serveProcess = exec('gulp', [task], {stdio: 'pipe'}).process;
18+
serveProcess.stderr.pipe(process.stderr);
19+
serveProcess.stdout.pipe(spy(chunk => {
20+
logs += chunk.toString();
21+
const result = regex.exec(logs);
22+
if (result !== null) {
23+
resolve(result[1]);
24+
}
25+
})).pipe(process.stdout);
26+
} catch (error) {
27+
console.error('Server error', error);
28+
}
2529
});
2630
}
2731

@@ -34,9 +38,13 @@ exports.serveDist = function serveDist() {
3438
};
3539

3640
exports.killServe = function killServe() {
37-
serveProcess.kill('SIGTERM');
38-
serveProcess = null;
39-
console.log('Gulp serve killed!');
41+
try {
42+
serveProcess.kill('SIGTERM');
43+
serveProcess = null;
44+
console.log('Gulp serve killed!');
45+
} catch (error) {
46+
console.error('Server kill error', error);
47+
}
4048
};
4149

4250
exports.test = function () {
@@ -48,8 +56,8 @@ exports.test = function () {
4856
logs += chunk.toString();
4957
const result = testRegex.exec(logs);
5058
if (result !== null) {
51-
resolve(logs);
5259
testProcess.kill('SIGTERM');
60+
resolve(logs);
5361
}
5462
})).pipe(process.stdout);
5563
});

test/helpers/jsdom-helper.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
'use strict';
2+
3+
const jsdom = require('jsdom');
4+
const expect = require('chai').expect;
5+
const spawn = require('cross-spawn');
6+
7+
exports.run = function run(url) {
8+
return new Promise((resolve, reject) => {
9+
const result = spawn.sync('node', [`${__dirname}/jsdom-runner`, url], {stdio: 'inherit'});
10+
if (result.status === 0) {
11+
console.log('jsdom run success');
12+
resolve();
13+
} else {
14+
console.log('jsdom run failed', result);
15+
reject();
16+
}
17+
});
18+
};
19+
20+
exports.open = function open(url) {
21+
const virtualConsole = jsdom.createVirtualConsole().sendTo(console);
22+
23+
virtualConsole.on('jsdomError', error => {
24+
console.error('jsdom Error', error.stack, error.detail);
25+
});
26+
27+
return new Promise((resolve, reject) => {
28+
jsdom.env({
29+
url,
30+
virtualConsole,
31+
features: {
32+
FetchExternalResources: ['script'],
33+
ProcessExternalResources: ['script']
34+
},
35+
created(error, window) {
36+
console.log('jsdom started on URL', url);
37+
if (error) {
38+
console.log('jsdom en creation error', error.stack, error.detail);
39+
reject();
40+
}
41+
window.addEventListener('error', event => {
42+
console.error("Script error", event.error.stack, event.error.detail);
43+
});
44+
resolve(window);
45+
}
46+
});
47+
});
48+
};
49+
50+
function wait(time) {
51+
return new Promise(resolve => {
52+
setTimeout(resolve, time);
53+
});
54+
}
55+
56+
function waitFor(test, retryTime, attemptsMax) {
57+
console.log('[WaitFor] First try');
58+
const firstResult = test();
59+
if (firstResult.length > 0) {
60+
return Promise.resolve(firstResult);
61+
}
62+
let attempts = 1;
63+
function oneTry() {
64+
return wait(retryTime).then(() => {
65+
console.log('[WaitFor] Try', attempts);
66+
const result = test();
67+
if (result.length > 0) {
68+
return result;
69+
}
70+
attempts++;
71+
if (attempts >= attemptsMax) {
72+
throw new Error(`[WaitFor] Max attemps reached (${attemptsMax})`);
73+
}
74+
return oneTry();
75+
});
76+
}
77+
return oneTry();
78+
}
79+
80+
exports.testTechs = function testTechs(window) {
81+
return waitFor(() => {
82+
return window.document.querySelectorAll('h3');
83+
}, 1000, 20)
84+
.then(
85+
elements => {
86+
expect(elements.length).to.equal(8);
87+
},
88+
error => {
89+
console.log('Test techs error', error.stack, error.detail);
90+
throw error;
91+
}
92+
);
93+
};

test/helpers/jsdom-runner.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const jsdom = require('./jsdom-helper');
2+
3+
jsdom.open(process.argv[2])
4+
.then(window => jsdom.testTechs(window))
5+
.then(
6+
() => process.exit(0),
7+
error => {
8+
console.log('jsdom runner failed', error.stack, error.detail);
9+
process.exit(1);
10+
}
11+
);

test/test-jsdom.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
require('co-mocha');
4+
const product = require('cartesian-product');
5+
6+
const gulp = require('./helpers/gulp-helper');
7+
const yeoman = require('./helpers/yeoman-helper');
8+
const linter = require('./helpers/linter-helper');
9+
const unit = require('./helpers/unit-helper');
10+
const jsdom = require('./helpers/jsdom-helper');
11+
12+
describe('fountain travis integration test with jsdom', function () {
13+
this.timeout(0);
14+
15+
const combinations = product([
16+
['react', 'angular1', 'angular2', 'vue'],
17+
['webpack', 'systemjs', 'inject'],
18+
['babel', 'js', 'typescript']
19+
])
20+
// Angular 2 and Bower are not supported right now
21+
.filter(combination => combination[0] !== 'angular2' || combination[1] !== 'inject')
22+
// Vue only with Webpack and Babel
23+
.filter(combination => combination[0] !== 'vue' || (combination[1] === 'webpack' && combination[2] === 'babel'));
24+
25+
combinations.forEach(combination => {
26+
const options = {
27+
framework: combination[0],
28+
modules: combination[1],
29+
css: 'scss',
30+
js: combination[2],
31+
sample: 'techs',
32+
router: combination[0] === 'angular1' ? 'uirouter' : 'router',
33+
ci: 'travis'
34+
};
35+
36+
describe(`tests with ${options.framework}, ${options.modules}, ${options.js}`, () => {
37+
before(function * () {
38+
console.log(`travis_fold:start:${options.framework}-${options.modules}-${options.js}`);
39+
yield yeoman.prepare();
40+
yield yeoman.run(options);
41+
});
42+
43+
it('test linter', function * () {
44+
yield linter.linterTest(options);
45+
});
46+
47+
it('run "gulp test"', function * () {
48+
const result = yield gulp.test();
49+
unit.unitTests(result);
50+
});
51+
52+
it('run "gulp serve" and e2e on number of Techs listed', function * () {
53+
const url = yield gulp.serve();
54+
yield jsdom.run(url);
55+
gulp.killServe();
56+
});
57+
58+
it('run "gulp serve:dist" and e2e on number of Techs listed', function * () {
59+
const url = yield gulp.serveDist();
60+
yield jsdom.run(url);
61+
gulp.killServe();
62+
});
63+
64+
after(() => {
65+
console.log(`travis_fold:end:${options.framework}-${options.modules}-${options.js}`);
66+
});
67+
});
68+
});
69+
});

0 commit comments

Comments
 (0)