UNPKG

siesta-lite

Version:

Stress-free JavaScript unit testing and functional testing tool, works in NodeJS and browsers

58 lines (39 loc) 1.45 kB
/* Siesta 5.6.1 Copyright(c) 2009-2022 Bryntum AB https://bryntum.com/contact https://bryntum.com/products/siesta/license */ Class('Scope.Provider.NodeJsEmbed', { isa : Scope.Provider.NodeJS, methods : { runCode : function (text, callback) { var vm = require('vm') // the '/' filename is required for loading Ecma modules // this goes as "referrer" for the module url var res = vm.runInThisContext(this.stripBom(text), { filename : '/' }) callback && callback(res) return res }, requireCjsModule : function (url, callback) { try { var res = this.requireFunc(this.fileNameToModuleId(url)) } catch (e) { this.onErrorHandler && this.onErrorHandler(e.message, null, null, null, e) } callback && callback(res) return res }, importEcmaModule : function (url, callback) { // seems node supports dynamic `import` ONLY for the current context (on the C++ level) this.runCode("import('" + this.fileNameToModuleId(url) + "')").then( function (res) { callback && callback(res) }, function () {} ) }, create : function (callback) { this.scope = SCOPE callback && callback() } } })