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

View File

@ -10,6 +10,7 @@ const { cleanJson } = require('../utils');
// Inbuilt Library Support
const atob = require('atob');
const axios = require('axios');
const btoa = require('btoa');
const lodash = require('lodash');
const moment = require('moment');
@ -20,7 +21,7 @@ const CryptoJS = require('crypto-js');
class TestRuntime {
constructor() {}
runTests(
async runTests(
testsFile,
request,
response,
@ -78,6 +79,7 @@ class TestRuntime {
root: [collectionPath],
mock: {
atob,
axios,
btoa,
lodash,
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 {
request,

View File

@ -1,6 +1,6 @@
const Test = (__brunoTestResults, chai) => (description, callback) => {
const Test = (__brunoTestResults, chai) => async (description, callback) => {
try {
callback();
await callback();
__brunoTestResults.addResult({ description, status: 'pass' });
} catch (error) {
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' }
]);
});
});
});