BREAKING CHANGE(Smartenv): Add Deno and Bun runtime detection, introduce getSafeModuleFor API, update docs and tests, and make isNode semantics Node-only (breaking change)

This commit is contained in:
2025-11-01 15:26:26 +00:00
parent ce338e27ea
commit aeac92f3fd
15 changed files with 10653 additions and 2151 deletions

81
test/test.bun.ts Normal file
View File

@@ -0,0 +1,81 @@
import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as smartenv from '../ts/index.js';
let testEnv: smartenv.Smartenv;
tap.test('should create smartenv instance', async () => {
testEnv = new smartenv.Smartenv();
});
tap.test('should detect Bun runtime correctly', async () => {
expect(testEnv.runtimeEnv).toEqual('bun');
expect(testEnv.isBun).toBeTrue();
expect(testEnv.isNode).toBeFalse();
expect(testEnv.isDeno).toBeFalse();
expect(testEnv.isBrowser).toBeFalse();
console.log(' Bun runtime detected correctly');
});
tap.test('should get Bun version', async () => {
const version = testEnv.bunVersion;
expect(version).not.toEqual('undefined');
expect(typeof version).toEqual('string');
console.log('Bun version is ' + version);
});
tap.test('should print environment', async () => {
testEnv.printEnv();
});
tap.test('should load modules for Bun', async () => {
const pathModule = await testEnv.getSafeModuleFor('bun', 'path');
expect(pathModule).not.toBeUndefined();
expect(typeof pathModule.join).toEqual('function');
console.log(' Successfully loaded module for Bun');
});
tap.test('should load modules for server runtimes', async () => {
const pathModule = await testEnv.getSafeModuleFor('server', 'path');
expect(pathModule).not.toBeUndefined();
expect(typeof pathModule.join).toEqual('function');
console.log(' Successfully loaded module with server target');
});
tap.test('should load modules for array of runtimes', async () => {
const pathModule = await testEnv.getSafeModuleFor(['bun', 'node'], 'path');
expect(pathModule).not.toBeUndefined();
expect(typeof pathModule.join).toEqual('function');
console.log(' Successfully loaded module with array target');
});
tap.test('should not load modules for wrong runtime', async () => {
const result = await testEnv.getSafeModuleFor('node', 'path');
expect(result).toBeUndefined();
console.log(' Correctly rejected Node.js-only module in Bun');
});
tap.test('should detect OS', async () => {
const resultMac = await testEnv.isMacAsync();
const resultLinux = await testEnv.isLinuxAsync();
const resultWindows = await testEnv.isWindowsAsync();
const osModule = await import('os');
if (resultMac) {
expect(osModule.platform()).toEqual('darwin');
console.log('platform is Mac!');
} else if (resultLinux) {
expect(osModule.platform()).toEqual('linux');
console.log('platform is Linux!');
} else {
expect(osModule.platform()).toEqual('win32');
console.log('platform is Windows!');
}
});
tap.test('should detect CI environment if present', async () => {
if (process.env.CI) {
expect(testEnv.isCI).toBeTrue();
}
console.log('CI detection: ' + testEnv.isCI);
});
tap.start();

58
test/test.chrome.ts Normal file
View File

@@ -0,0 +1,58 @@
import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as smartenv from '../ts/index.js';
let testEnv: smartenv.Smartenv;
tap.test('should create smartenv instance', async () => {
testEnv = new smartenv.Smartenv();
});
tap.test('should detect browser runtime correctly', async () => {
expect(testEnv.runtimeEnv).toEqual('browser');
expect(testEnv.isBrowser).toBeTrue();
expect(testEnv.isNode).toBeFalse();
expect(testEnv.isDeno).toBeFalse();
expect(testEnv.isBun).toBeFalse();
console.log(' Browser runtime detected correctly');
});
tap.test('should get user agent', async () => {
const userAgent = testEnv.userAgent;
expect(userAgent).not.toEqual('undefined');
expect(typeof userAgent).toEqual('string');
console.log('User agent: ' + userAgent);
});
tap.test('should print environment', async () => {
testEnv.printEnv();
});
tap.test('should not get server runtime versions', async () => {
expect(testEnv.nodeVersion).toEqual('undefined');
expect(testEnv.denoVersion).toEqual('undefined');
expect(testEnv.bunVersion).toEqual('undefined');
console.log(' Server runtime versions correctly return undefined in browser');
});
tap.test('should not load server modules', async () => {
const result = await testEnv.getSafeModuleFor('server', 'path');
expect(result).toBeUndefined();
console.log(' Correctly rejected server module in browser');
});
tap.test('should not detect as CI', async () => {
expect(testEnv.isCI).toBeFalse();
console.log(' CI detection correctly false in browser');
});
tap.test('OS detection should return false in browser', async () => {
const resultMac = await testEnv.isMacAsync();
const resultLinux = await testEnv.isLinuxAsync();
const resultWindows = await testEnv.isWindowsAsync();
expect(resultMac).toBeFalse();
expect(resultLinux).toBeFalse();
expect(resultWindows).toBeFalse();
console.log(' OS detection correctly returns false in browser');
});
tap.start();

59
test/test.deno.ts Normal file
View File

