Test minifier

v1.18.x
Peter Jaszkowiak 8 years ago
parent 4c1e25c8ce
commit d988e8a50f

@ -17,3 +17,4 @@ logs/
/coverage
/build
.eslintrc
test/files

@ -156,7 +156,7 @@ function concat(data, callback) {
return callback(err);
}
var output = files.join(os.EOL + ';');
var output = files.join('\n;');
callback(null, { code: output });
});

@ -1,8 +1,102 @@
'use strict';
var string = require('string');
var path = require('path');
var fs = require('fs');
var assert = require('assert');
var mkdirp = require('mkdirp');
var db = require('./mocks/databasemock');
var file = require('../src/file');
describe('minifier', function () {
before(function (done) {
mkdirp(path.join(__dirname, '../build/test'), done);
});
var minifier = require('../src/meta/minifier');
var scripts = [
path.resolve(__dirname, './files/1.js'),
path.resolve(__dirname, './files/2.js'),
];
it('.js.bundle() should concat scripts', function (done) {
minifier.js.bundle(scripts, false, false, function (err, bundle) {
assert.ifError(err);
assert.strictEqual(
bundle.code,
'(function (window, document) {' +
'\n\twindow.doStuff = function () {' +
'\n\t\tdocument.body.innerHTML = \'Stuff has been done\';' +
'\n\t};' +
'\n})(window, document);' +
'\n' +
'\n;function foo(name, age) {' +
'\n\treturn \'The person known as "\' + name + \'" is \' + age + \' years old\';' +
'\n}' +
'\n'
);
done();
});
});
it('.js.bundle() should minify scripts', function (done) {
minifier.js.bundle(scripts, true, false, function (err, bundle) {
assert.ifError(err);
assert.strictEqual(
bundle.code,
'(function(n,o){n.doStuff=function(){o.body.innerHTML="Stuff has been done"}})(window,document);function foo(n,o){return\'The person known as "\'+n+\'" is \'+o+" years old"}'
);
done();
});
});
it('.js.minifyBatch() should minify each script', function (done) {
var s = scripts.map(function (script) {
return {
srcPath: script,
destPath: path.resolve(__dirname, '../build/test', path.basename(script)),
};
});
minifier.js.minifyBatch(s, false, function (err) {
assert.ifError(err);
assert(file.existsSync(s[0].destPath));
assert(file.existsSync(s[1].destPath));
fs.readFile(s[0].destPath, function (err, buffer) {
assert.ifError(err);
assert.strictEqual(
buffer.toString(),
'(function(n,o){n.doStuff=function(){o.body.innerHTML="Stuff has been done"}})(window,document);'
);
done();
});
});
});
var styles = [
'@import (inline) "./1.css";',
'@import "./2.less";',
].join('\n');
var paths = [
path.resolve(__dirname, './files'),
];
it('.css.bundle() should concat styles', function (done) {
minifier.css.bundle(styles, paths, false, false, function (err, bundle) {
assert.ifError(err);
assert.strictEqual(bundle.code, '.help { margin: 10px; } .yellow { background: yellow; }\n.help {\n display: block;\n}\n.help .blue {\n background: blue;\n}\n');
done();
});
});
it('.css.bundle() should minify styles', function (done) {
minifier.css.bundle(styles, paths, true, false, function (err, bundle) {
assert.ifError(err);
assert.strictEqual(bundle.code, '.help{margin:10px;display:block}.yellow{background:#ff0}.help .blue{background:#00f}');
done();
});
});
});
describe('Build', function () {
it('should build all assets', function (done) {

@ -0,0 +1 @@
.help { margin: 10px; } .yellow { background: yellow; }

@ -0,0 +1,5 @@
(function (window, document) {
window.doStuff = function () {
document.body.innerHTML = 'Stuff has been done';
};
})(window, document);

@ -0,0 +1,3 @@
function foo(name, age) {
return 'The person known as "' + name + '" is ' + age + ' years old';
}

@ -0,0 +1 @@
.help { display: block; .blue { background: blue; } }
Loading…
Cancel
Save