bhofstaetter

payload integration test utils

by bhofstaetter
Testing & developer experience
v3
>=3
MIT

Overview

Summary generated from the README

Provides global setup and teardown functions that start a MongoDB or PostgreSQL Testcontainer, plus a helper that initializes a Payload instance for the collections and globals under test. Tests use the Local API through a context object. Requires Payload 3 or later, Vitest, Docker and the matching Testcontainers package.

Features

  • Global setup and teardown that start and stop a MongoDB or PostgreSQL container
  • getTestContextFor registers Vitest hooks and exposes a Payload instance
  • Deletes all documents in the provided collections before each test and after all tests
  • Supports collections, globals or both in a minimal test config
  • Caches Payload instances by database URL within a test file
  • Optional output path for generating Payload types

Use cases

  • Write integration tests for custom collections against a real database
  • Run the same Payload test suite against MongoDB and PostgreSQL
  • Test plugins using the Local API with a clean database for every test

From the author

Opinionated integration test helpers providing a ready-to-use Payload instance for vitest.

Installation

npm i payload-integration-test-utils

0

Stars

0

Forks

0

Open Issues

9 months ago

Last Updated

9

Weekly Downloads

v1.0.0

npm Version

12.1 KB

Unpacked Size

0

Dependencies

GitHub stars

00 since Sep 6

Show data
GitHub stars by day (UTC)
DateStars
Dec 5, 20260
Dec 4, 20260
Dec 3, 20260
Dec 2, 20260
Dec 1, 20260
Nov 30, 20260
Nov 29, 20260
Nov 28, 20260
Nov 27, 20260
Nov 26, 20260
Nov 25, 20260
Nov 24, 20260
Nov 23, 20260
Nov 22, 20260
Nov 21, 20260
Nov 20, 20260
Nov 19, 20260
Nov 18, 20260
Nov 17, 20260
Nov 16, 20260
Nov 15, 20260
Nov 14, 20260
Nov 13, 20260
Nov 12, 20260
Nov 11, 20260
Nov 10, 20260
Nov 9, 20260
Nov 8, 20260
Nov 7, 20260
Nov 6, 20260
Nov 5, 20260
Nov 4, 20260
Nov 3, 20260
Nov 2, 20260
Nov 1, 20260
Oct 31, 20260
Oct 30, 20260
Oct 29, 20260
Oct 28, 20260
Oct 27, 20260
Oct 26, 20260
Oct 25, 20260
Oct 24, 20260
Oct 23, 20260
Oct 22, 20260
Oct 21, 20260
Oct 20, 20260
Oct 19, 20260
Oct 18, 20260
Oct 17, 20260
Oct 16, 20260
Oct 15, 20260
Oct 14, 20260
Oct 13, 20260
Oct 12, 20260
Oct 11, 20260
Oct 10, 20260
Oct 9, 20260
Oct 8, 20260
Oct 7, 20260
Oct 6, 20260
Oct 5, 20260
Oct 4, 20260
Oct 3, 20260
Oct 2, 20260
Oct 1, 20260
Sep 30, 20260
Sep 29, 20260
Sep 28, 20260
Sep 27, 20260
Sep 26, 20260
Sep 25, 20260
Sep 24, 20260
Sep 23, 20260
Sep 22, 20260
Sep 21, 20260
Sep 20, 20260
Sep 19, 20260
Sep 18, 20260
Sep 17, 20260
Sep 16, 20260
Sep 15, 20260
Sep 14, 20260
Sep 13, 20260
Sep 12, 20260
Sep 11, 20260
Sep 10, 20260
Sep 9, 20260
Sep 8, 20260
Sep 7, 20260
Sep 6, 20260
npm weekly downloads

6+1 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 20266
Dec 4, 20267
Dec 3, 20266
Dec 2, 20264
Dec 1, 20267
Nov 30, 20267
Nov 29, 20266
Nov 28, 20265
Nov 27, 20264
Nov 26, 20264
Nov 25, 20264
Nov 24, 20261
Nov 23, 20261
Nov 22, 20262
Nov 21, 20264
Nov 20, 20263
Nov 19, 20265
Nov 18, 20266
Nov 17, 20266
Nov 16, 20267
Nov 15, 20267
Nov 14, 20265
Nov 13, 20268
Nov 12, 20267
Nov 11, 20266
Nov 10, 20266
Nov 9, 20265
Nov 8, 20265
Nov 7, 20265
Nov 6, 20262
Nov 5, 20261
Nov 4, 20261
Nov 3, 20262
Nov 2, 20266
Nov 1, 20266
Oct 31, 20266
Oct 30, 20266
Oct 29, 20266
Oct 28, 20266
Oct 27, 20266
Oct 26, 20263
Oct 25, 20262
Oct 24, 20262
Oct 23, 20262
Oct 22, 20265
Oct 21, 20265
Oct 20, 20264
Oct 19, 20263
Oct 18, 20263
Oct 17, 20264
Oct 16, 20264
Oct 15, 20262
Oct 14, 20262
Oct 13, 20262
Oct 12, 20262
Oct 11, 20263
Oct 10, 20262
Oct 9, 20262
Oct 8, 20261
Oct 7, 20261
Oct 6, 20261
Oct 5, 20261
Oct 4, 20260
Oct 3, 20261
Oct 2, 20261
Oct 1, 20262
Sep 30, 20262
Sep 29, 202610
Sep 28, 202618
Sep 27, 202625
Sep 26, 202629
Sep 25, 202633
Sep 24, 202633
Sep 23, 202633
Sep 22, 202627
Sep 21, 202619
Sep 20, 202613
Sep 19, 20268
Sep 18, 20264
Sep 17, 20264
Sep 16, 20265
Sep 15, 20263
Sep 14, 20265
Sep 13, 20265
Sep 12, 20266
Sep 11, 20266
Sep 10, 20265
Sep 9, 20265
Sep 8, 20265
Sep 7, 20266
Sep 6, 20265

