diff --git a/test/parallel/test-buffer-tostring-rangeerror.js b/test/parallel/test-buffer-tostring-rangeerror.js index a5591213cf2c54..bee04d7d794b6c 100644 --- a/test/parallel/test-buffer-tostring-rangeerror.js +++ b/test/parallel/test-buffer-tostring-rangeerror.js @@ -23,8 +23,22 @@ const message = { code: 'ERR_STRING_TOO_LONG', name: 'Error', }; -assert.throws(() => Buffer(len).toString('utf8'), message); -assert.throws(() => Buffer.allocUnsafeSlow(len).toString('utf8'), message); -assert.throws(() => Buffer.alloc(len).toString('utf8'), message); -assert.throws(() => Buffer.allocUnsafe(len).toString('utf8'), message); -assert.throws(() => Buffer.allocUnsafeSlow(len).toString('utf8'), message); + +function test(getBuffer) { + let buf; + try { + buf = getBuffer(); + } catch (e) { + // If the buffer allocation fails, we skip the test. + if (e.code === 'ERR_MEMORY_ALLOCATION_FAILED' || /Array buffer allocation failed/.test(e.message)) { + return; + } + } + assert.throws(() => { buf.toString('utf8'); }, message); +} + +test(() => Buffer(len)); +test(() => Buffer.allocUnsafeSlow(len)); +test(() => Buffer.alloc(len)); +test(() => Buffer.allocUnsafe(len)); +test(() => Buffer.allocUnsafeSlow(len));