Skip to content

Commit 7ac39b0

Browse files
author
Kuniwak
committed
We can take exams by CI
1 parent 5d88ef0 commit 7ac39b0

File tree

9 files changed

+96
-46
lines changed

9 files changed

+96
-46
lines changed

gulp/serve.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* eslint no-underscore-dangle:0 */
2+
'use strict';
3+
4+
var stream = require('stream');
5+
var gutil = require('gulp-util');
6+
7+
var SERVER_SCRIPT = './server.js';
8+
var PORT = 8000;
9+
10+
11+
var serve = function() {
12+
var nodemon = require('gulp-nodemon');
13+
var readable = new stream.Readable({ objectMode: true });
14+
15+
readable._read = function() {
16+
var self = this;
17+
18+
nodemon({
19+
script: SERVER_SCRIPT,
20+
watch: [SERVER_SCRIPT],
21+
env: { PORT: PORT },
22+
stdout: false
23+
})
24+
.on('readable', function() {
25+
this.stdout.on('data', function(buf) {
26+
gutil.log(String(buf));
27+
if (!String(buf).match(/SERVER_READY/)) { return; }
28+
29+
// server is ready.
30+
self.emit('end');
31+
});
32+
33+
this.stderr.on('data', function(buf) {
34+
gutil.log(gutil.colors.red(buf));
35+
});
36+
});
37+
};
38+
39+
return readable;
40+
};
41+
42+
module.exports = serve;

gulpfile.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,67 @@
11
'use strict';
22

3+
var util = require('util');
34
var gulp = require('gulp-help')(require('gulp'));
4-
var mocha = require('gulp-mocha');
5-
var eslint = require('gulp-eslint');
6-
var nodemon = require('gulp-nodemon');
5+
6+
var PORT = 8000;
7+
var BASE_URL = util.format('http://localhost:%d/', PORT);
78

89
var tasks = [
910
{
10-
cmd: 'test:all',
11-
help: 'すべてのテストを実行します',
12-
src: 'test/**/*.js'
13-
}, {
14-
cmd: 'test:stage1',
11+
cmd: 'stage1',
1512
help: '意図した DOM を取得できているかテストします',
13+
url: BASE_URL + 'stage1',
1614
src: 'test/stage1.js'
1715
}, {
18-
cmd: 'test:stage2',
16+
cmd: 'stage2',
1917
help: '意図通りに DOM の構造・スタイルが変更できているかテストします',
18+
url: BASE_URL + 'stage2',
2019
src: 'test/stage2.js'
2120
}, {
22-
cmd: 'test:stage3',
21+
cmd: 'stage3',
2322
help: '意図通りにイベントを利用できているかテストします',
23+
url: BASE_URL + 'stage3',
2424
src: 'test/stage3.js'
2525
}, {
26-
cmd: 'test:stage4',
26+
cmd: 'stage4',
2727
help: '意図通りにサーバーと通信できているかテストします',
28+
url: BASE_URL + 'stage4',
2829
src: 'test/stage4.js'
2930
}, {
30-
cmd: 'test:stage5',
31+
cmd: 'stage5',
3132
help: '意図通りにモジュールを実装できているかテストします',
33+
url: BASE_URL + 'stage5',
3234
src: 'test/stage5.js'
3335
}, {
34-
cmd: 'test:stage6',
36+
cmd: 'stage6',
3537
help: 'よくあるイディオムを読み書きできているかテストします',
38+
url: BASE_URL + 'stage6',
3639
src: 'test/stage6.js'
3740
}
3841
];
3942

4043

4144
tasks.forEach(function(task) {
42-
gulp.task(task.cmd, task.help, function() {
43-
return gulp.src(task.src, { read: false }).
44-
pipe(eslint()).
45-
pipe(eslint.format()).
46-
pipe(mocha());
45+
var run = require('gulp-run');
46+
47+
gulp.task(task.cmd, task.help, ['lint'], function() {
48+
// We expected that mocha-phantomjs print colorized results, but it isn't.
49+
// So, I take a fast way that is using gulp-run.
50+
return run('`npm bin`/mocha-phantomjs ' + task.url + ' || true').exec();
4751
});
4852
});
4953

5054

55+
gulp.task('lint', 'ミスのおこりやすいコード・可読性の低いコードがないか検査します', function() {
56+
var eslint = require('gulp-eslint');
57+
58+
return gulp.src('test/**/*.js')
59+
.pipe(eslint())
60+
.pipe(eslint.format());
61+
});
62+
63+
5164
gulp.task('serve', 'サーバーを起動し、ブラウザでテストを確認できるようにします', function(){
52-
nodemon({
53-
script: './server.js',
54-
watch: ['server.js'],
55-
env: { PORT: 8000 }
56-
});
65+
var serve = require('./gulp/serve.js');
66+
return serve();
5767
});

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Training course repository for JavaScript by mixi",
55
"main": "index.js",
66
"scripts": {
7-
"test": "$(npm bin)/gulp test:all"
7+
"help": "$(npm bin)/gulp help"
88
},
99
"repository": {
1010
"type": "git",
@@ -26,11 +26,14 @@
2626
"gulp": "^3.8.11",
2727
"gulp-eslint": "^0.6.0",
2828
"gulp-help": "^1.3.3",
29-
"gulp-mocha": "^2.0.0",
3029
"gulp-nodemon": "^1.0.5",
30+
"gulp-run": "^1.6.6",
3131
"gulp-util": "^3.0.4",
3232
"mocha": "^2.2.0",
33-
"mocha-phantomjs": "^3.5.3",
34-
"phantomjs": "^1.9.16"
33+
"mocha-phantomjs": "^3.5.3"
34+
},
35+
"dependencies": {
36+
"chai-jquery": "^2.0.0",
37+
"jquery": "^2.1.3"
3538
}
3639
}

server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ var server = require('http').createServer(app);
1818
var PORT = 8000;
1919
var HOSTNAME = 'localhost';
2020
server.listen(PORT, HOSTNAME, function () {
21-
console.log(util.format('ブラウザで http://%s:%d/stage1 にアクセスしてください', HOSTNAME, PORT));
21+
console.log(util.format('SERVER_READY on http://%s:%d', HOSTNAME, PORT));
2222
});

test/.eslintrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"env": {
3-
"browser": true,
43
"mocha": true
54
},
65
"globals": {

test/stage1/.eslintrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"mocha": true
5+
},
6+
"globals": {
7+
"$": false,
8+
"jQuery": false,
9+
"HTMLCollection": false
10+
}
11+
}

test/stage1/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
<script>mocha.setup('bdd')</script>
4343
<script src="tests.js"></script>
4444
<script>
45-
mocha.run();
45+
if (window.mochaPhantomJS) { mochaPhantomJS.run(); }
46+
else { mocha.run(); }
4647
</script>
4748
</body>
4849
</html>

test/stage1/secret.js

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

test/stage1/tests.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* global $, jQuery, HTMLCollection */
21
'use strict';
32

43
describe('ステージ1(意図した DOM 要素を取得できるようになる)', function(){

0 commit comments

Comments
 (0)