UNPKG

errnext

Version:

Generic middleware based on (err, next, ...args) pattern.

84 lines (80 loc) 2.6 kB
'use strict'; var _index = require('./index'); function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } /* eslint-env jest */ describe('compose', function () { it('calls 1 function', function (done) { (0, _index.compose)(function (err, next) { return next(err, 1); }, function (err, next, x) { return next(err, x * 2); }, function (err, next, x) { expect(err).toEqual(undefined); expect(x).toEqual(2); done(); })(); }); it('callNext', function (done) { (0, _index.compose)(function (err, next) { return next(err, 1); }, _index.callNext, function (err, next, x) { return next(err, x * 10); }, function (err, next, result) { expect(err).toEqual(undefined); expect(result).toEqual(10); done(); })(); }); it('null', function (done) { (0, _index.compose)(function (err, next) { return next(err, 1); }, null, function (err, next, x) { return next(err, x * 10); }, function (err, next, result) { expect(err).toEqual(undefined); expect(result).toEqual(10); done(); })(); }); it('calls more functions', function (done) { (0, _index.compose)(function (err, next) { return next(err, 1); }, function (err, next, x) { return next(err, x * 2); }, function (err, next, x) { return next(err, x * 3); }, function (err, next, x) { return next(err, x * 10); }, function (err, next, x) { return next(err, x + 0.5); }, function (err, next, result) { expect(err).toEqual(undefined); expect(result).toEqual(60.5); done(); })(); }); it('works with multiple arguments', function (done) { (0, _index.compose)(function (err, next) { return next(err, 1); }, function (err, next, x) { return next(err, x, 2); }, function (err, next, x, y) { return next(err, x + y, 10, 100); }, function (err, next, x, y, z) { return next(err, x + y + z); }, function (err, next, result) { expect(err).toEqual(undefined); expect(result).toEqual(113); done(); })(); }); it('calls 10000 functions', function (done) { var fns = Array(10000).fill(function (err, next, x) { return next(err, x + 1); }); _index.compose.apply(undefined, _toConsumableArray(fns))(null, function (err, x) { expect(err).toEqual(null); expect(x).toEqual(10000); done(); }, 0); }); });