UNPKG

@edge-runtime/jest-expect

Version:

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

54 lines 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.assertRequestOrResponse = assertRequestOrResponse; function assertRequestOrResponse(actual) { if (actual instanceof Request || actual instanceof Response) return; throw new Error('Expected a Request or Response instance'); } expect.extend({ async toHaveJSONBody(actual, ...args) { assertRequestOrResponse(actual); const [body] = args; const contentType = actual.headers.get('Content-Type'); if (!(contentType === null || contentType === void 0 ? void 0 : contentType.includes('application/json'))) { const type = actual instanceof Request ? 'request' : 'response'; return { message: () => [ `${this.utils.matcherHint('toHaveJSONBody')}\n`, `Expected ${type} to have "Content-Type": ${this.utils.printExpected('application/json')}`, `Received: ${this.utils.printReceived(contentType)}`, ].join('\n'), pass: false, }; } const json = await actual.clone().json(); const pass = this.equals(json, body); if (pass) { return { message: () => `expected ${json} not to be ${body}`, pass: true, }; } return { message: () => `expected JSON body '${JSON.stringify(json)}' to be '${JSON.stringify(body)}'`, pass: false, }; }, async toHaveTextBody(actual, body) { assertRequestOrResponse(actual); const text = await actual.clone().text(); const pass = this.equals(text, body); if (pass) { return { message: () => `expected ${text} not to be ${body}`, pass: true, }; } return { message: () => `expected text body '${text}' to be '${body}'`, pass: false, }; }, }); //# sourceMappingURL=shared.js.map