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.
Opinionated integration test helpers providing a ready-to-use Payload instance for vitest.
Installation
npm i payload-integration-test-utils0
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
00 since Sep 6
| Date | Stars |
|---|---|
| Dec 5, 2026 | 0 |
| Dec 4, 2026 | 0 |
| Dec 3, 2026 | 0 |
| Dec 2, 2026 | 0 |
| Dec 1, 2026 | 0 |
| Nov 30, 2026 | 0 |
| Nov 29, 2026 | 0 |
| Nov 28, 2026 | 0 |
| Nov 27, 2026 | 0 |
| Nov 26, 2026 | 0 |
| Nov 25, 2026 | 0 |
| Nov 24, 2026 | 0 |
| Nov 23, 2026 | 0 |
| Nov 22, 2026 | 0 |
| Nov 21, 2026 | 0 |
| Nov 20, 2026 | 0 |
| Nov 19, 2026 | 0 |
| Nov 18, 2026 | 0 |
| Nov 17, 2026 | 0 |
| Nov 16, 2026 | 0 |
| Nov 15, 2026 | 0 |
| Nov 14, 2026 | 0 |
| Nov 13, 2026 | 0 |
| Nov 12, 2026 | 0 |
| Nov 11, 2026 | 0 |
| Nov 10, 2026 | 0 |
| Nov 9, 2026 | 0 |
| Nov 8, 2026 | 0 |
| Nov 7, 2026 | 0 |
| Nov 6, 2026 | 0 |
| Nov 5, 2026 | 0 |
| Nov 4, 2026 | 0 |
| Nov 3, 2026 | 0 |
| Nov 2, 2026 | 0 |
| Nov 1, 2026 | 0 |
| Oct 31, 2026 | 0 |
| Oct 30, 2026 | 0 |
| Oct 29, 2026 | 0 |
| Oct 28, 2026 | 0 |
| Oct 27, 2026 | 0 |
| Oct 26, 2026 | 0 |
| Oct 25, 2026 | 0 |
| Oct 24, 2026 | 0 |
| Oct 23, 2026 | 0 |
| Oct 22, 2026 | 0 |
| Oct 21, 2026 | 0 |
| Oct 20, 2026 | 0 |
| Oct 19, 2026 | 0 |
| Oct 18, 2026 | 0 |
| Oct 17, 2026 | 0 |
| Oct 16, 2026 | 0 |
| Oct 15, 2026 | 0 |
| Oct 14, 2026 | 0 |
| Oct 13, 2026 | 0 |
| Oct 12, 2026 | 0 |
| Oct 11, 2026 | 0 |
| Oct 10, 2026 | 0 |
| Oct 9, 2026 | 0 |
| Oct 8, 2026 | 0 |
| Oct 7, 2026 | 0 |
| Oct 6, 2026 | 0 |
| Oct 5, 2026 | 0 |
| Oct 4, 2026 | 0 |
| Oct 3, 2026 | 0 |
| Oct 2, 2026 | 0 |
| Oct 1, 2026 | 0 |
| Sep 30, 2026 | 0 |
| Sep 29, 2026 | 0 |
| Sep 28, 2026 | 0 |
| Sep 27, 2026 | 0 |
| Sep 26, 2026 | 0 |
| Sep 25, 2026 | 0 |
| Sep 24, 2026 | 0 |
| Sep 23, 2026 | 0 |
| Sep 22, 2026 | 0 |
| Sep 21, 2026 | 0 |
| Sep 20, 2026 | 0 |
| Sep 19, 2026 | 0 |
| Sep 18, 2026 | 0 |
| Sep 17, 2026 | 0 |
| Sep 16, 2026 | 0 |
| Sep 15, 2026 | 0 |
| Sep 14, 2026 | 0 |
| Sep 13, 2026 | 0 |
| Sep 12, 2026 | 0 |
| Sep 11, 2026 | 0 |
| Sep 10, 2026 | 0 |
| Sep 9, 2026 | 0 |
| Sep 8, 2026 | 0 |
| Sep 7, 2026 | 0 |
| Sep 6, 2026 | 0 |
6+1 since Sep 6
| Date | Weekly downloads |
|---|---|
| Dec 5, 2026 | 6 |
| Dec 4, 2026 | 7 |
| Dec 3, 2026 | 6 |
| Dec 2, 2026 | 4 |
| Dec 1, 2026 | 7 |
| Nov 30, 2026 | 7 |
| Nov 29, 2026 | 6 |
| Nov 28, 2026 | 5 |
| Nov 27, 2026 | 4 |
| Nov 26, 2026 | 4 |
| Nov 25, 2026 | 4 |
| Nov 24, 2026 | 1 |
| Nov 23, 2026 | 1 |
| Nov 22, 2026 | 2 |
| Nov 21, 2026 | 4 |
| Nov 20, 2026 | 3 |
| Nov 19, 2026 | 5 |
| Nov 18, 2026 | 6 |
| Nov 17, 2026 | 6 |
| Nov 16, 2026 | 7 |
| Nov 15, 2026 | 7 |
| Nov 14, 2026 | 5 |
| Nov 13, 2026 | 8 |
| Nov 12, 2026 | 7 |
| Nov 11, 2026 | 6 |
| Nov 10, 2026 | 6 |
| Nov 9, 2026 | 5 |
| Nov 8, 2026 | 5 |
| Nov 7, 2026 | 5 |
| Nov 6, 2026 | 2 |
| Nov 5, 2026 | 1 |
| Nov 4, 2026 | 1 |
| Nov 3, 2026 | 2 |
| Nov 2, 2026 | 6 |
| Nov 1, 2026 | 6 |
| Oct 31, 2026 | 6 |
| Oct 30, 2026 | 6 |
| Oct 29, 2026 | 6 |
| Oct 28, 2026 | 6 |
| Oct 27, 2026 | 6 |
| Oct 26, 2026 | 3 |
| Oct 25, 2026 | 2 |
| Oct 24, 2026 | 2 |
| Oct 23, 2026 | 2 |
| Oct 22, 2026 | 5 |
| Oct 21, 2026 | 5 |
| Oct 20, 2026 | 4 |
| Oct 19, 2026 | 3 |
| Oct 18, 2026 | 3 |
| Oct 17, 2026 | 4 |
| Oct 16, 2026 | 4 |
| Oct 15, 2026 | 2 |
| Oct 14, 2026 | 2 |
| Oct 13, 2026 | 2 |
| Oct 12, 2026 | 2 |
| Oct 11, 2026 | 3 |
| Oct 10, 2026 | 2 |
| Oct 9, 2026 | 2 |
| Oct 8, 2026 | 1 |
| Oct 7, 2026 | 1 |
| Oct 6, 2026 | 1 |
| Oct 5, 2026 | 1 |
| Oct 4, 2026 | 0 |
| Oct 3, 2026 | 1 |
| Oct 2, 2026 | 1 |
| Oct 1, 2026 | 2 |
| Sep 30, 2026 | 2 |
| Sep 29, 2026 | 10 |
| Sep 28, 2026 | 18 |
| Sep 27, 2026 | 25 |
| Sep 26, 2026 | 29 |
| Sep 25, 2026 | 33 |
| Sep 24, 2026 | 33 |
| Sep 23, 2026 | 33 |
| Sep 22, 2026 | 27 |
| Sep 21, 2026 | 19 |
| Sep 20, 2026 | 13 |
| Sep 19, 2026 | 8 |
| Sep 18, 2026 | 4 |
| Sep 17, 2026 | 4 |
| Sep 16, 2026 | 5 |
| Sep 15, 2026 | 3 |
| Sep 14, 2026 | 5 |
| Sep 13, 2026 | 5 |
| Sep 12, 2026 | 6 |
| Sep 11, 2026 | 6 |
| Sep 10, 2026 | 5 |
| Sep 9, 2026 | 5 |
| Sep 8, 2026 | 5 |
| Sep 7, 2026 | 6 |
| Sep 6, 2026 | 5 |
Each point is npm's rolling 7-day download count.
Health Score
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/1174814833-root)<a href="https://payloaddirectory.dev/plugins/1174814833-root"><img src="https://payloaddirectory.dev/badge/1174814833-root.svg" alt="Payload Directory"></a>README
Opinionated integration test helpers providing a ready-to-use Payload instance for vitest.
npm install -D payload-integration-test-utils
Depending on your used database
npm install -D @testcontainers/mongodb
or
npm install -D @testcontainers/postgresql
Create a file that re-exports the setup and teardown functions:
// test/setupIntegrationTest.ts
export {setup, teardown} from 'payload-integration-test-utils';
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',
},
},
},
],
},
});
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]});
setup / teardown)The setup function reads DB_TYPE from the Vitest project env and starts the corresponding Testcontainer:
DB_TYPE | Container image env | Container |
|---|---|---|
mongodb | MONGO_DB_IMAGE | MongoDBContainer |
postgres | POSTGRES_IMAGE | PostgreSqlContainer |
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:
| Property | Type | Default | Description |
|---|---|---|---|
collections | CollectionConfig[] | [] | Collections to register and clean |
globals | GlobalConfig[] | [] | Globals to register |
tsOutputFile | string | /dev/null | Path to generate Payload types to |
Lifecycle hooks:
beforeAll — initializes a Payload instance with the given collections and globals using the database from the global setupbeforeEach — deletes all documents from every provided collectionafterAll — deletes all documents from every provided collectionctx.payload — getter that returns the BasePayload instance for Local API callsgetPayloadInstance(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.
Pass tsOutputFile to generate Payload types:
const ctx = getTestContextFor({collections: [Posts], tsOutputFile: './test/payload-types.ts'});
Defaults to /dev/null (disabled).
MIT
payload plugin tooling
Shared lint, format, TypeScript, build, test and CI config for developing Payload plugins
6039/wk0v4
payload pglite
Run Payload on an embedded PGlite Postgres for development, tests and CI without installing a database
350v3 v4
payload plugin template
Starter template for building Payload v3 plugins with a dev app, tests and Docker support
197v3
payload dev tools
Floating dev toolbar, /dev pages and an app snapshot for AI agents in Payload projects on Next.js
5548/wk18v3
payload dry run
Test create operations with full validation and hooks, then roll back so nothing is persisted
439/wk0v3
payload field factories
Typed factory functions that build Payload field configs with concise, composable overrides
77255/wk1v3