diff --git a/packages/bruno-js/package.json b/packages/bruno-js/package.json index 22432cc08..41a8b7bcb 100644 --- a/packages/bruno-js/package.json +++ b/packages/bruno-js/package.json @@ -11,7 +11,7 @@ "@n8n/vm2": "^3.9.23" }, "scripts": { - "test": "jest --testPathIgnorePatterns test.js", + "test": "node --experimental-vm-modules $(npx --no-install which jest) --testPathIgnorePatterns test.js", "sandbox:bundle-libraries": "node ./src/sandbox/bundle-libraries.js" }, "dependencies": { diff --git a/packages/bruno-js/src/runtime/test-runtime.js b/packages/bruno-js/src/runtime/test-runtime.js index 72f4fc519..966849d72 100644 --- a/packages/bruno-js/src/runtime/test-runtime.js +++ b/packages/bruno-js/src/runtime/test-runtime.js @@ -15,7 +15,7 @@ const BrunoRequest = require('../bruno-request'); const BrunoResponse = require('../bruno-response'); const Test = require('../test'); const TestResults = require('../test-results'); -const { cleanJson } = require('../utils'); +const { cleanJson, appendAwaitToTestFunc } = require('../utils'); // Inbuilt Library Support const ajv = require('ajv'); @@ -85,7 +85,7 @@ class TestRuntime { } // add 'await' prefix to the test function calls - testsFile = testsFile.replace(/^(?!\s*await\s)(test\([^)]*\))/gm, 'await $1'); + testsFile = appendAwaitToTestFunc(testsFile); const context = { test, diff --git a/packages/bruno-js/src/sandbox/quickjs/index.js b/packages/bruno-js/src/sandbox/quickjs/index.js index a18428c52..5e568ee66 100644 --- a/packages/bruno-js/src/sandbox/quickjs/index.js +++ b/packages/bruno-js/src/sandbox/quickjs/index.js @@ -69,10 +69,11 @@ const executeQuickJsVmAsync = async ({ script: externalScript, context: external return ` globalThis.require = (mod) => { let lib = globalThis.requireObject[mod]; + let isModuleAPath = (module) => (module?.startsWith('.') || module?.startsWith?.(bru.cwd())) if (lib) { return lib; } - else if(mod?.startsWith('.') || mod?.startsWith?.(bru.cwd())){ + else if (isModuleAPath(mod)) { // fetch local module let localModuleCode = globalThis.__brunoLoadLocalModule(mod); @@ -82,7 +83,7 @@ const executeQuickJsVmAsync = async ({ script: externalScript, context: external const copyModuleExportsCode = "\\n;globalThis.requireObject[mod] = module.exports;"; const patchedRequire = ${` "\\n;" + - "let require = (subModule) => globalThis.require(path.resolve(bru.cwd(), mod, '..', subModule))" + + "let require = (subModule) => isModuleAPath(subModule) ? globalThis.require(path.resolve(bru.cwd(), mod, '..', subModule)) : globalThis.require(subModule)" + "\\n;" `} eval(initModuleExportsCode + patchedRequire + localModuleCode + copyModuleExportsCode); diff --git a/packages/bruno-js/src/utils.js b/packages/bruno-js/src/utils.js index e15ec09a7..3c99c48fb 100644 --- a/packages/bruno-js/src/utils.js +++ b/packages/bruno-js/src/utils.js @@ -142,10 +142,15 @@ const cleanJson = (data) => { } }; +const appendAwaitToTestFunc = (str) => { + return str.replace(/(? { describe('expression evaluation', () => { @@ -137,4 +142,70 @@ describe('utils', () => { expect(value).toBe(20); }); }); + + describe('appendAwaitToTestFunc function', () => { + it('example 1', () => { + const inputTestsString = ` + test("should return json", function() { + const data = res.getBody(); + expect(res.getBody()).to.eql({ + "hello": "bruno" + }); + }); + `; + const ouutputTestsString = appendAwaitToTestFunc(inputTestsString); + expect(ouutputTestsString).toBe(` + await test("should return json", function() { + const data = res.getBody(); + expect(res.getBody()).to.eql({ + "hello": "bruno" + }); + }); + `); + }); + + it('example 2', () => { + const inputTestsString = ` + await test("should return json", function() { + const data = res.getBody(); + expect(res.getBody()).to.eql({ + "hello": "bruno" + }); + }); + test("should return json", function() { + const data = res.getBody(); + expect(res.getBody()).to.eql({ + "hello": "bruno" + }); + }); + test("should return json", function() { + const data = res.getBody(); + expect(res.getBody()).to.eql({ + "hello": "bruno" + }); + }); + `; + const ouutputTestsString = appendAwaitToTestFunc(inputTestsString); + expect(ouutputTestsString).toBe(` + await test("should return json", function() { + const data = res.getBody(); + expect(res.getBody()).to.eql({ + "hello": "bruno" + }); + }); + await test("should return json", function() { + const data = res.getBody(); + expect(res.getBody()).to.eql({ + "hello": "bruno" + }); + }); + await test("should return json", function() { + const data = res.getBody(); + expect(res.getBody()).to.eql({ + "hello": "bruno" + }); + }); + `); + }); + }); });