UNPKG

als-require

Version:

A utility for using CommonJS require in the browser and creating bundles.

46 lines (39 loc) 1.99 kB
const assert = require('assert'); const { describe, it, beforeEach } = require('node:test'); const getNodeModules = require('../lib/node-modules'); describe('getNodeModules', () => { let logs = []; const logger = { warn: (msg) => logs.push(msg) }; beforeEach(() => logs = []); it('should return correct path for als-called-from library', () => { const result = getNodeModules('als-called-from', logger); assert.ok(result.endsWith('/node_modules/als-called-from/index.js'), 'Path should point to als-called-from main file'); assert.deepStrictEqual(logs, [], 'No warnings should be logged for a valid module'); }); it('should log a warning and return false for standard Node.js module', () => { const result = getNodeModules('fs', logger); assert.strictEqual(result, false, 'Should return false for standard Node.js module'); assert.deepStrictEqual( logs, ['The module "fs" can\'t be imported and will be replaced with {}'], 'Warning should be logged for standard Node.js module' ); }); it('should log a warning and return false for non-existent module', () => { const result = getNodeModules('non-existent-module', logger); assert.strictEqual(result, false, 'Should return false for non-existent module'); assert.deepStrictEqual( logs, ['The module "non-existent-module" can\'t be imported and will be replaced with {}'], 'Warning should be logged for non-existent module' ); }); it('should return correct path for als-called-from/test.js', () => { const result = getNodeModules('als-called-from/test.js', logger); assert.ok( result.endsWith('/node_modules/als-called-from/test.js'), 'Path should point to als-called-from/test.js file' ); assert.deepStrictEqual(logs, [], 'No warnings should be logged for a valid file within a module'); }); });