Files
payloadcms/test/admin/seed.ts
Alessio Gravili 545d870650 chore: fix various e2e test setup issues (#12670)
I noticed a few issues when running e2e tests that will be resolved by
this PR:

- Most important: for some test suites (fields, fields-relationship,
versions, queues, lexical), the database was cleared and seeded
**twice** in between each test run. This is because the onInit function
was running the clear and seed script, when it should only have been
running the seed script. Clearing the database / the snapshot workflow
is being done by the reInit endpoint, which then calls onInit to seed
the actual data.
- The slowest part of `clearAndSeedEverything` is recreating indexes on
mongodb. This PR slightly improves performance here by:
- Skipping this process for the built-in `['payload-migrations',
'payload-preferences', 'payload-locked-documents']` collections
- Previously we were calling both `createIndexes` and `ensureIndexes`.
This was unnecessary - `ensureIndexes` is a deprecated alias of
`createIndexes`. This PR changes it to only call `createIndexes`
- Makes the reinit endpoint accept GET requests instead of POST requests
- this makes it easier to debug right in the browser
- Some typescript fixes
- Adds a `dev:memorydb` script to the package.json. For some reason,
`dev` is super unreliable on mongodb locally when running e2e tests - it
frequently fails during index creation. Using the memorydb fixes this
issue, with the bonus of more closely resembling the CI environment
- Previously, you were unable to run test suites using turbopack +
postgres. This fixes it, by explicitly installing `pg` as devDependency
in our monorepo
- Fixes jest open handles warning
2025-06-04 17:34:37 -03:00

140 lines
3.3 KiB
TypeScript

import type { Payload } from 'payload'
import { devUser } from '../credentials.js'
import { executePromises } from '../helpers/executePromises.js'
import {
customViews1CollectionSlug,
customViews2CollectionSlug,
geoCollectionSlug,
noApiViewCollectionSlug,
postsCollectionSlug,
usersCollectionSlug,
with300DocumentsSlug,
} from './slugs.js'
export const seed = async (_payload: Payload) => {
await executePromises(
[
() =>
_payload.create({
collection: usersCollectionSlug,
data: {
email: devUser.email,
password: devUser.password,
},
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: 'base-list-filters',
data: {
title: 'show me',
},
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: 'base-list-filters',
data: {
title: 'hide me',
},
depth: 0,
overrideAccess: true,
}),
...[...Array(11)].map((_, i) => async () => {
const postDoc = await _payload.create({
collection: postsCollectionSlug,
data: {
description: 'Description',
title: `Post ${i + 1}`,
disableListColumnText: 'Disable List Column Text',
disableListFilterText: 'Disable List Filter Text',
},
depth: 0,
overrideAccess: true,
})
return await _payload.update({
collection: postsCollectionSlug,
where: {
id: {
equals: postDoc.id,
},
},
data: {
relationship: postDoc.id,
},
depth: 0,
overrideAccess: true,
})
}),
() =>
_payload.create({
collection: customViews1CollectionSlug,
data: {
title: 'Custom View',
},
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: customViews2CollectionSlug,
data: {
title: 'Custom View',
},
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: geoCollectionSlug,
data: {
point: [7, -7],
},
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: geoCollectionSlug,
data: {
point: [5, -5],
},
depth: 0,
overrideAccess: true,
}),
() =>
_payload.create({
collection: noApiViewCollectionSlug,
data: {},
depth: 0,
overrideAccess: true,
}),
],
false,
)
// delete all with300Documents
await _payload.delete({
collection: with300DocumentsSlug,
where: {},
})
// Create 300 documents of with300Documents
const manyDocumentsPromises: Promise<unknown>[] = Array.from({ length: 300 }, (_, i) => {
const index = (i + 1).toString().padStart(3, '0')
return _payload.create({
collection: with300DocumentsSlug,
data: {
id: index,
text: `document ${index}`,
},
})
})
await Promise.all([...manyDocumentsPromises])
}