Skip to content

Commit

Permalink
Add tests and example for module nesting
Browse files Browse the repository at this point in the history
Refs: #58

PR-URL: #58
  • Loading branch information
AliusDieMorietur authored and tshemsedinov committed Apr 11, 2022
1 parent 673ca52 commit eef3cd2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/examples/nestedmodule1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable */
({
name: 'nestedmodule1',
value: 1,
nested: require('./test/examples/nestedmodule2.js'),
});
/* eslint-enable */
6 changes: 6 additions & 0 deletions test/examples/nestedmodule2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint-disable */
({
name: 'nestedmodule2',
value: 2,
});
/* eslint-enable */
55 changes: 55 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const fs = require('fs');
const metavm = require('..');
const path = require('path');
const metatests = require('metatests');
Expand Down Expand Up @@ -184,3 +185,57 @@ metatests.test('Call undefined as a function', async (test) => {
}
test.end();
});

metatests.test('Metarequire node internal module', async (test) => {
const sandbox = {};
sandbox.global = sandbox;
sandbox.require = metavm.metarequire(sandbox, { fs });
const context = metavm.createContext(sandbox);
const src = `({ fs: require('fs') })`;
const ms = metavm.createScript('Example', src, { context });
test.strictSame(typeof ms.exports, 'object');
test.strictSame(typeof ms.exports.fs.promises, 'object');
test.end();
});

metatests.test('Metarequire internal not permitted', async (test) => {
const sandbox = {};
sandbox.global = sandbox;
sandbox.require = metavm.metarequire(sandbox);
const context = metavm.createContext(sandbox);
const src = `({ fs: require('fs') })`;
try {
const ms = metavm.createScript('Example', src, { context });
test.strictSame(ms, undefined);
} catch (err) {
test.strictSame(err.message, `Cannot find module: 'fs'`);
}
test.end();
});

metatests.test('Metarequire non-existent module', async (test) => {
const sandbox = {};
sandbox.global = sandbox;
sandbox.require = metavm.metarequire(sandbox);
const context = metavm.createContext(sandbox);
const src = `({ notExist: require('nothing') })`;
try {
const ms = metavm.createScript('Example', src, { context });
test.strictSame(ms, undefined);
} catch (err) {
test.strictSame(err.message, `Cannot find module: 'nothing'`);
}
test.end();
});

metatests.test('Metarequire nestsed modules', async (test) => {
const sandbox = {};
sandbox.global = sandbox;
sandbox.require = metavm.metarequire(sandbox);
const context = metavm.createContext(sandbox);
const src = `({ loaded: require('./test/examples/nestedmodule1.js') })`;
const ms = metavm.createScript('Example', src, { context });
test.strictSame(ms.exports.loaded.exports.value, 1);
test.strictSame(ms.exports.loaded.exports.nested.exports.value, 2);
test.end();
});

0 comments on commit eef3cd2

Please sign in to comment.