als-require
Version:
A utility for using CommonJS require in the browser and creating bundles.
62 lines (51 loc) • 2.38 kB
JavaScript
const { describe, it, afterEach } = require('node:test');
const assert = require('node:assert');
const Require = require('../index');
describe('Require class plugins tests', () => {
it('should modify module content using a single plugin', () => {
const plugin = (mod) => {
// Плагин заменяет слово 'original' на 'modified'
mod.content = mod.content.replace(/original/g, 'modified');
};
const mod = new Require('./modules/pluginTest', { plugins: [plugin] });
const fn = mod.fn();
const result = fn();
assert.strictEqual(result, 'This is a modified content.', 'Content should be modified by the plugin');
});
it('should apply multiple plugins sequentially', () => {
const plugin1 = (mod) => {
// Плагин заменяет 'original' на 'temp'
mod.content = mod.content.replace(/original/g, 'temp');
};
const plugin2 = (mod) => {
// Плагин заменяет 'temp' на 'final'
mod.content = mod.content.replace(/temp/g, 'final');
};
const mod = new Require('./modules/pluginTest', { plugins: [plugin1, plugin2] });
const fn = mod.fn();
const result = fn();
assert.strictEqual(result, 'This is a final content.', 'Content should be modified by both plugins sequentially');
});
it('should not break if a plugin does not modify the content', () => {
const plugin = (module) => {
// Пустой плагин, ничего не меняет
};
const mod = new Require('./modules/pluginTest', { plugins: [plugin] });
const fn = mod.fn();
const result = fn();
assert.strictEqual(result, 'This is a original content.', 'Content should remain unchanged');
});
it('should handle plugins that throw errors gracefully', () => {
const plugin = (module) => {
// Плагин выбрасывает ошибку
throw new Error('Plugin error');
};
try {
const mod = new Require('./modules/pluginTest', { plugins: [plugin] });
mod.fn();
assert.fail('Should have thrown an error');
} catch (error) {
assert.strictEqual(error.message, 'Plugin error', 'Error message should match the plugin error');
}
});
});