diff --git a/__config__/jest.setup-pacts-serlo-org-database-layer.ts b/__config__/jest.setup-pacts-serlo-org-database-layer.ts index 2d90122be..dc50686d1 100644 --- a/__config__/jest.setup-pacts-serlo-org-database-layer.ts +++ b/__config__/jest.setup-pacts-serlo-org-database-layer.ts @@ -1,5 +1,5 @@ import { Pact } from '@pact-foundation/pact' -import { http } from 'msw' +import { bypass, http, passthrough } from 'msw' import path from 'path' import { @@ -26,7 +26,7 @@ global.pact = new Pact({ }) beforeAll(async () => { - await createBeforeAll({ onUnhandledRequest: 'bypass' }) + createBeforeAll() await global.pact.setup() }) @@ -37,7 +37,7 @@ beforeEach(async () => { new RegExp(process.env.SERLO_ORG_DATABASE_LAYER_HOST.replace('.', '\\.')), async ({ request }) => { const url = new URL(request.url) - return fetch(`http://127.0.0.1:${port}${url.pathname}`, request) + return fetch(bypass(`http://127.0.0.1:${port}${url.pathname}`, request)) }, ), ) @@ -54,7 +54,7 @@ afterEach(async () => { try { await global.pact.verify() } finally { - createAfterEach() + await createAfterEach() } }) @@ -62,7 +62,7 @@ afterAll(async () => { try { await global.pact.finalize() } finally { - await createAfterAll() + createAfterAll() } }) diff --git a/__config__/jest.setup.ts b/__config__/jest.setup.ts index a9100478b..39e2e722b 100644 --- a/__config__/jest.setup.ts +++ b/__config__/jest.setup.ts @@ -25,24 +25,7 @@ jest.mock('@google-cloud/storage', () => { } }) -beforeAll(() => { - createBeforeAll({ - onUnhandledRequest(req) { - if ( - req.method === 'POST' && - req.url.includes(process.env.SERLO_ORG_DATABASE_LAYER_HOST) - ) { - console.error('Found an unhandled request for message %s', req.text()) - } else { - console.error( - 'Found an unhandled %s request to %s', - req.method, - req.url, - ) - } - }, - }) -}) +beforeAll(createBeforeAll) beforeEach(createBeforeEach) diff --git a/__config__/setup.ts b/__config__/setup.ts index 02bd70aea..434718bba 100644 --- a/__config__/setup.ts +++ b/__config__/setup.ts @@ -1,7 +1,7 @@ import { flush as flushSentry } from '@sentry/node' import crypto from 'crypto' import { http, HttpResponse } from 'msw' -import { SetupServer, setupServer } from 'msw/node' +import { setupServer } from 'msw/node' import { defaultSpreadsheetApi, @@ -35,7 +35,7 @@ export class MockTimer implements Timer { } } -export function createBeforeAll(options: Parameters[0]) { +export function createBeforeAll() { initializeSentry({ dsn: 'https://public@127.0.0.1/0', environment: 'testing', @@ -50,7 +50,18 @@ export function createBeforeAll(options: Parameters[0]) { global.timer = timer global.kratos = kratos - global.server.listen(options) + global.server.listen({ + async onUnhandledRequest(req) { + // eslint-disable-next-line no-console + console.error( + 'Found an unhandled %s request to %s with body %s', + req.method, + req.url, + await req.text(), + ) + return 'error' + }, + }) } export async function createBeforeEach() { diff --git a/__tests__/internals/kratos-middleware.ts b/__tests__/internals/kratos-middleware.ts index acbc0bc78..2396746fe 100644 --- a/__tests__/internals/kratos-middleware.ts +++ b/__tests__/internals/kratos-middleware.ts @@ -1,5 +1,6 @@ import express, { Express } from 'express' import type { Server } from 'http' +import { bypass } from 'msw' import { given } from '../__utils__' import { Kratos } from '~/internals/authentication' @@ -135,28 +136,30 @@ function fetchKratosRegister({ withKratosKey?: boolean body?: unknown }) { - return fetch(`http://localhost:${port}/kratos/register`, { - method: 'POST', - headers: { - 'x-msw-bypass': 'true', - 'content-type': `application/json`, - ...(withKratosKey - ? { 'x-kratos-key': process.env.SERVER_KRATOS_SECRET } - : {}), - }, - ...(body != null ? { body: JSON.stringify(body) } : {}), - }) + return fetch( + bypass(`http://localhost:${port}/kratos/register`, { + method: 'POST', + headers: { + 'content-type': `application/json`, + ...(withKratosKey + ? { 'x-kratos-key': process.env.SERVER_KRATOS_SECRET } + : {}), + }, + ...(body != null ? { body: JSON.stringify(body) } : {}), + }), + ) } function fetchKratosSingleLogout(body?: string | undefined) { - return fetch(`http://localhost:${port}/kratos/single-logout`, { - method: 'POST', - headers: { - 'x-msw-bypass': 'true', - 'content-type': `application/x-www-form-urlencoded`, - }, - ...(body != null ? { body } : {}), - }) + return fetch( + bypass(`http://localhost:${port}/kratos/single-logout`, { + method: 'POST', + headers: { + 'content-type': `application/x-www-form-urlencoded`, + }, + ...(body != null ? { body } : {}), + }), + ) } function createKratosMiddlewareBeforeEach(done: jest.DoneCallback) {