diff --git a/src/15utility.js b/src/15utility.js index 4c0ec4770c..20dbfa17b7 100755 --- a/src/15utility.js +++ b/src/15utility.js @@ -194,7 +194,10 @@ utils.isWebWorker = (function () { */ utils.isNode = (function () { try { - return utils.isNativeFunction(utils.global.process.reallyExit); + if (typeof process === 'undefined' || !process.versions || !process.versions.node) { + return false; + } + return true; } catch (e) { return false; } diff --git a/test/test1889.js b/test/test1889.js new file mode 100644 index 0000000000..2888c8cfa0 --- /dev/null +++ b/test/test1889.js @@ -0,0 +1,38 @@ +if (typeof exports === 'object') { + var assert = require('assert'); + var alasql = require('..'); +} + +var test = '1889'; // insert test file number + +describe('Test 1889 - Ensure utils.isNode handles node and non-Node environments', function () { + let originalProcess; + + before(function() { + // Store the original process object + originalProcess = global.process; + }); + + after(function() { + // Restore the original process object after all tests + global.process = originalProcess; + }); + + it('Positive: Detect Node environment', function () { + // No modification needed here, running in actual Node.js environment + const isNode = alasql.utils.isNode; + assert.strictEqual(isNode, true, 'utils.isNode should return true in a Node.js environment'); + }); + + it('Negative: Detect Node environment', function () { + // Temporarily override the global process object + global.process = null; + + delete require.cache[require.resolve('..')]; + const reloadedAlasql = require('..'); + + const isNodeAfterModification = reloadedAlasql.utils.isNode; + assert.strictEqual(isNodeAfterModification, false, 'utils.isNode should return true in a Node.js environment'); + }); + +});