pp-parachute
Version:
Airdrop JS Applications
131 lines (122 loc) • 5.03 kB
JavaScript
var ScriptBundler = require('../../bundlers/script_bundler');
var Environment = require('../../application/environment');
var path = require('path');
var fs = require('fs');
describe('ScriptBundler', function () {
it('should bundle scripts', function (done) {
var s = new ScriptBundler('js/**/*.js');
var __global = null;
s._bundle().then(function () {
eval(s.rawOutput.toString());
expect(__global).toBe(1);
done();
});
});
it('should bundle them in require order', function (done) {
var s = new ScriptBundler('js/**/*.js');
var testPath = path.join(__dirname, '/js/order.js');
__testGlobal = "hello";
var codeString = "__testGlobal = require('./file1')";
fs.existsSync(testPath) && fs.unlinkSync(testPath);
fs.writeFile(testPath, codeString);
s._bundle().then(function () {
eval(s.rawOutput.toString());
expect(__testGlobal).toBe("abc");
fs.existsSync(testPath) && fs.unlinkSync(testPath);
done();
});
});
it('should watch a bundle and recompile on add', function (done) {
var s = new ScriptBundler('js/**/*.js');
s.createSourceMaps(false);
var testPath = path.join(__dirname, '/js/add.js');
var __testGlobal = null;
var codeString = "__testGlobal = 'here I am'";
fs.existsSync(testPath) && fs.unlinkSync(testPath);
s.watch().then(function () {
s.on('bundleCompleted', function () {
eval(s.rawOutput.toString());
expect(__testGlobal).toBe("here I am");
s.shutdown();
fs.existsSync(testPath) && fs.unlinkSync(testPath);
done();
});
fs.writeFileSync(testPath, codeString);
});
});
it('should watch a bundle and recompile on change', function (done) {
var s = new ScriptBundler('js/**/*.js');
var testPath = path.join(__dirname, '/js/change.js');
var __testGlobal = null;
var codeString = "__testGlobal = 'here I am'";
fs.writeFileSync(testPath, codeString);
s.watch().then(function () {
s.on('bundleCompleted', function (b, v) {
eval(s.rawOutput.toString());
expect(__testGlobal).toBe("here I am again");
s.shutdown();
fs.existsSync(testPath) && fs.unlinkSync(testPath);
done();
});
fs.writeFileSync(testPath, "__testGlobal = 'here I am again'");
});
});
it('should watch a bundle and recompile on remove', function (done) {
var s = new ScriptBundler('js/**/*.js');
var testPath = path.join(__dirname, '/js/remove.js');
var __testGlobal = null;
var codeString = "__testGlobal = 'here I am'";
fs.writeFileSync(testPath, codeString);
s.watch().then(function () {
s.on('bundleCompleted', function (b, v) {
eval(s.rawOutput.toString());
expect(__testGlobal).toBe(null);
s.shutdown();
done();
});
fs.existsSync(testPath) && fs.unlinkSync(testPath);
});
});
it('should return an error if it fails', function(done) {
var s = new ScriptBundler('js/**/*.js');
var testPath = path.join(__dirname, '/js/error.js');
var codeString = "require('not_there')";
fs.writeFileSync(testPath, codeString);
s.on('bundleFailed', function () {
fs.existsSync(testPath) && fs.unlinkSync(testPath);
done();
});
s.watch().then(function () {
s.shutdown();
});
});
describe('Source Maps', function() {
it('should create sourcemaps', function (done) {
var s = new ScriptBundler('js/**/*.js');
var e = new Environment('test');
e.rootPath = __dirname;
e.useBundler(s, 'srcMapTest.js');
e.setOutputDirectory('dist');
e.build().then(function () {
fs.readFile(__dirname + '/dist/srcMapTest.js', 'utf-8', function (error, result) {
expect(result.indexOf('//# sourceMappingURL=')).not.toBe(-1);
done();
})
});
});
it('should not create sourcemaps', function (done) {
var s = new ScriptBundler('js/**/*.js')
.createSourceMaps(false);
var e = new Environment('test');
e.rootPath = __dirname;
e.useBundler(s, 'srcMapTest.js');
e.setOutputDirectory('dist');
e.build().then(function () {
fs.readFile(__dirname + '/dist/srcMapTest.js', 'utf-8', function (error, result) {
expect(result.indexOf('//# sourceMappingURL=')).toBe(-1);
done();
})
});
});
});
});