Tests: boot Payload without type generation, guard against the leak

Outside production getPayload() spawns a detached generate:types child.
With no PAYLOAD_CONFIG_PATH it never finds a config (tsconfig rootDir
'./' sends Payload's findUpSync into an endless loop), so every full
vitest run left six processes spinning at ~40% CPU.

- test/helpers.ts: buildTestConfig() = buildConfig with
  typescript.autoGenerate false
- the six tests that boot Payload use it
- test/testConfig.test.ts: fails if a test calls buildConfig directly

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-10-27 14:59:41 +01:00
co-authored by Claude Opus 5.5
parent e53d09b720
commit 53a8920564
8 changed files with 56 additions and 14 deletions
+3 -2
View File
@@ -4,13 +4,14 @@ import { sqliteAdapter } from '@payloadcms/db-sqlite'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import os from 'node:os'
import path from 'node:path'
import { buildConfig, getPayload } from 'payload'
import { getPayload } from 'payload'
import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
import { constructionNoticeHref, isPathAllowed, isPathVisibleIn, normalizePath } from '../src/modules/construction/logic.js'
import { createContentHelpers } from '../src/modules/frontend/createContentHelpers.js'
import { createPageRoute } from '../src/modules/frontend/pageRoute.js'
import { ipalKit } from '../src/plugin.js'
import { buildTestConfig } from './helpers.js'
vi.mock('server-only', () => ({}))
@@ -36,7 +37,7 @@ describe('under construction — Site Settings → createPageRoute', () => {
const i18n = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }, { code: 'en', label: 'English' }] } as const
beforeAll(async () => {
configPromise = buildConfig({
configPromise = buildTestConfig({
collections: [
{ slug: 'users', auth: true, fields: [] },
{ slug: 'pages', fields: [{ name: 'title', type: 'text' }, { name: 'slug', type: 'text', index: true, localized: true }] },
+3 -2
View File
@@ -5,17 +5,18 @@ import { sqliteAdapter } from '@payloadcms/db-sqlite'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import os from 'os'
import path from 'path'
import { buildConfig, getPayload } from 'payload'
import { getPayload } from 'payload'
import { afterAll, beforeAll, describe, expect, test } from 'vitest'
import { resolveRoute } from '../src/modules/content/resolveRoute.js'
import { buildNoindexField } from '../src/modules/seo/buildNoindexField.js'
import { ipalKit } from '../src/plugin.js'
import { buildTestConfig } from './helpers.js'
let payload: Payload
beforeAll(async () => {
const config = buildConfig({
const config = buildTestConfig({
collections: [
{
slug: 'pages',
+12 -1
View File
@@ -1,7 +1,18 @@
import type { BasePayload } from 'payload'
import type { BasePayload, Config } from 'payload'
import { buildConfig } from 'payload'
import { vi } from 'vitest'
/**
* The only way a test builds a real Payload config. Outside production
* getPayload() spawns a detached `payload generate:types` child; with no
* PAYLOAD_CONFIG_PATH it never finds a config (tsconfig rootDir "./" makes
* Payload's findUpSync loop forever) and is left spinning after vitest exits.
* test/testConfig.test.ts fails when a test calls buildConfig directly.
*/
export const buildTestConfig = (config: Config) =>
buildConfig({ ...config, typescript: { ...config.typescript, autoGenerate: false } })
type FakeOptions = {
forms?: Record<string, { fields: Record<string, unknown>[] }>
integrations?: Record<string, unknown>
+3 -2
View File
@@ -9,7 +9,7 @@ import { sqliteAdapter } from '@payloadcms/db-sqlite'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import os from 'os'
import path from 'path'
import { buildConfig, getPayload } from 'payload'
import { getPayload } from 'payload'
import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
import { buildConsentLogEndpoint } from '../src/modules/consent/consentLog.js'
@@ -18,6 +18,7 @@ import { submitForm } from '../src/modules/forms/submitForm.js'
import { createContentHelpers } from '../src/modules/frontend/createContentHelpers.js'
import { createPageRoute } from '../src/modules/frontend/pageRoute.js'
import { ipalKit } from '../src/plugin.js'
import { buildTestConfig } from './helpers.js'
vi.mock('server-only', () => ({}))
@@ -26,7 +27,7 @@ let configPromise: Parameters<typeof getPayload>[0]['config']
let formId: string
beforeAll(async () => {
const config = buildConfig({
const config = buildTestConfig({
collections: [
// Open sign-up, the risky setup the roles field guard exists for.
{ slug: 'users', access: { create: () => true }, auth: true, fields: [] },
+3 -2
View File
@@ -4,11 +4,12 @@ import { sqliteAdapter } from '@payloadcms/db-sqlite'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import os from 'node:os'
import path from 'node:path'
import { buildConfig, getPayload } from 'payload'
import { getPayload } from 'payload'
import { afterAll, afterEach, beforeAll, describe, expect, test, vi } from 'vitest'
import { createSeeder, editedSinceSeed, isLocalDatabase, seedHash } from '../src/exports/seed.js'
import { ipalKit } from '../src/plugin.js'
import { buildTestConfig } from './helpers.js'
vi.mock('server-only', () => ({}))
@@ -75,7 +76,7 @@ describe('createSeeder', () => {
beforeAll(async () => {
vi.stubEnv('DATABASE_URI', 'file::memory:')
const config = buildConfig({
const config = buildTestConfig({
collections: [
{ slug: 'users', auth: true, fields: [] },
{
+3 -2
View File
@@ -4,11 +4,12 @@ import { sqliteAdapter } from '@payloadcms/db-sqlite'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import os from 'node:os'
import path from 'node:path'
import { buildConfig, getPayload } from 'payload'
import { getPayload } from 'payload'
import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
import { createContentHelpers } from '../src/modules/frontend/createContentHelpers.js'
import { ipalKit } from '../src/plugin.js'
import { buildTestConfig } from './helpers.js'
vi.mock('server-only', () => ({}))
@@ -25,7 +26,7 @@ describe('SEO outputs against a real database', () => {
const urls = async () => (await helpers().sitemap()).map((e) => e.url).sort()
beforeAll(async () => {
configPromise = buildConfig({
configPromise = buildTestConfig({
collections: [
{ slug: 'users', auth: true, fields: [] },
{
+4 -3
View File
@@ -5,19 +5,20 @@ import { sqliteAdapter } from '@payloadcms/db-sqlite'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import os from 'os'
import path from 'path'
import { buildConfig, getPayload } from 'payload'
import { getPayload } from 'payload'
import { afterAll, beforeAll, describe, expect, test } from 'vitest'
import { resolveRoute } from '../src/modules/content/resolveRoute.js'
import { createContentHelpers } from '../src/modules/frontend/createContentHelpers.js'
import { ipalKit } from '../src/plugin.js'
import { buildTestConfig } from './helpers.js'
let payload: Payload
let configPromise: ReturnType<typeof buildConfig>
let configPromise: ReturnType<typeof buildTestConfig>
let mediaId: number | string
beforeAll(async () => {
configPromise = buildConfig({
configPromise = buildTestConfig({
collections: [
{
slug: 'pages',
+25
View File
@@ -0,0 +1,25 @@
/**
* Guard: a test that boots Payload with a raw buildConfig leaks a spinning
* `payload generate:types` process per run (see buildTestConfig in helpers.ts).
*/
import { readdirSync, readFileSync } from 'node:fs'
import path from 'node:path'
import { describe, expect, test } from 'vitest'
import { buildTestConfig } from './helpers.js'
const dir = import.meta.dirname
describe('test configs never auto-generate types', () => {
test('buildTestConfig turns autoGenerate off', async () => {
const config = await buildTestConfig({ collections: [], secret: 'x', typescript: { outputFile: 'types.ts' } } as never)
expect(config.typescript).toMatchObject({ autoGenerate: false, outputFile: 'types.ts' })
})
test('no test calls buildConfig from payload directly', () => {
const offenders = readdirSync(dir)
.filter((f) => f.endsWith('.ts') && f !== 'helpers.ts' && f !== 'testConfig.test.ts')
.filter((f) => /\bbuildConfig\b/.test(readFileSync(path.join(dir, f), 'utf8').replace(/buildTestConfig/g, '')))
expect(offenders).toEqual([])
})
})