Files
bruno/packages/bruno-js/tests/runtime.spec.js
sanish chirayath 87f74262bb feat(variables): persist scripted variable changes by default + re-enable disabled scripting APIs (#8315)
* feat(variables): add variable persistence with scripting

feat(collections): implement script-driven update for collection variables, ensuring direct root modification and draft synchronization

feat(collections): enhance script variable management with baseline tracking and draft preservation

* feat(variables): add runtime variable updates and optimize disk writes by implementing dirty flags

fix(collections): handle errors during environment persistence in script execution

feat(collections): implement baseline clearing for script execution and optimize variable update handling

feat(tests): add default persistence tests for environment variables and update runtime variable handling

refactor(collections): streamline variable update handling and improve draft management by removing redundant comments and optimizing code clarity

test(collection-vars): add verification for draft edits and script variable visibility in collection settings UI

refactor(collection-vars): update header value selection logic for improved clarity and accuracy in draft isolation tests

* feat(global-environments): enhance global environment updates to resolve stale active UIDs and improve persistence logic

- Updated the `updateGlobalEnvironments` reducer to handle stale active UIDs by matching against environment names.
- Improved the logic for setting global environments and active UIDs to ensure consistency after disk reloads.
- Removed outdated tests related to persisted values in favor of more relevant assertions for environment variable handling.

* feat(variables): enhance typed value handling and persistence in global and collection environments

- Added tests to infer data types (number, boolean, object) when setting environment and collection variables.
- Updated the logic to preserve existing data types when variables are not modified by scripts.
- Implemented dirty flags to track changes in typed variables, ensuring accurate persistence across sessions.
- Refactored related tests to verify the correct behavior of typed variables in various scenarios.

* refactor(variables): streamline data type inference and enhance deletion methods

- Removed redundant data type inference logic from global and collection variable updates to simplify the codebase.
- Updated deletion methods in the Bru class to use Object.keys for improved resilience against user-defined properties.
- Added tests to ensure deletion methods function correctly even when properties are shadowed.
- Enhanced clarity in draft merge tests by standardizing keyboard shortcuts for selecting all text.

* fix(tests): correct variable naming and improve environment panel interactions

- Updated test cases to reflect the correct variable name 'wasSaved' instead of 'was-saved'.
- Modified environment panel interaction to remove forced click, enhancing test reliability.
- Added a utility function to close the environment panel in safe mode tests for better readability and maintainability.

* feat(runtime): enhance variable management and cleanup logic

- Introduced a new method to clear script-driven variable baselines for collections, ensuring no stale data leaks into new requests.
- Updated the handling of runtime variables in the Bru class to track changes with a new dirty flag, improving state management.
- Refactored the application of script environment variables to prevent direct mutations, ensuring immutability and cleaner state updates.
- Enhanced the response handling in the script runtime to conditionally include runtime variables based on their dirty state.

* feat(variables): improve request handling and state management for collections and environments

- Enhanced event listeners to clear global environment baselines on both 'testrun-started' and 'request-queued' events, preventing stale data issues.
- Updated global environment and collection variable update events to ignore stale updates from superseded requests, ensuring accurate state management.
- Refactored the Bru class to optimize variable management, including checks for existing keys before updates and deletions, improving performance and reliability.
- Introduced request UID tracking to maintain consistency across variable updates during concurrent requests.

* refactor(collections): update action to clear script variable baselines

- Replaced the dispatch of `_clearScriptGlobalEnvBaseline` with `clearScriptVariableBaselines` to improve clarity and maintainability in the Redux action handling for collections.

* feat(environments): introduce getScriptModifiedKeys utility for improved variable management

- Added a new utility function, `getScriptModifiedKeys`, to identify keys modified by scripts relative to a baseline, enhancing the handling of data types during variable updates.
- Updated the application of script environment variables to prevent overwriting user-defined draft changes during no-op writes.
- Refactored related logic in collections and global environments to utilize the new utility, ensuring accurate state management and improved clarity in the Redux slices.

* refactor(global-environments): simplify active UID resolution logic in updateGlobalEnvironments reducer

- Streamlined the logic for resolving the active global environment UID by consolidating conditions into a more concise format.
- Removed outdated comments to enhance code clarity and maintainability.
- Updated tests to ensure accurate resolution of active UIDs based on incoming environment data.

* refactor(tests): remove outdated comments and streamline environment variable row expectations

- Eliminated comments related to state sync and inference issues to enhance code clarity.
- Adjusted expectations for environment variable row rendering in tests, focusing on relevant assertions.

* feat(tests): add comprehensive tests for secret variable persistence in environments

- Introduced new test cases to validate the preservation of secret variables when updated via scripts in both collection and global environments.
- Implemented tests to ensure that secret values are encrypted before storage and can be correctly decrypted for subsequent requests.
- Added fixtures and environment configurations for testing secret variable behavior in both bru and yml formats.
- Enhanced utility functions for managing environment configurations and interactions within the test suite.

* feat(tests): enhance environment variable tests and add global variable persistence

- Updated MultiLineEditor and SingleLineEditor components to include data-testid for secret reveal toggle buttons, improving testability.
- Introduced new tests for global environment variable persistence, ensuring non-secret variables survive app restarts and are correctly interpolated.
- Added fixtures for workspace and collections to support the new global variable tests, enhancing the overall test coverage for environment management.
- Refactored utility functions to streamline interactions with environment variables in tests.

* refactor(collections): optimize environment and collection saving logic

- Simplified the persistence logic for active environments by directly constructing the environment copy, reducing unnecessary cloning.
- Updated the collection saving process to utilize the fresh collection state, ensuring accurate data is saved without drafts.
- Enhanced error handling during the save operations to improve reliability and maintainability.

* feat(tests): implement collection variable persistence tests

- Added multiple test cases to validate the persistence of collection variables across app restarts, including typed values and multiple variable settings.
- Created new fixtures for collection variables to support the tests, ensuring accurate simulation of variable management scenarios.
- Enhanced the existing collection management logic to ensure that variables are correctly set and deleted as per the test requirements.

* feat(tests): add tests for typed global environment variable persistence

- Introduced a new test suite to validate the persistence of typed global environment variables across app restarts, ensuring correct data types are maintained.
- Created a fixture for the test collection to simulate setting global variables with various data types, including number, boolean, object, and string.
- Enhanced the test logic to verify that the environment file reflects the correct state before and after application restarts.

* fix(tests): update request tab close interaction in variable persistence tests

* fix(tests): improve hover interaction for collection actions in runner tests

- Updated the hover logic for revealing collection actions to handle sidebar re-renders more reliably.
- Replaced one-shot hover with a polling mechanism to ensure visibility of actions, enhancing test stability.

* refactor(environments): streamline environment variable handling and remove ephemeral metadata logic

- Simplified the comparison logic for environment variables by removing unnecessary ephemeral metadata handling.
- Updated the saving process to directly use the environment variables without stripping metadata, enhancing clarity and maintainability.
- Removed outdated comments and unused utility functions related to ephemeral variables, improving code cleanliness.

* fix(ipc): update persistActiveEnvironment to handle requestUid for stale updates

- Modified the persistActiveEnvironment function to accept a requestUid parameter, allowing for better management of stale updates.
- Enhanced the logic to prevent disk writes for superseded requests, improving data integrity during environment persistence.

* refactor(bru): remove unused envName variable in deleteAllEnvVars method

- Eliminated the envName variable from the deleteAllEnvVars method, simplifying the logic for deleting environment variables.
- Cleaned up the method by removing unnecessary checks related to the envName, enhancing code clarity and maintainability.

* fix(bru): prevent deletion of internal __name__ variable in deleteEnvVar method

- Added a check in the deleteEnvVar method to silently ignore attempts to delete the internal __name__ variable, preserving its integrity.
- Updated tests to verify that the __name__ variable remains unchanged when deleteEnvVar is called with this key.
- Enhanced runtime tests to ensure compatibility with QuickJS by confirming that environment variables set with persist options are handled correctly.

* feat(tests): add legacy support test for environment variable persistence

- Introduced a new test suite to validate that the legacy argument for setting environment variables with persistence is still functional in version 4.
- Created a fixture to simulate the legacy syntax, ensuring that the variable is correctly persisted on disk without errors.
- Enhanced integration testing to confirm that the legacy behavior aligns with the current implementation, maintaining backward compatibility.

* test(tests): enhance legacy environment variable persistence tests for safe and developer modes

- Updated the test suite for `bru.setEnvVar` to verify that the legacy persist flag is correctly handled in both safe and developer modes.
- Introduced a helper function to streamline the verification process and ensure consistent behavior across different execution contexts.
- Adjusted the test logic to reset the environment state between mode switches, maintaining test integrity.
- Improved hover interaction in multiple persistent variable tests to ensure reliable visibility of actions during execution.

* fix(EnvironmentVariablesTable): correct change detection logic for environment variables

- Updated the logic for determining changes in environment variables to compare active current and saved values instead of previously used variablesToSave and savedValues.
- This change ensures accurate detection of modifications before saving, improving user feedback when no changes are present.

* test(tests): enhance secret variable persistence tests for environment configurations

- Updated the test suites for `bru.setEnvVar` and `bru.setGlobalEnvVar` to include interactions with the secrets tab, ensuring visibility of secret variables during various states of the environment.
- Added checks to confirm that the eye toggle functionality correctly reveals the values of secret variables after setting and overwriting them.
- Improved test coverage for secret variable persistence, validating that the expected values are displayed in both collection and global environment contexts.
2026-06-26 23:01:37 +05:30

590 lines
20 KiB
JavaScript

const { describe, it, expect, beforeAll } = require('@jest/globals');
const TestRuntime = require('../src/runtime/test-runtime');
const ScriptRuntime = require('../src/runtime/script-runtime');
const AssertRuntime = require('../src/runtime/assert-runtime');
const Bru = require('../src/bru');
const VarsRuntime = require('../src/runtime/vars-runtime');
const { loader: quickJsLoader } = require('../src/sandbox/quickjs');
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({ runtime: 'nodevm' });
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' }
]);
});
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({ runtime: 'nodevm' });
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({ runtime: 'nodevm' });
const result = await runtime.runRequestScript(script, { ...baseRequest }, {}, {}, '.', null, process.env);
expect(result.runtimeVariables.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({ runtime: 'nodevm' });
const result = await runtime.runResponseScript(
script,
{ ...baseRequest },
{ ...baseResponse },
{},
{},
'.',
null,
process.env
);
expect(result.runtimeVariables.validation).toBeTruthy();
});
});
});
describe('environment variables from scripts', () => {
it('should allow any value type', async () => {
const script = `
bru.setEnvVar('str', 'hello');
bru.setEnvVar('number', 42);
bru.setEnvVar('boolean', true);
bru.setEnvVar('object', {key: 'value'});
bru.setEnvVar('array', [1, 2, 3]);
`;
const runtime = new ScriptRuntime({ runtime: 'nodevm' });
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', null, process.env);
expect(result.envVariables.str).toBe('hello');
expect(result.envVariables.number).toBe(42);
expect(result.envVariables.boolean).toBe(true);
expect(result.envVariables.object).toEqual({ key: 'value' });
expect(result.envVariables.array).toEqual([1, 2, 3]);
});
it('should preserve typed values through the QuickJS shim', async () => {
await quickJsLoader();
const script = `
bru.setEnvVar('num', 42);
bru.setEnvVar('bool', true);
bru.setEnvVar('obj', { key: 'value' });
bru.setCollectionVar('collNum', 7);
bru.setGlobalEnvVar('globalBool', false);
`;
const runtime = new ScriptRuntime({ runtime: 'quickjs' });
const onConsoleLog = () => {};
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', onConsoleLog, process.env);
expect(typeof result.envVariables.num).toBe('number');
expect(result.envVariables.num).toBe(42);
expect(typeof result.envVariables.bool).toBe('boolean');
expect(result.envVariables.bool).toBe(true);
expect(typeof result.envVariables.obj).toBe('object');
expect(result.envVariables.obj).toEqual({ key: 'value' });
expect(typeof result.collectionVariables.collNum).toBe('number');
expect(result.collectionVariables.collNum).toBe(7);
expect(typeof result.globalEnvironmentVariables.globalBool).toBe('boolean');
expect(result.globalEnvironmentVariables.globalBool).toBe(false);
});
it('should return null for scopes the script did not touch (dirty-flag gating)', async () => {
const script = `bru.setEnvVar('only_env', 'val');`;
const runtime = new ScriptRuntime({ runtime: 'nodevm' });
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', null, process.env);
expect(result.envVariables).not.toBeNull();
expect(result.envVariables.only_env).toBe('val');
expect(result.collectionVariables).toBeNull();
expect(result.globalEnvironmentVariables).toBeNull();
});
it('should return null for scopes the script did not touch — QuickJS parity', async () => {
await quickJsLoader();
const script = `bru.setEnvVar('only_env', 'val');`;
const runtime = new ScriptRuntime({ runtime: 'quickjs' });
const onConsoleLog = () => {};
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', onConsoleLog, process.env);
expect(result.envVariables).not.toBeNull();
expect(result.envVariables.only_env).toBe('val');
expect(result.collectionVariables).toBeNull();
expect(result.globalEnvironmentVariables).toBeNull();
});
it('should not include persistentEnvVariables in result', async () => {
const script = `bru.setEnvVar('key', 'val');`;
const runtime = new ScriptRuntime({ runtime: 'nodevm' });
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', null, process.env);
expect(result).not.toHaveProperty('persistentEnvVariables');
});
it('should include collectionVariables in result', async () => {
const script = `bru.setCollectionVar('myVar', 'myValue');`;
const runtime = new ScriptRuntime({ runtime: 'nodevm' });
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', null, process.env);
expect(result.collectionVariables).toBeDefined();
expect(result.collectionVariables.myVar).toBe('myValue');
});
it('should silently ignore old persist flag as extra argument', async () => {
const scriptTrue = `bru.setEnvVar('key1', 'val1', { persist: true });`;
const scriptFalse = `bru.setEnvVar('key2', 'val2', { persist: false });`;
const runtime = new ScriptRuntime({ runtime: 'nodevm' });
const result1 = await runtime.runRequestScript(scriptTrue, {}, {}, {}, '.', null, process.env);
expect(result1.envVariables.key1).toBe('val1');
const result2 = await runtime.runRequestScript(scriptFalse, {}, {}, {}, '.', null, process.env);
expect(result2.envVariables.key2).toBe('val2');
});
it('should silently ignore old persist flag as extra argument — QuickJS parity', async () => {
await quickJsLoader();
const scriptTrue = `bru.setEnvVar('key1', 'val1', { persist: true });`;
const scriptFalse = `bru.setEnvVar('key2', 'val2', { persist: false });`;
const runtime = new ScriptRuntime({ runtime: 'quickjs' });
const onConsoleLog = () => {};
const result1 = await runtime.runRequestScript(scriptTrue, {}, {}, {}, '.', onConsoleLog, process.env);
expect(result1.envVariables.key1).toBe('val1');
const result2 = await runtime.runRequestScript(scriptFalse, {}, {}, {}, '.', onConsoleLog, process.env);
expect(result2.envVariables.key2).toBe('val2');
});
});
describe('bru.setVar random variable', () => {
it('should be able to set random variables as values', async () => {
const script = `bru.setVar('title', '{{$randomFirstName}}')`;
const runtime = new ScriptRuntime({ runtime: 'nodevm' });
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', null, process.env);
expect(result.runtimeVariables.title).toBe('{{$randomFirstName}}');
});
});
describe('assert-runtime', () => {
beforeAll(async () => {
await quickJsLoader();
});
const baseRequest = {
method: 'GET',
url: 'http://localhost:3000/',
headers: {},
data: undefined
};
const makeResponse = (data) => ({
status: 200,
statusText: 'OK',
data,
headers: {}
});
const runAssertions = (assertions, response, runtime = 'nodevm', runtimeVariables = {}) => {
const assertRuntime = new AssertRuntime({ runtime });
return assertRuntime.runAssertions(assertions, { ...baseRequest }, response, {}, runtimeVariables, process.env);
};
// Ensures each QuickJS evaluation gets a fresh context
describe('quickjs context isolation across iterations', () => {
const ITERATION_COUNT = 350;
it('should return correct res.status on every iteration', () => {
for (let i = 0; i < ITERATION_COUNT; i++) {
const status = 200 + i;
const results = runAssertions(
[{ name: 'res.status', value: `eq ${status}`, enabled: true }],
{ status, statusText: 'OK', data: {}, headers: {} },
'quickjs'
);
expect(results[0].status).toBe('pass');
}
});
it('should return correct res.body values on every iteration', () => {
for (let i = 0; i < ITERATION_COUNT; i++) {
const results = runAssertions(
[{ name: 'res.body.id', value: `eq ${i}`, enabled: true }],
{ status: 200, statusText: 'OK', data: { id: i }, headers: {} },
'quickjs'
);
expect(results[0].status).toBe('pass');
}
});
it('should not return stale data from a previous iteration', () => {
// First call with status 200
runAssertions(
[{ name: 'res.status', value: 'eq 200', enabled: true }],
{ status: 200, statusText: 'OK', data: { token: 'bearer_abc' }, headers: { authorization: 'bearer xyz' } },
'quickjs'
);
// Second call with status 404 — must not return 200 or any data from previous call
const results = runAssertions(
[
{ name: 'res.status', value: 'eq 404', enabled: true },
{ name: 'res.body.error', value: 'eq not_found', enabled: true }
],
{ status: 404, statusText: 'Not Found', data: { error: 'not_found' }, headers: {} },
'quickjs'
);
expect(results[0].status).toBe('pass');
expect(results[1].status).toBe('pass');
});
it('should not persist runtime variables from a previous call', () => {
// First call with runtime variable token = "one"
const results1 = runAssertions(
[{ name: 'token', value: 'eq one', enabled: true }],
{ status: 200, statusText: 'OK', data: {}, headers: {} },
'quickjs',
{ token: 'one' }
);
expect(results1[0].status).toBe('pass');
// Second call without token
const results2 = runAssertions(
[{ name: 'token', value: 'eq one', enabled: true }],
{ status: 200, statusText: 'OK', data: {}, headers: {} },
'quickjs'
);
// Must fail — token should not exist in a fresh context
expect(results2[0].status).toBe('fail');
});
});
describe('isJson', () => {
it('should pass for a plain object', () => {
const results = runAssertions(
[{ name: 'res.body', value: 'isJson', enabled: true }],
makeResponse({ id: 1, name: 'test' })
);
expect(results[0].status).toBe('pass');
});
it('should pass for a nested object', () => {
const results = runAssertions(
[{ name: 'res.body', value: 'isJson', enabled: true }],
makeResponse({ user: { id: 1, tags: ['a', 'b'] } })
);
expect(results[0].status).toBe('pass');
});
it('should pass for objects from a different realm (e.g. after res.setBody in node-vm)', async () => {
const response = makeResponse({ id: 1, name: 'original' });
// res.setBody() inside node-vm creates a cross-realm object whose
// constructor is the VM's Object, not the host's Object
const scriptRuntime = new ScriptRuntime({ runtime: 'nodevm' });
await scriptRuntime.runResponseScript(
`res.setBody({ id: 2, name: 'updated' });`,
{ ...baseRequest },
response,
{}, {}, '.', null, process.env
);
const results = runAssertions(
[{ name: 'res.body', value: 'isJson', enabled: true }],
response
);
expect(results[0].status).toBe('pass');
});
it('should pass for an array', () => {
const results = runAssertions(
[{ name: 'res.body', value: 'isJson', enabled: true }],
makeResponse([1, 2, 3])
);
expect(results[0].status).toBe('pass');
});
it('should pass for an array of strings', () => {
const results = runAssertions(
[{ name: 'res.body', value: 'isJson', enabled: true }],
makeResponse(['A55001213ZX0A'])
);
expect(results[0].status).toBe('pass');
});
it('should pass for an empty array', () => {
const results = runAssertions(
[{ name: 'res.body', value: 'isJson', enabled: true }],
makeResponse([])
);
expect(results[0].status).toBe('pass');
});
it('should pass for an array of objects', () => {
const results = runAssertions(
[{ name: 'res.body', value: 'isJson', enabled: true }],
makeResponse([{ id: 1 }, { id: 2 }])
);
expect(results[0].status).toBe('pass');
});
it('should fail for a string', () => {
const results = runAssertions(
[{ name: 'res.body', value: 'isJson', enabled: true }],
makeResponse('hello')
);
expect(results[0].status).toBe('fail');
});
it('should fail for null', () => {
const results = runAssertions(
[{ name: 'res.body', value: 'isJson', enabled: true }],
makeResponse(null)
);
expect(results[0].status).toBe('fail');
});
});
describe('jsonSchema', () => {
const chai = require('chai');
it('should pass when body matches a valid schema', () => {
const body = { name: 'John', age: 30 };
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' }
},
required: ['name', 'age']
};
chai.expect(body).to.have.jsonSchema(schema);
});
it('should fail when body has a type mismatch', () => {
const body = { name: 'John', age: 'thirty' };
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' }
},
required: ['name', 'age']
};
expect(() => chai.expect(body).to.have.jsonSchema(schema)).toThrow(/validation errors/);
});
it('should fail when a required field is missing', () => {
const body = { name: 'John' };
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' }
},
required: ['name', 'age']
};
expect(() => chai.expect(body).to.have.jsonSchema(schema)).toThrow(/validation errors/);
});
it('should pass with custom ajvOptions', () => {
const body = { name: 'John', age: 30 };
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' }
},
required: ['name', 'age']
};
chai.expect(body).to.have.jsonSchema(schema, { allErrors: false });
});
it('should support negation with .not', () => {
const body = { name: 'John' };
const schema = { type: 'array' };
chai.expect(body).to.not.have.jsonSchema(schema);
});
it('should throw a clear error for unsupported Draft 2020-12 $schema', () => {
const body = { name: 'John' };
const schema = {
$schema: 'https://json-schema.org/draft/2020-12/schema',
type: 'object',
properties: { name: { type: 'string' } }
};
expect(() => chai.expect(body).to.have.jsonSchema(schema)).toThrow(/Unsupported JSON Schema version.*2020-12.*only supports Draft-07/);
});
it('should throw a clear error for unsupported Draft 2019-09 $schema', () => {
const body = { name: 'John' };
const schema = {
$schema: 'https://json-schema.org/draft/2019-09/schema',
type: 'object',
properties: { name: { type: 'string' } }
};
expect(() => chai.expect(body).to.have.jsonSchema(schema)).toThrow(/Unsupported JSON Schema version.*2019-09.*only supports Draft-07/);
});
it('should allow explicit Draft-07 $schema', () => {
const body = { name: 'John', age: 30 };
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' }
},
required: ['name', 'age']
};
chai.expect(body).to.have.jsonSchema(schema);
});
});
});
});