Each point is npm's rolling 7-day download count.

Health Score

44/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1174814833-root.svg)](https://payloaddirectory.dev/plugins/1174814833-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1174814833-root"><img src="https://payloaddirectory.dev/badge/1174814833-root.svg" alt="Payload Directory"></a>
Updated Mar 8, 2026Created Mar 6, 2026Published Mar 8, 2026

README

Payload Integration Test Utils

Opinionated integration test helpers providing a ready-to-use Payload instance for vitest.

Installation

npm install -D payload-integration-test-utils

Depending on your used database

npm install -D @testcontainers/mongodb

or

npm install -D @testcontainers/postgresql

Usage

1. Create a global setup file

Create a file that re-exports the setup and teardown functions:

// test/setupIntegrationTest.ts
export {setup, teardown} from 'payload-integration-test-utils';

2. Configure Vitest

Reference the global setup file and set DB_TYPE plus the corresponding Docker image MONGO_DB_IMAGE|POSTGRES_IMAGE.

// vitest.config.ts
import {defineConfig} from 'vitest/config';

export default defineConfig({
    test: {
        projects: [
            {
                test: {
                    name: 'mongodb',
                    include: ['./test/integration/**/*.test.ts'],
                    globalSetup: './test/setupIntegrationTest.ts',
                    testTimeout: 30000,
                    hookTimeout: 30000,
                    fileParallelism: false,
                    env: {
                        DB_TYPE: 'mongodb',
                        MONGO_DB_IMAGE: 'mongo:8.0',
                    },
                },
            },
        ],
    },
});

3. Write tests

Use getTestContextFor to get a Payload Local API instance wired to the containerized database:

// test/integration/posts.test.ts
import type {CollectionConfig} from 'payload';
import {expect, it} from 'vitest';
import {getTestContextFor} from 'payload-integration-test-utils';

const Posts: CollectionConfig = {
    slug: 'posts',
    fields: [{type: 'text', name: 'title'}],
};

const ctx = getTestContextFor({collections: [Posts]});

it('creates and retrieves a document', async () => {
    const created = await ctx.payload.create({
        collection: 'posts',
        data: {title: 'Hello World'},
    });

    const result = await ctx.payload.find({collection: 'posts'});

    expect(result.totalDocs).toBe(1);
    expect(result.docs[0].id).toBe(created.id);
});

it('starts each test with a clean database', async () => {
    const count = await ctx.payload.count({collection: 'posts'});
    expect(count.totalDocs).toBe(0);
});

You can also test globals, or combine both:

import type {CollectionConfig, GlobalConfig} from 'payload';
import {getTestContextFor} from 'payload-integration-test-utils';

const Posts: CollectionConfig = {
    slug: 'posts',
    fields: [{type: 'text', name: 'title'}],
};

const Settings: GlobalConfig = {
    slug: 'settings',
    fields: [{type: 'text', name: 'siteTitle'}],
};

// collections only
const ctx = getTestContextFor({collections: [Posts]});

// globals only
const ctx = getTestContextFor({globals: [Settings]});

// both
const ctx = getTestContextFor({collections: [Posts], globals: [Settings]});

How It Works

Global Setup (setup / teardown)

The setup function reads DB_TYPE from the Vitest project env and starts the corresponding Testcontainer:

DB_TYPEContainer image envContainer
mongodbMONGO_DB_IMAGEMongoDBContainer
postgresPOSTGRES_IMAGEPostgreSqlContainer

It sets DATABASE_URL and DATABASE_TYPE as process environment variables so the Payload adapter can connect. The teardown function stops the container.

getTestContextFor(options)

Registers Vitest lifecycle hooks and returns a context object.

Options:

PropertyTypeDefaultDescription
collectionsCollectionConfig[][]Collections to register and clean
globalsGlobalConfig[][]Globals to register
tsOutputFilestring/dev/nullPath to generate Payload types to

Lifecycle hooks:

  • beforeAll — initializes a Payload instance with the given collections and globals using the database from the global setup
  • beforeEach — deletes all documents from every provided collection
  • afterAll — deletes all documents from every provided collection
  • ctx.payload — getter that returns the BasePayload instance for Local API calls

getPayloadInstance(collections, globals, tsOutputFile?)

Lower-level function used by getTestContextFor. Creates (or returns a cached) Payload instance based on DATABASE_TYPE and DATABASE_URL environment variables. Caches by URL so repeated calls in the same test file reuse the same instance.

Type Generation

Pass tsOutputFile to generate Payload types:

const ctx = getTestContextFor({collections: [Posts], tsOutputFile: './test/payload-types.ts'});

Defaults to /dev/null (disabled).

License

MIT

Todo

  • Github Actions

Similar plugins