grunt-contrib-watch/test/tasks/livereload_test.js

240 lines
9.4 KiB
JavaScript
Raw Normal View History

'use strict';
var grunt = require('grunt');
var path = require('path');
var fs = require('fs');
var http = require('http');
var helper = require('./helper');
var fixtures = helper.fixtures;
function cleanUp() {
helper.cleanUp([
2016-06-06 14:25:10 +08:00
'livereload/node_modules'
]);
}
// Helper for requesting the live reload server
function request(port, done) {
var data = '';
var req = http.request({
hostname: 'localhost',
2016-06-06 14:25:10 +08:00
port: port
}, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
done(data);
});
});
req.end();
}
exports.livereload = {
setUp: function(done) {
cleanUp();
fs.symlinkSync(path.join(__dirname, '../../node_modules'), path.join(fixtures, 'livereload', 'node_modules'));
done();
},
tearDown: function(done) {
cleanUp();
done();
},
basic: function(test) {
test.expect(4);
var resultData = '';
var cwd = path.resolve(fixtures, 'livereload');
var assertWatch = helper.assertTask(['watch:basic', '-v'], {cwd: cwd});
assertWatch([function() {
request(35729, function(data) {
resultData += data;
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
});
}], function(result) {
2013-05-02 11:25:09 +08:00
result = helper.unixify(result);
helper.verboseLog(result);
test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.');
test.ok(result.indexOf('Live reload server started on *:35729') !== -1,
2014-11-04 09:38:53 +08:00
'live reload server should have been started on port 35729.');
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js');
resultData = JSON.parse(resultData);
test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.');
test.done();
});
},
2014-11-07 11:04:17 +08:00
customhost: function(test) {
test.expect(4);
var resultData = '';
var cwd = path.resolve(fixtures, 'livereload');
2014-11-07 11:04:17 +08:00
var assertWatch = helper.assertTask(['watch:customhost', '-v'], {cwd: cwd});
assertWatch([function() {
request(8675, function(data) {
resultData += data;
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
});
}], function(result) {
2013-05-02 11:25:09 +08:00
result = helper.unixify(result);
helper.verboseLog(result);
test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.');
test.ok(result.indexOf('Live reload server started on localhost:8675') !== -1,
'live reload server should have been started on localhost:8675.');
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js');
resultData = JSON.parse(resultData);
test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.');
test.done();
});
},
differentfiles: function(test) {
test.expect(3);
var cwd = path.resolve(fixtures, 'livereload');
var assertWatch = helper.assertTask(['watch:differentfiles', '-v'], {cwd: cwd});
assertWatch([function() {
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
2013-08-28 07:39:29 +08:00
}, function() {
grunt.file.write(path.join(cwd, 'lib', 'two.js'), 'var two = true;');
}], function(result) {
result = helper.unixify(result);
helper.verboseLog(result);
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js');
test.ok(result.indexOf('Live reloading lib/two.js...') !== -1, 'live reload should have triggered on lib/two.js');
2014-11-04 09:38:53 +08:00
test.ok(!/Live reloading (lib\/one\.js, lib\/two.js|lib\/two.js, lib\/one.js)\.\.\./.test(result),
'live reload should have cleared js file that was already reloaded');
test.done();
});
},
multiplefiles: function(test) {
test.expect(4);
var resultData = '';
var cwd = path.resolve(fixtures, 'livereload');
var assertWatch = helper.assertTask(['watch:multiplefiles', '-v'], {cwd: cwd});
assertWatch([function() {
request(9876, function(data) {
resultData += data;
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
grunt.file.write(path.join(cwd, 'lib', 'two.js'), 'var two = true;');
});
}], function(result) {
result = helper.unixify(result);
helper.verboseLog(result);
test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.');
test.ok(result.indexOf('Live reload server started on *:9876') !== -1,
2014-11-04 09:38:53 +08:00
'live reload server should have been started on port 9876.');
test.ok(/Live reloading (lib\/one\.js, lib\/two.js|lib\/two.js, lib\/one.js)\.\.\./.test(result),
'live reload should have triggered on lib/one.js and lib/two.js');
resultData = JSON.parse(resultData);
test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.');
test.done();
});
},
nospawn: function(test) {
test.expect(4);
var resultData = '';
var cwd = path.resolve(fixtures, 'livereload');
var assertWatch = helper.assertTask(['watch:nospawn', '-v'], {cwd: cwd});
assertWatch([function() {
2013-05-01 04:44:21 +08:00
request(1337, function(data) {
resultData += data;
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
});
}], function(result) {
2013-05-02 11:25:09 +08:00
result = helper.unixify(result);
helper.verboseLog(result);
test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.');
test.ok(result.indexOf('Live reload server started on *:1337') !== -1,
2014-11-04 09:38:53 +08:00
'live reload server should have been started on port 1337.');
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js');
resultData = JSON.parse(resultData);
test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.');
test.done();
});
},
notasks: function(test) {
test.expect(3);
var resultData = '';
var cwd = path.resolve(fixtures, 'livereload');
var assertWatch = helper.assertTask(['watch:notasks', '-v'], {cwd: cwd});
assertWatch([function() {
request(35729, function(data) {
resultData += data;
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
});
}], function(result) {
result = helper.unixify(result);
helper.verboseLog(result);
test.ok(result.indexOf('Live reload server started on *:35729') !== -1,
2014-11-04 09:38:53 +08:00
'live reload server should have been started on port 35729.');
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js');
resultData = JSON.parse(resultData);
test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.');
test.done();
});
},
onlytriggeron: function(test) {
test.expect(2);
var cwd = path.resolve(fixtures, 'livereload');
var assertWatch = helper.assertTask(['watch', '-v'], {cwd: cwd});
assertWatch([function() {
2015-10-11 18:26:55 +08:00
request(35729, function() {
grunt.file.write(path.join(cwd, 'sass', 'one.scss'), '#one {}');
});
}], function(result) {
result = helper.unixify(result);
helper.verboseLog(result);
2014-11-04 09:38:53 +08:00
test.ok(result.indexOf('Live reloading sass/one.scss') === -1,
'Should not trigger live reload on non livereload targets.');
test.ok(result.indexOf('Live reloading css/one.css') !== -1,
'Should trigger live reload when other tasks trigger livereload targets.');
test.done();
});
},
livereloadOnErrorTrue: function(test) {
test.expect(1);
var cwd = path.resolve(fixtures, 'livereload');
var assertWatch = helper.assertTask(['watch:livereloadOnErrorTrue', '-v'], {cwd: cwd});
assertWatch([function() {
2015-10-11 18:26:55 +08:00
request(35729, function() {
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
});
}], function(result) {
result = helper.unixify(result);
helper.verboseLog(result);
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'Should livereload when a task errors w/o flag');
test.done();
});
},
livereloadOnErrorFalse: function(test) {
test.expect(1);
var cwd = path.resolve(fixtures, 'livereload');
var assertWatch = helper.assertTask(['watch:livereloadOnErrorFalse', '-v'], {cwd: cwd});
assertWatch([function() {
2015-10-11 18:26:55 +08:00
request(35729, function() {
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
});
}], function(result) {
result = helper.unixify(result);
helper.verboseLog(result);
2014-11-04 09:38:53 +08:00
test.ok(result.indexOf('Live reloading lib/one.js...') === -1,
'Should not livereload when a task errors with flag');
test.done();
});
},
livereloadOnErrorFalseNoSpawn: function(test) {
test.expect(1);
var cwd = path.resolve(fixtures, 'livereload');
var assertWatch = helper.assertTask(['watch:livereloadOnErrorFalseNoSpawn', '-v'], {cwd: cwd});
assertWatch([function() {
2015-10-11 18:26:55 +08:00
request(35729, function() {
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
});
}], function(result) {
result = helper.unixify(result);
helper.verboseLog(result);
2014-11-04 09:38:53 +08:00
test.ok(result.indexOf('Live reloading lib/one.js...') === -1,
'Should not livereload when a task errors with flag and spawn=false');
test.done();
});
2016-06-06 14:25:10 +08:00
}
};