Skip to content

Commit

Permalink
chore(all): refactor unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
terinjokes committed May 20, 2017
1 parent f2b779b commit 5d4c4c6
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 579 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ node_modules
build
components
coverage
.nyc_output
10 changes: 9 additions & 1 deletion composer.js
Expand Up @@ -4,6 +4,14 @@ var minify = require('./lib/minify');

module.exports = function(uglify, logger) {
return function(opts) {
return through.obj(minify(uglify, logger)(opts));
var minifier = minify(uglify, logger)(opts);
return through.obj(function(file, encoding, callback) {
try {
var newFile = minifier(file);
callback(null, newFile);
} catch (err) {
callback(err);
}
});
};
};
16 changes: 9 additions & 7 deletions lib/minify.js
Expand Up @@ -20,16 +20,16 @@ module.exports = function(uglify, log) {
}

return function(opts) {
return function(file, encoding, callback) {
return function(file) {
var options = setup(opts || {});
var hasSourceMaps = Boolean(file.sourceMap);

if (file.isNull()) {
return callback(null, file);
return file;
}

if (file.isStream()) {
return callback(createError(file, 'Streaming not supported', null));
throw createError(file, 'Streaming not supported', null);
}

if (hasSourceMaps) {
Expand All @@ -50,9 +50,11 @@ module.exports = function(uglify, log) {

var mangled = uglify.minify(fileMap, options);

if (mangled.error) {
return callback(
createError(file, 'unable to minify JavaScript', mangled.error)
if (!mangled || mangled.error) {
throw createError(
file,
'unable to minify JavaScript',
mangled && mangled.error
);
}

Expand All @@ -69,7 +71,7 @@ module.exports = function(uglify, log) {
applySourceMap(file, sourceMap);
}

callback(null, file);
return file;
};
};
};
9 changes: 3 additions & 6 deletions package.json
Expand Up @@ -21,15 +21,12 @@
"eslint-plugin-no-use-extend-native": "^0.3.12",
"eslint-plugin-prettier": "^2.0.1",
"eslint-plugin-unicorn": "^2.1.0",
"gulp-concat": "^2.0.0",
"gulp-sourcemaps": "^2.6.0",
"intelli-espower-loader": "^1.0.1",
"istanbul": "^0.4.0",
"mississippi": "^1.2.0",
"mocha": "^3.0.1",
"nyc": "^10.3.2",
"power-assert": "^1.4.1",
"prettier": "^1.1.0",
"semver": "^5.3.0",
"source-list-map": "^1.1.2",
"tape": "^4.0.0",
"testdouble": "^2.1.2",
"vinyl": "^2.0.0"
Expand Down Expand Up @@ -74,7 +71,7 @@
],
"scripts": {
"lint": "eslint *.js lib test",
"test": "mocha --require intelli-espower-loader",
"test": "nyc --reporter=lcov --reporter=text mocha --require intelli-espower-loader",
"coverage": "cat ./coverage/lcov.info | coveralls"
},
"greenkeeper": {
Expand Down
2 changes: 1 addition & 1 deletion test/create-error.js
@@ -1,6 +1,6 @@
'use strict';
var mocha = require('mocha');
var assert = require('power-assert');
var assert = require('assert');
var Vinyl = require('vinyl');
var createError = require('../lib/create-error');
var GulpUglifyError = require('../lib/gulp-uglify-error');
Expand Down
117 changes: 78 additions & 39 deletions test/err.js
@@ -1,38 +1,50 @@
'use strict';
var mocha = require('mocha');
var assert = require('power-assert');
var assert = require('assert');
var Vinyl = require('vinyl');
var mississippi = require('mississippi');
var td = require('testdouble');
var GulpUglifyError = require('../lib/gulp-uglify-error');
var gulpUglify = require('../');

var pipe = mississippi.pipe;
var to = mississippi.to;
var from = mississippi.from;
var minify = require('../lib/minify');

var describe = mocha.describe;
var it = mocha.it;

describe('stream errors', function() {
it('should report files in error', function(done) {
describe('errors', function() {
it('should report files in error', function() {
var testFile = new Vinyl({
cwd: '/home/terin/broken-promises/',
base: '/home/terin/broken-promises/test',
path: '/home/terin/broken-promises/test/test1.js',
contents: new Buffer('function errorFunction(error)\n{')
});
var uglify = td.object(['minify']);
var logger = td.object(['warn']);
var expOptions = {
output: {}
};
var err = new Error();
err.line = 28889;

td
.when(
uglify.minify(
{
'test1.js': 'function errorFunction(error)\n{'
},
expOptions
)
)
.thenReturn({
error: err
});

pipe(
[
from.obj([testFile]),
gulpUglify(),
to.obj(function(chunk, enc, next) {
assert(false, 'we shouldn\t have gotten here');
next();
})
],
var subject = minify(uglify, logger)({});

assert.throws(
function() {
subject(testFile);
},
function(err) {
assert.ok(err instanceof Error, 'argument should be of type Error');
assert.ok(
err instanceof GulpUglifyError,
'argument should be of type GulpUglifyError'
Expand All @@ -43,37 +55,61 @@ describe('stream errors', function() {
testFile.path,
'error reports correct file name'
);
assert.equal(err.cause.line, 2, 'error reports correct line number');
assert.equal(
err.cause.line,
28889,
'error reports correct line number'
);
assert.ok(err.stack, 'error has a stack');
assert.ok(!err.showStack, 'error is configured to not print the stack');
done();
assert.ok(err instanceof Error, 'argument should be of type Error');

return true;
}
);

td.verify(logger.warn(), {times: 0, ignoreExtraArgs: true});
});

it("shouldn't blow up when given output options", function(done) {
it("shouldn't blow up when given output options", function() {
var testFile = new Vinyl({
cwd: '/home/terin/broken-promises/',
base: '/home/terin/broken-promises/test',
path: '/home/terin/broken-promises/test/test2.js',
contents: new Buffer(
'"use strict"; (function(console, first, second) { console.log(first + second) }(5, 10))'
path: '/home/terin/broken-promises/test/test1.js',
contents: new Buffer('{}')
});
var uglify = td.object(['minify']);
var logger = td.object(['warn']);
var expOptions = {
output: {
exportAll: true
}
};
var err = new Error('`exportAll` is not a supported option');

td
.when(
uglify.minify(
{
'test1.js': '{}'
},
expOptions
)
)
.thenReturn({
error: err
});

var subject = minify(uglify, logger)({
output: {
exportAll: true
}
});

pipe(
[
from.obj([testFile]),
gulpUglify({
output: {
exportAll: true
}
}),
to.obj(function(chunk, enc, next) {
assert(false, 'we shouldn\t have gotten here');
next();
})
],
assert.throws(
function() {
subject(testFile);
},
function(err) {
assert.ok(err instanceof Error, 'argument should be of type Error');
assert.ok(
Expand All @@ -91,8 +127,11 @@ describe('stream errors', function() {
'error reports correct file name'
);
assert.ok(!err.showStack, 'error is configured to not print the stack');
done();

return true;
}
);

td.verify(logger.warn(), {times: 0, ignoreExtraArgs: true});
});
});
71 changes: 0 additions & 71 deletions test/injectable.js

This file was deleted.

0 comments on commit 5d4c4c6

Please sign in to comment.