diff --git a/src/mergeData.ts b/src/mergeData.ts index bdd614c..6b0f120 100644 --- a/src/mergeData.ts +++ b/src/mergeData.ts @@ -73,8 +73,9 @@ function getMergedOptions(cOptions: RawOptions = {}, rOptions: RawOptions = {}): const keepRawJSON = options.keepRawJSON == true; const showHeaders = options.showHeaders == true; const rawParams = options.rawParams == true; + const stopOnFailure = options.stopOnFailure == true; - return { follow, verifySSL, keepRawJSON, showHeaders, rawParams }; + return { follow, verifySSL, keepRawJSON, showHeaders, rawParams, stopOnFailure }; } function getMergedSetVars( diff --git a/src/models.ts b/src/models.ts index 9a075cd..c56a0d3 100644 --- a/src/models.ts +++ b/src/models.ts @@ -16,6 +16,7 @@ export interface Options { keepRawJSON: boolean; showHeaders: boolean; rawParams: boolean; + stopOnFailure: boolean; } export type Assertion = number | boolean | string | null | { [op: string]: any }; @@ -50,6 +51,7 @@ export interface RawOptions { keepRawJSON?: boolean; showHeaders?: boolean; rawParams?: boolean; + stopOnFailure?: boolean; } export interface RawTests { diff --git a/src/runTests.ts b/src/runTests.ts index e6d8c3f..5a92d13 100644 --- a/src/runTests.ts +++ b/src/runTests.ts @@ -4,16 +4,23 @@ import { getStringIfNotScalar } from "./utils/typeUtils"; import { Tests, ResponseData, TestResult, Assertion } from "./models"; -export function runAllTests(tests: Tests, responseData: ResponseData): TestResult[] { +export function runAllTests( + tests: Tests, + responseData: ResponseData, + stopOnFailure: boolean, +): TestResult[] { const results: TestResult[] = []; if (!tests) return results; - for (const spec in tests.json) { - const expected = tests.json[spec]; - const received = getValueForJSONTests(responseData.json, spec); - const jsonResults = runTest(spec, expected, received); - results.push(...jsonResults); + let statusFail = false; + if (tests.status) { + const expected = tests.status; + const received = responseData.status; + const statusResults = runTest("status", expected, received); + results.push(...statusResults); + statusFail = statusResults.some((r) => !r.pass); } + if (stopOnFailure && statusFail) return results; for (const spec in tests.headers) { const expected = tests.headers[spec]; @@ -22,6 +29,13 @@ export function runAllTests(tests: Tests, responseData: ResponseData): TestResul results.push(...headerResults); } + for (const spec in tests.json) { + const expected = tests.json[spec]; + const received = getValueForJSONTests(responseData.json, spec); + const jsonResults = runTest(spec, expected, received); + results.push(...jsonResults); + } + if (tests.body) { const expected = tests.body; const received = responseData.body; @@ -29,13 +43,6 @@ export function runAllTests(tests: Tests, responseData: ResponseData): TestResul results.push(...bodyResults); } - if (tests.status) { - const expected = tests.status; - const received = responseData.status; - const statusResults = runTest("status", expected, received); - results.push(...statusResults); - } - return results; }