mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-21 23:43:15 +01:00
feat: allow test to be asynchrone
This commit is contained in:
parent
c9ec6902a5
commit
314e8c17d3
4
.gitignore
vendored
4
.gitignore
vendored
@ -41,3 +41,7 @@ yarn-error.log*
|
|||||||
/test-results/
|
/test-results/
|
||||||
/playwright-report/
|
/playwright-report/
|
||||||
/playwright/.cache/
|
/playwright/.cache/
|
||||||
|
|
||||||
|
#dev editor
|
||||||
|
bruno.iml
|
||||||
|
.idea
|
@ -187,7 +187,7 @@ const runSingleRequest = async function (
|
|||||||
const testFile = get(bruJson, 'request.tests');
|
const testFile = get(bruJson, 'request.tests');
|
||||||
if (testFile && testFile.length) {
|
if (testFile && testFile.length) {
|
||||||
const testRuntime = new TestRuntime();
|
const testRuntime = new TestRuntime();
|
||||||
const result = testRuntime.runTests(
|
const result = await testRuntime.runTests(
|
||||||
testFile,
|
testFile,
|
||||||
request,
|
request,
|
||||||
response,
|
response,
|
||||||
@ -292,7 +292,7 @@ const runSingleRequest = async function (
|
|||||||
const testFile = get(bruJson, 'request.tests');
|
const testFile = get(bruJson, 'request.tests');
|
||||||
if (testFile && testFile.length) {
|
if (testFile && testFile.length) {
|
||||||
const testRuntime = new TestRuntime();
|
const testRuntime = new TestRuntime();
|
||||||
const result = testRuntime.runTests(
|
const result = await testRuntime.runTests(
|
||||||
testFile,
|
testFile,
|
||||||
request,
|
request,
|
||||||
err.response,
|
err.response,
|
||||||
|
@ -315,7 +315,7 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
||||||
if (testFile && testFile.length) {
|
if (testFile && testFile.length) {
|
||||||
const testRuntime = new TestRuntime();
|
const testRuntime = new TestRuntime();
|
||||||
const testResults = testRuntime.runTests(
|
const testResults = await testRuntime.runTests(
|
||||||
testFile,
|
testFile,
|
||||||
request,
|
request,
|
||||||
response,
|
response,
|
||||||
@ -389,7 +389,7 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
||||||
if (testFile && testFile.length) {
|
if (testFile && testFile.length) {
|
||||||
const testRuntime = new TestRuntime();
|
const testRuntime = new TestRuntime();
|
||||||
const testResults = testRuntime.runTests(
|
const testResults = await testRuntime.runTests(
|
||||||
testFile,
|
testFile,
|
||||||
request,
|
request,
|
||||||
error.response,
|
error.response,
|
||||||
@ -725,7 +725,7 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
||||||
if (testFile && testFile.length) {
|
if (testFile && testFile.length) {
|
||||||
const testRuntime = new TestRuntime();
|
const testRuntime = new TestRuntime();
|
||||||
const testResults = testRuntime.runTests(
|
const testResults = await testRuntime.runTests(
|
||||||
testFile,
|
testFile,
|
||||||
request,
|
request,
|
||||||
response,
|
response,
|
||||||
@ -804,7 +804,7 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
||||||
if (testFile && testFile.length) {
|
if (testFile && testFile.length) {
|
||||||
const testRuntime = new TestRuntime();
|
const testRuntime = new TestRuntime();
|
||||||
const testResults = testRuntime.runTests(
|
const testResults = await testRuntime.runTests(
|
||||||
testFile,
|
testFile,
|
||||||
request,
|
request,
|
||||||
error.response,
|
error.response,
|
||||||
|
@ -10,6 +10,7 @@ const { cleanJson } = require('../utils');
|
|||||||
|
|
||||||
// Inbuilt Library Support
|
// Inbuilt Library Support
|
||||||
const atob = require('atob');
|
const atob = require('atob');
|
||||||
|
const axios = require('axios');
|
||||||
const btoa = require('btoa');
|
const btoa = require('btoa');
|
||||||
const lodash = require('lodash');
|
const lodash = require('lodash');
|
||||||
const moment = require('moment');
|
const moment = require('moment');
|
||||||
@ -20,7 +21,7 @@ const CryptoJS = require('crypto-js');
|
|||||||
class TestRuntime {
|
class TestRuntime {
|
||||||
constructor() {}
|
constructor() {}
|
||||||
|
|
||||||
runTests(
|
async runTests(
|
||||||
testsFile,
|
testsFile,
|
||||||
request,
|
request,
|
||||||
response,
|
response,
|
||||||
@ -78,6 +79,7 @@ class TestRuntime {
|
|||||||
root: [collectionPath],
|
root: [collectionPath],
|
||||||
mock: {
|
mock: {
|
||||||
atob,
|
atob,
|
||||||
|
axios,
|
||||||
btoa,
|
btoa,
|
||||||
lodash,
|
lodash,
|
||||||
moment,
|
moment,
|
||||||
@ -89,7 +91,8 @@ class TestRuntime {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
vm.run(testsFile, path.join(collectionPath, 'vm.js'));
|
const asyncVM = vm.run(`module.exports = async () => { ${testsFile}}`, path.join(collectionPath, 'vm.js'));
|
||||||
|
await asyncVM();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
request,
|
request,
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
const Test = (__brunoTestResults, chai) => (description, callback) => {
|
const Test = (__brunoTestResults, chai) => async (description, callback) => {
|
||||||
try {
|
try {
|
||||||
callback();
|
await callback();
|
||||||
__brunoTestResults.addResult({ description, status: 'pass' });
|
__brunoTestResults.addResult({ description, status: 'pass' });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(chai.AssertionError);
|
console.log(chai.AssertionError);
|
||||||
|
53
packages/bruno-js/tests/runtime.spec.js
Normal file
53
packages/bruno-js/tests/runtime.spec.js
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
const { describe, it, expect } = require('@jest/globals');
|
||||||
|
const TestRuntime = require('../src/runtime/test-runtime');
|
||||||
|
|
||||||
|
describe('runtime', () => {
|
||||||
|
describe('test-runtime', () => {
|
||||||
|
const baseRequest = {
|
||||||
|
method: 'GET',
|
||||||
|
url: 'http://localhost:3000/',
|
||||||
|
headers: {},
|
||||||
|
data: undefined
|
||||||
|
};
|
||||||
|
const baseResponse = {
|
||||||
|
status: 200,
|
||||||
|
statusText: 'OK',
|
||||||
|
data: [
|
||||||
|
{
|
||||||
|
id: 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 3
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
it('should wait async tests', async () => {
|
||||||
|
const testFile = `
|
||||||
|
await test('async test', ()=> {
|
||||||
|
return new Promise((resolve)=> {
|
||||||
|
setTimeout(()=> {resolve()},200)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
`;
|
||||||
|
|
||||||
|
const runtime = new TestRuntime();
|
||||||
|
const result = await runtime.runTests(
|
||||||
|
testFile,
|
||||||
|
{ ...baseRequest },
|
||||||
|
{ ...baseResponse },
|
||||||
|
{},
|
||||||
|
{},
|
||||||
|
'.',
|
||||||
|
null,
|
||||||
|
process.env
|
||||||
|
);
|
||||||
|
expect(result.results.map((el) => ({ description: el.description, status: el.status }))).toEqual([
|
||||||
|
{ description: 'async test', status: 'pass' }
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user