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:
		
							
								
								
									
										81
									
								
								test/test.node.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								test/test.node.ts
									
									
									
									
									
										Normal 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();
 | 
			
		||||
		Reference in New Issue
	
	Block a user