From eef3cd2d2462fecc41273ec9bee38b9137aae46b Mon Sep 17 00:00:00 2001 From: AliusDieMorietur Date: Thu, 29 Apr 2021 23:33:40 +0300 Subject: [PATCH] Add tests and example for module nesting Refs: https://github.com/metarhia/metavm/pull/58 PR-URL: https://github.com/metarhia/metavm/pull/58 --- test/examples/nestedmodule1.js | 7 +++++ test/examples/nestedmodule2.js | 6 ++++ test/unit.js | 55 ++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 test/examples/nestedmodule1.js create mode 100644 test/examples/nestedmodule2.js diff --git a/test/examples/nestedmodule1.js b/test/examples/nestedmodule1.js new file mode 100644 index 0000000..9d61665 --- /dev/null +++ b/test/examples/nestedmodule1.js @@ -0,0 +1,7 @@ +/* eslint-disable */ +({ + name: 'nestedmodule1', + value: 1, + nested: require('./test/examples/nestedmodule2.js'), +}); +/* eslint-enable */ diff --git a/test/examples/nestedmodule2.js b/test/examples/nestedmodule2.js new file mode 100644 index 0000000..5a31a27 --- /dev/null +++ b/test/examples/nestedmodule2.js @@ -0,0 +1,6 @@ +/* eslint-disable */ +({ + name: 'nestedmodule2', + value: 2, +}); +/* eslint-enable */ diff --git a/test/unit.js b/test/unit.js index f9f2ef4..60e4f42 100644 --- a/test/unit.js +++ b/test/unit.js @@ -1,5 +1,6 @@ 'use strict'; +const fs = require('fs'); const metavm = require('..'); const path = require('path'); const metatests = require('metatests'); @@ -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(); +});