feat: allow test to be asynchrone

This commit is contained in:
Lesage Yann 2023-09-29 08:55:26 +02:00
parent c9ec6902a5
commit 314e8c17d3
6 changed files with 70 additions and 10 deletions

4
.gitignore vendored
View File

@ -41,3 +41,7 @@ yarn-error.log*
/test-results/ /test-results/
/playwright-report/ /playwright-report/
/playwright/.cache/ /playwright/.cache/
#dev editor
bruno.iml
.idea

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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);

View 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' }
]);
});
});
});