UNPKG

@edge-runtime/jest-expect

Version:

Custom matchers for Jest's expect to help test Request/Response instances

42 lines 1.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function assertResponse(actual) { if (actual instanceof Response) return; throw new Error('Expected a Response instance'); } const HTTP_STATUS_CODE_RANGES = { '1': 'Informational', '2': 'Successful', '3': 'Redirection', '4': 'Client Error', '5': 'Server Error', }; expect.extend({ toHaveStatus(actual, ...args) { assertResponse(actual); const [status] = args; if (typeof status === 'string') { const httpStatusCodeRange = actual.status.toString()[0]; const matchedRange = HTTP_STATUS_CODE_RANGES[httpStatusCodeRange]; const statusRange = Object.keys(httpStatusCodeRange).find((k) => httpStatusCodeRange[k] === status); const pass = matchedRange === status; return { message: () => `expected ${actual.status} (${matchedRange})${pass ? ' not' : ''} to be in status range (${statusRange}xx)`, pass, }; } const pass = actual.status === status; if (pass) { return { message: () => `expected ${actual.status} not to be ${status}`, pass, }; } return { message: () => `expected ${actual.status} to be ${status}`, pass, }; }, }); //# sourceMappingURL=response.js.map