In this case ... is not an extension operator, but a syntax for defining rest parameters .
Therefore, a piece inside a function is an array, and when casting an array to a string , other rules apply , namely: if the element is null or undefined , then the output will be an empty string.
As in the test buffer(null); is called buffer(null); , inside the piece=== [null] function piece=== [null]
And when casting to the string of this array, an empty string is obtained.
In addition, if several parameters are transferred to the function buffer, then they will be separated by commas at the output.
If only one parameter is supposed to be present, there is no need to use rest parameters, it is enough to explicitly describe one parameter.
If you intend to pass several parameters, and the behavior remains the same, it is worth changing the function makeBuffer working inside it with a piece as with an array, for example:
function makeBuffer() { let text = ''; //используем деструктивные параметры return function(...piece) { if (piece.length == 0) { // вызов без аргументов return text; } text += piece.map(el=>el+'').join(''); }; }; let buffer; beforeEach(function() { buffer = makeBuffer(); }); it("возвращает пустую строку по умолчанию", function() { assert.strictEqual(buffer(), ""); }); it("добавляет аргументы в буффер", function() { buffer('Замыкания'); buffer(' Использовать'); buffer(' Нужно!'); assert.equal(buffer(), 'Замыкания Использовать Нужно!'); }); it("приводит всё к строке", function() { buffer(null); buffer(false); assert.equal(buffer(), "nullfalse"); }); it("добавляет аргументы в буффер, несколько аргументов", function() { buffer('Замыкания',' ','Использовать'); buffer(' ','Нужно!'); assert.equal(buffer(), 'Замыкания Использовать Нужно!'); });
<script src="https://js.cx/test/libs.js"></script>