Skip to content

Commit

Permalink
Merge pull request #1172 from serlo/1171-fix-pact-tests
Browse files Browse the repository at this point in the history
test: Fix pact tests and simplify test config
  • Loading branch information
kulla authored Dec 1, 2023
2 parents c3f125e + 52b342a commit ccde1a5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 45 deletions.
10 changes: 5 additions & 5 deletions __config__/jest.setup-pacts-serlo-org-database-layer.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -26,7 +26,7 @@ global.pact = new Pact({
})

beforeAll(async () => {
await createBeforeAll({ onUnhandledRequest: 'bypass' })
createBeforeAll()
await global.pact.setup()
})

Expand All @@ -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))
},
),
)
Expand All @@ -54,15 +54,15 @@ afterEach(async () => {
try {
await global.pact.verify()
} finally {
createAfterEach()
await createAfterEach()
}
})

afterAll(async () => {
try {
await global.pact.finalize()
} finally {
await createAfterAll()
createAfterAll()
}
})

Expand Down
19 changes: 1 addition & 18 deletions __config__/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 14 additions & 3 deletions __config__/setup.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -35,7 +35,7 @@ export class MockTimer implements Timer {
}
}

export function createBeforeAll(options: Parameters<SetupServer['listen']>[0]) {
export function createBeforeAll() {
initializeSentry({
dsn: 'https://public@127.0.0.1/0',
environment: 'testing',
Expand All @@ -50,7 +50,18 @@ export function createBeforeAll(options: Parameters<SetupServer['listen']>[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() {
Expand Down
41 changes: 22 additions & 19 deletions __tests__/internals/kratos-middleware.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ccde1a5

Please sign in to comment.