@@ -0,0 +1,59 @@
import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as smartenv from '../ts/index.js';
let testEnv: smartenv.Smartenv;
tap.test('should create smartenv instance', async () => {
testEnv = new smartenv.Smartenv();
});
tap.test('should detect Deno runtime correctly', async () => {
expect(testEnv.runtimeEnv).toEqual('deno');
expect(testEnv.isDeno).toBeTrue();
expect(testEnv.isNode).toBeFalse();
expect(testEnv.isBun).toBeFalse();
expect(testEnv.isBrowser).toBeFalse();
console.log(' Deno runtime detected correctly');
});
tap.test('should get Deno version', async () => {
const version = testEnv.denoVersion;
expect(version).not.toEqual('undefined');
expect(typeof version).toEqual('string');
console.log('Deno version is ' + version);
});
tap.test('should print environment', async () => {
testEnv.printEnv();
});
tap.test('should load modules for Deno', async () => {
// Deno requires 'node:' prefix for Node.js built-in modules
const pathModule = await testEnv.getSafeModuleFor('deno', 'node:path');
expect(pathModule).not.toBeUndefined();
expect(typeof pathModule.join).toEqual('function');
console.log(' Successfully loaded module for Deno');
});
tap.test('should load modules for server runtimes', async () => {
// Deno requires 'node:' prefix for Node.js built-in modules
const pathModule = await testEnv.getSafeModuleFor('server', 'node:path');
expect(pathModule).not.toBeUndefined();
expect(typeof pathModule.join).toEqual('function');
console.log(' Successfully loaded module with server target');
});
tap.test('should not load modules for wrong runtime', async () => {
const result = await testEnv.getSafeModuleFor('node', 'node:path');
expect(result).toBeUndefined();
console.log(' Correctly rejected Node.js-only module in Deno');
});
tap.test('should detect CI environment if present', async () => {
// CI detection relies on Node.js process.env, which may not work in Deno
// This test documents expected behavior
const isCI = testEnv.isCI;
console.log('CI detection in Deno: ' + isCI);
});
tap.start();

81
test/test.node.ts Normal file
View File

@@ -0,0 +1,81 @@
import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as smartenv from '../ts/index.js';
let testEnv: smartenv.Smartenv;
tap.test('should create smartenv instance', async () => {
testEnv = new smartenv.Smartenv();
});
tap.test('should detect Node.js runtime correctly', async () => {
expect(testEnv.runtimeEnv).toEqual('node');
expect(testEnv.isNode).toBeTrue();
expect(testEnv.isDeno).toBeFalse();
expect(testEnv.isBun).toBeFalse();
expect(testEnv.isBrowser).toBeFalse();
console.log('✓ Node.js runtime detected correctly (not confused with Deno or Bun)');
});
tap.test('should get Node.js version', async () => {
const version = testEnv.nodeVersion;
expect(version).not.toEqual('undefined');
expect(typeof version).toEqual('string');
expect(version).toMatch(/^v\d+\.\d+\.\d+/);
console.log('Node.js version is ' + version);
});
tap.test('should print environment', async () => {
testEnv.printEnv();
});
tap.test('should load modules for Node.js', async () => {
const pathModule = await testEnv.getSafeModuleFor('node', 'path');
expect(pathModule).not.toBeUndefined();
expect(typeof pathModule.join).toEqual('function');
console.log('✓ Successfully loaded module for Node.js');
});
tap.test('should load modules for server runtimes', async () => {
const pathModule = await testEnv.getSafeModuleFor('server', 'path');
expect(pathModule).not.toBeUndefined();
expect(typeof pathModule.join).toEqual('function');
console.log('✓ Successfully loaded module with server target');
});
tap.test('should load modules for array of runtimes', async () => {
const pathModule = await testEnv.getSafeModuleFor(['node', 'deno'], 'path');
expect(pathModule).not.toBeUndefined();
expect(typeof pathModule.join).toEqual('function');
console.log('✓ Successfully loaded module with array target');
});
tap.test('should not load modules for wrong runtime', async () => {
const result = await testEnv.getSafeModuleFor('browser', 'path');
expect(result).toBeUndefined();
console.log('✓ Correctly rejected browser-only module in Node.js');
});
tap.test('should get os', async () => {
const resultMac = await testEnv.isMacAsync();
const resultLinux = await testEnv.isLinuxAsync();
const resultWindows = await testEnv.isWindowsAsync();
const osModule = await import('os');
if (resultMac) {
expect(osModule.platform()).toEqual('darwin');
console.log('platform is Mac!');
} else if (resultLinux) {
expect(osModule.platform()).toEqual('linux');
console.log('platform is Linux!');
} else {
expect(osModule.platform()).toEqual('win32');
console.log('platform is Windows!');
}
});
tap.test('should state wether we are in CI', async () => {
if (process.env.CI) {
expect(testEnv.isCI).toBeTrue();
}
});
tap.start();

View File

@@ -1,37 +0,0 @@
import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as smartenv from '../ts/index.js';
let testEnv: smartenv.Smartenv;
tap.test('should print env', async () => {
testEnv = new smartenv.Smartenv();
});
tap.test('should print a overview to console', async () => {
testEnv.printEnv();
});
tap.test('should get os', async () => {
const resultMac = await testEnv.isMacAsync();
const resultLinux = await testEnv.isLinuxAsync();
const resultWindows = await testEnv.isWindowsAsync();
const osModule = await import('os');
if (resultMac) {
expect(osModule.platform()).toEqual('darwin');
console.log('platform is Mac!');
} else if (resultLinux) {
expect(osModule.platform()).toEqual('linux');
console.log('platform is Linux!');
} else {
expect(osModule.platform()).toEqual('win32');
console.log('platform is Windows!');
}
});
tap.test('should state wether we are in CI', async () => {
if (process.env.CI) {
expect(testEnv.isCI).toBeTrue();
}
});
tap.start();