Skip to content

Commit

Permalink
feat: introduce multi clause to allow using jp.query instead of jp.value
Browse files Browse the repository at this point in the history
  • Loading branch information
Varun0157 committed Jun 25, 2024
1 parent 06f2a37 commit f451186
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { getStringIfNotScalar, isDict } from "./utils/typeUtils";
import { Tests, ResponseData, Assertion, SpecResult, TestResult } from "./models";
import { mergePrefixBasedTests } from "./mergeData";

const SKIP_CLAUSE = "$skip",
OPTIONS_CLAUSE = "$options",
MULTI_CLAUSE = "multi";

function hasFailure(res: SpecResult): boolean {
return res.results.some((r) => !r.pass) || res.subResults.some(hasFailure);
}
Expand All @@ -14,7 +18,7 @@ export function runAllTests(
responseData: ResponseData,
stopOnFailure: boolean,
rootSpec: string | null = null,
skip?: boolean,
skip?: boolean
): SpecResult {
const res: SpecResult = { spec: rootSpec, results: [], subResults: [] };
if (!tests) return res;
Expand All @@ -37,10 +41,14 @@ export function runAllTests(
}

for (const spec in tests.json) {
let expected = tests.json[spec],
received;
const expected = tests.json[spec];
let received;
try {
received = getValueForJSONTests(responseData.json, spec);
received = getValueForJSONTests(
responseData.json,
spec,
typeof expected === "object" && expected !== null && expected[MULTI_CLAUSE]
);
} catch (err: any) {
res.subResults.push({
spec,
Expand Down Expand Up @@ -78,9 +86,9 @@ function runTest(spec: string, expected: Assertion, received: any, skip?: boolea
return { spec, skipped: skip, results: [{ pass, expected, received, op: ":" }], subResults: [] };
}

function getValueForJSONTests(responseContent: object, key: string): any {
function getValueForJSONTests(responseContent: object, key: string, multi?: boolean): any {
try {
return jp.value(responseContent, key);
return multi ? jp.query(responseContent, key) : jp.value(responseContent, key);
} catch (err: any) {
throw new Error(`Error while evaluating JSONPath ${key}: ${err.description || err.message || err}`);
}
Expand All @@ -92,16 +100,13 @@ function getType(data: any): string {
return typeof data;
}

const SKIP_CLAUSE = "$skip",
OPTIONS_CLAUSE: string = "$options";

const tests: {
[name: string]: (
expectedObj: any,
receivedObj: any,
spec: string,
op: string,
options: { [key: string]: any },
options: { [key: string]: any }
) => SpecResult;
} = {
$eq: function (expectedObj, receivedObj, spec, op, options): SpecResult {
Expand Down Expand Up @@ -165,8 +170,8 @@ const tests: {
typeof receivedObj === "object" && receivedObj !== null
? Object.keys(receivedObj).length
: typeof receivedObj === "string" || Array.isArray(receivedObj)
? receivedObj.length
: undefined;
? receivedObj.length
: undefined;

const received: Exclude<any, object> = getStringIfNotScalar(receivedObj);
const expected: Exclude<any, object> = getStringIfNotScalar(expectedObj);
Expand Down Expand Up @@ -290,6 +295,13 @@ const tests: {
results: [],
};
},
[MULTI_CLAUSE]: function (expectedObj, receivedObj, spec, op, options): SpecResult {
return {
spec,
subResults: [],
results: [],
};
},
$tests: function (expectedObj, receivedObj, spec, op, options): SpecResult {
const res: SpecResult = { spec, results: [], subResults: [] };

Expand Down Expand Up @@ -325,7 +337,7 @@ export function runObjectTests(
opVals: { [key: string]: any },
receivedObject: any,
spec: string,
skip?: boolean,
skip?: boolean
): SpecResult {
const objRes: SpecResult = {
spec,
Expand Down

0 comments on commit f451186

Please sign in to comment.