mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-24 13:15:40 +00:00
wip: safe mode updates (#2874)
* wip: safe mode updates * wip: safe mode updates
This commit is contained in:
@@ -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": {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -142,10 +142,15 @@ const cleanJson = (data) => {
|
||||
}
|
||||
};
|
||||
|
||||
const appendAwaitToTestFunc = (str) => {
|
||||
return str.replace(/(?<!\.\s*)(?<!await\s)(test\()/g, 'await $1');
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
evaluateJsExpression,
|
||||
evaluateJsTemplateLiteral,
|
||||
createResponseParser,
|
||||
internalExpressionCache,
|
||||
cleanJson
|
||||
cleanJson,
|
||||
appendAwaitToTestFunc
|
||||
};
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
const { describe, it, expect } = require('@jest/globals');
|
||||
const { evaluateJsExpression, internalExpressionCache: cache, createResponseParser } = require('../src/utils');
|
||||
const {
|
||||
evaluateJsExpression,
|
||||
internalExpressionCache: cache,
|
||||
createResponseParser,
|
||||
appendAwaitToTestFunc
|
||||
} = require('../src/utils');
|
||||
|
||||
describe('utils', () => {
|
||||
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"
|
||||
});
|
||||
});
|
||||
`);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user