Merge pull request #1618 from trusta/feat/set-up-ajv-formats-in-scripts

feat(#947): set up ajv-formats in script and test runtimes
This commit is contained in:
Anoop M D 2024-02-21 20:08:57 +05:30 committed by GitHub
commit 09c496e516
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 134 additions and 2 deletions

1
package-lock.json generated
View File

@ -20986,6 +20986,7 @@
"dependencies": {
"@usebruno/query": "0.1.0",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
"atob": "^2.1.2",
"axios": "^1.5.1",
"btoa": "^1.2.1",

View File

@ -16,6 +16,7 @@
"dependencies": {
"@usebruno/query": "0.1.0",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
"atob": "^2.1.2",
"axios": "^1.5.1",
"btoa": "^1.2.1",
@ -28,7 +29,7 @@
"moment": "^2.29.4",
"nanoid": "3.3.4",
"node-fetch": "2.*",
"uuid": "^9.0.0",
"node-vault": "^0.10.2"
"node-vault": "^0.10.2",
"uuid": "^9.0.0"
}
}

View File

@ -16,6 +16,7 @@ const { cleanJson } = require('../utils');
// Inbuilt Library Support
const ajv = require('ajv');
const addFormats = require('ajv-formats');
const atob = require('atob');
const btoa = require('btoa');
const lodash = require('lodash');
@ -102,6 +103,7 @@ class ScriptRuntime {
zlib,
// 3rd party libs
ajv,
'ajv-formats': addFormats,
atob,
btoa,
lodash,
@ -194,6 +196,7 @@ class ScriptRuntime {
zlib,
// 3rd party libs
ajv,
'ajv-formats': addFormats,
atob,
btoa,
lodash,

View File

@ -19,6 +19,7 @@ const { cleanJson } = require('../utils');
// Inbuilt Library Support
const ajv = require('ajv');
const addFormats = require('ajv-formats');
const atob = require('atob');
const btoa = require('btoa');
const lodash = require('lodash');
@ -120,6 +121,7 @@ class TestRuntime {
zlib,
// 3rd party libs
ajv,
'ajv-formats': addFormats,
btoa,
atob,
lodash,

View File

@ -1,5 +1,6 @@
const { describe, it, expect } = require('@jest/globals');
const TestRuntime = require('../src/runtime/test-runtime');
const ScriptRuntime = require('../src/runtime/script-runtime');
describe('runtime', () => {
describe('test-runtime', () => {
@ -49,5 +50,129 @@ describe('runtime', () => {
{ description: 'async test', status: 'pass' }
]);
});
it('should have ajv and ajv-formats dependencies available', async () => {
const testFile = `
const Ajv = require('ajv');
const addFormats = require("ajv-formats");
const ajv = new Ajv();
addFormats(ajv);
const schema = {
type: 'string',
format: 'date-time'
};
const validate = ajv.compile(schema)
test('format valid', () => {
const valid = validate(new Date().toISOString())
expect(valid).to.be.true;
})
`;
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: 'format valid', status: 'pass' }
]);
});
});
describe('script-runtime', () => {
describe('run-request-script', () => {
const baseRequest = {
method: 'GET',
url: 'http://localhost:3000/',
headers: {},
data: undefined
};
it('should have ajv and ajv-formats dependencies available', async () => {
const script = `
const Ajv = require('ajv');
const addFormats = require("ajv-formats");
const ajv = new Ajv();
addFormats(ajv);
const schema = {
type: 'string',
format: 'date-time'
};
const validate = ajv.compile(schema)
bru.setVar('validation', validate(new Date().toISOString()))
`;
const runtime = new ScriptRuntime();
const result = await runtime.runRequestScript(script, { ...baseRequest }, {}, {}, '.', null, process.env);
expect(result.collectionVariables.validation).toBeTruthy();
});
});
describe('run-response-script', () => {
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 have ajv and ajv-formats dependencies available', async () => {
const script = `
const Ajv = require('ajv');
const addFormats = require("ajv-formats");
const ajv = new Ajv();
addFormats(ajv);
const schema = {
type: 'string',
format: 'date-time'
};
const validate = ajv.compile(schema)
bru.setVar('validation', validate(new Date().toISOString()))
`;
const runtime = new ScriptRuntime();
const result = await runtime.runResponseScript(
script,
{ ...baseRequest },
{ ...baseResponse },
{},
{},
'.',
null,
process.env
);
expect(result.collectionVariables.validation).toBeTruthy();
});
});
});
});