From 53a892056495f3bfa0d77b023736e22cb781c596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Smoli=C5=84ski?= Date: Tue, 27 Oct 2026 14:59:41 +0100 Subject: [PATCH] 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 --- test/construction.test.ts | 5 +++-- test/drafts.test.ts | 5 +++-- test/helpers.ts | 13 ++++++++++++- test/integration.test.ts | 5 +++-- test/seed.test.ts | 5 +++-- test/seoIntegration.test.ts | 5 +++-- test/settingsDepth.test.ts | 7 ++++--- test/testConfig.test.ts | 25 +++++++++++++++++++++++++ 8 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 test/testConfig.test.ts diff --git a/test/construction.test.ts b/test/construction.test.ts index 15412f6..950ebbd 100644 --- a/test/construction.test.ts +++ b/test/construction.test.ts @@ -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 }] }, diff --git a/test/drafts.test.ts b/test/drafts.test.ts index dbb8299..1eee0b4 100644 --- a/test/drafts.test.ts +++ b/test/drafts.test.ts @@ -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', diff --git a/test/helpers.ts b/test/helpers.ts index a0b3b75..08bd9e3 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -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[] }> integrations?: Record diff --git a/test/integration.test.ts b/test/integration.test.ts index ce022d4..b3d1644 100644 --- a/test/integration.test.ts +++ b/test/integration.test.ts @@ -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[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: [] }, diff --git a/test/seed.test.ts b/test/seed.test.ts index cb25586..4f273c9 100644 --- a/test/seed.test.ts +++ b/test/seed.test.ts @@ -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: [] }, { diff --git a/test/seoIntegration.test.ts b/test/seoIntegration.test.ts index 53687d6..d2618ca 100644 --- a/test/seoIntegration.test.ts +++ b/test/seoIntegration.test.ts @@ -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: [] }, { diff --git a/test/settingsDepth.test.ts b/test/settingsDepth.test.ts index d59dbd5..a314c04 100644 --- a/test/settingsDepth.test.ts +++ b/test/settingsDepth.test.ts @@ -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 +let configPromise: ReturnType let mediaId: number | string beforeAll(async () => { - configPromise = buildConfig({ + configPromise = buildTestConfig({ collections: [ { slug: 'pages', diff --git a/test/testConfig.test.ts b/test/testConfig.test.ts new file mode 100644 index 0000000..0062634 --- /dev/null +++ b/test/testConfig.test.ts @@ -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([]) + }) +})