UNPKG

@iamkenos/iris

Version:

Test API endpoints with Axios & Jest using a collection of custom matchers and built-in utility functions.

82 lines 5.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.thenResponseSchemaEquals = exports.thenResponseTimeIsGreaterThan = exports.thenResponseTimeIsLessThan = exports.thenResponseBodyIsEmpty = exports.thenResponseBodyPropEquals = exports.thenResponseBodyEquals = exports.thenResponseStatusEquals = exports.thenCookiesEquals = exports.thenCookiesExist = exports.thenPendingStep = exports.formatStepName = void 0; const _common_1 = require("../common/index"); const formatStepName = (functionName, preferred = true) => { return preferred ? _common_1.changecase.capitalCase(functionName) : `${_common_1.changecase.capitalCase(functionName)} (Not)`; }; exports.formatStepName = formatStepName; const thenPendingStep = (message) => { _common_1.Reporter.startStep(`${_common_1.changecase.capitalCase(exports.thenPendingStep.name)}: ${message}`); _common_1.Reporter.endStep("Pending"); }; exports.thenPendingStep = thenPendingStep; const thenCookiesExist = (response, preferred = true) => { _common_1.Reporter.startStep((0, exports.formatStepName)(exports.thenCookiesExist.name, preferred)); const cookies = response.headers["set-cookie"]; const then = preferred ? expect(cookies).not : expect(cookies); then.toEqual(undefined); _common_1.Reporter.endStep(); }; exports.thenCookiesExist = thenCookiesExist; const thenCookiesEquals = (response, expected, preferred = true) => { _common_1.Reporter.startStep(`${(0, exports.formatStepName)(exports.thenCookiesEquals.name, preferred)}: ${expected}`); const actual = response.headers["set-cookie"]; const then = preferred ? expect(expected) : expect(expected).not; then.toEqual(actual); _common_1.Reporter.endStep(); }; exports.thenCookiesEquals = thenCookiesEquals; const thenResponseStatusEquals = (response, expected = 200, preferred = true) => { const then = preferred ? expect(response.status) : expect(response.status).not; _common_1.Reporter.startStep(`${(0, exports.formatStepName)(exports.thenResponseStatusEquals.name, preferred)}: ${expected}`); then.toEqual(expected); _common_1.Reporter.endStep(); }; exports.thenResponseStatusEquals = thenResponseStatusEquals; const thenResponseBodyEquals = (response, expected, preferred = true) => { const then = preferred ? expect(response.body) : expect(response.body).not; _common_1.Reporter.startStep((0, exports.formatStepName)(exports.thenResponseBodyEquals.name, preferred)); _common_1.Reporter.addAttachment("Expected", JSON.stringify(expected, null, 2), _common_1.MimeType.APP_JSON); then.toEqual(expected); _common_1.Reporter.endStep(); }; exports.thenResponseBodyEquals = thenResponseBodyEquals; const thenResponseBodyPropEquals = (response, prop, expected, preferred = true) => { const nested = prop.split(".").reduce((prev, cur) => prev[cur], response.body); const then = preferred ? expect(nested) : expect(nested).not; _common_1.Reporter.startStep(`${(0, exports.formatStepName)(exports.thenResponseBodyPropEquals.name, preferred)}: ${prop}`); _common_1.Reporter.addAttachment("Expected", _common_1.isJSON ? JSON.stringify(expected, null, 2) : expected, _common_1.MimeType.APP_JSON); then.toEqual(expected); _common_1.Reporter.endStep(); }; exports.thenResponseBodyPropEquals = thenResponseBodyPropEquals; const thenResponseBodyIsEmpty = (response, preferred = true) => { const then = preferred ? expect(response.body) : expect(response.body).not; _common_1.Reporter.startStep((0, exports.formatStepName)(exports.thenResponseBodyIsEmpty.name, preferred)); then.toEqual(""); _common_1.Reporter.endStep(); }; exports.thenResponseBodyIsEmpty = thenResponseBodyIsEmpty; const thenResponseTimeIsLessThan = (response, expected, preferred = true) => { const then = preferred ? expect(response.time) : expect(response.time).not; _common_1.Reporter.startStep(`${(0, exports.formatStepName)(exports.thenResponseTimeIsLessThan.name, preferred)}: ${expected}`); then.toBeLessThan(expected); _common_1.Reporter.endStep(); }; exports.thenResponseTimeIsLessThan = thenResponseTimeIsLessThan; const thenResponseTimeIsGreaterThan = (response, expected, preferred = true) => { const then = preferred ? expect(response.time) : expect(response.time).not; _common_1.Reporter.startStep(`${(0, exports.formatStepName)(exports.thenResponseTimeIsGreaterThan.name, preferred)}: ${expected}`); then.toBeGreaterThan(expected); _common_1.Reporter.endStep(); }; exports.thenResponseTimeIsGreaterThan = thenResponseTimeIsGreaterThan; const thenResponseSchemaEquals = async (response, expected, preferred = true) => { const then = preferred ? expect(response.body) : expect(response.body).not; _common_1.Reporter.startStep(`${(0, exports.formatStepName)(exports.thenResponseSchemaEquals.name, preferred)}: ${expected}`); await then.toMatchJsonSchema(expected); _common_1.Reporter.endStep(); }; exports.thenResponseSchemaEquals = thenResponseSchemaEquals; //# sourceMappingURL=then.js.map