Skip to content

Commit 545d870

Browse files
authored
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
1 parent 337f618 commit 545d870

File tree

28 files changed

+139
-126
lines changed

28 files changed

+139
-126
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"dev:generate-graphql-schema": "pnpm runts ./test/generateGraphQLSchema.ts",
6666
"dev:generate-importmap": "pnpm runts ./test/generateImportMap.ts",
6767
"dev:generate-types": "pnpm runts ./test/generateTypes.ts",
68+
"dev:memorydb": "cross-env NODE_OPTIONS=--no-deprecation tsx ./test/dev.ts --start-memory-db",
6869
"dev:postgres": "cross-env PAYLOAD_DATABASE=postgres pnpm runts ./test/dev.ts",
6970
"dev:prod": "cross-env NODE_OPTIONS=--no-deprecation tsx ./test/dev.ts --prod",
7071
"dev:prod:memorydb": "cross-env NODE_OPTIONS=--no-deprecation tsx ./test/dev.ts --prod --start-memory-db",
@@ -155,10 +156,11 @@
155156
"jest": "29.7.0",
156157
"lint-staged": "15.2.7",
157158
"minimist": "1.2.8",
158-
"mongodb-memory-server": "^10",
159+
"mongodb-memory-server": "10.1.4",
159160
"next": "15.3.2",
160161
"open": "^10.1.0",
161162
"p-limit": "^5.0.0",
163+
"pg": "8.11.3",
162164
"playwright": "1.50.0",
163165
"playwright-core": "1.50.0",
164166
"prettier": "3.5.3",

packages/db-mongodb/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"url": "https://payloadcms.com"
1818
}
1919
],
20-
"type": "module",
2120
"sideEffects": false,
21+
"type": "module",
2222
"exports": {
2323
".": {
2424
"import": "./src/index.ts",
@@ -58,7 +58,7 @@
5858
"@types/prompts": "^2.4.5",
5959
"@types/uuid": "10.0.0",
6060
"mongodb": "6.12.0",
61-
"mongodb-memory-server": "^10",
61+
"mongodb-memory-server": "10.1.4",
6262
"payload": "workspace:*"
6363
},
6464
"peerDependencies": {

packages/db-mongodb/src/connect.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export const connect: Connect = async function connect(
4040
// If we are running a replica set with MongoDB Memory Server,
4141
// wait until the replica set elects a primary before proceeding
4242
if (this.mongoMemoryServer) {
43+
this.payload.logger.info(
44+
'Waiting for MongoDB Memory Server replica set to elect a primary...',
45+
)
4346
await new Promise((resolve) => setTimeout(resolve, 2000))
4447
}
4548

@@ -50,7 +53,7 @@ export const connect: Connect = async function connect(
5053
this.beginTransaction = defaultBeginTransaction()
5154
}
5255

53-
if (!this.mongoMemoryServer && !hotReload) {
56+
if (!hotReload) {
5457
if (process.env.PAYLOAD_DROP_DATABASE === 'true') {
5558
this.payload.logger.info('---- DROPPING DATABASE ----')
5659
await mongoose.connection.dropDatabase()

packages/db-mongodb/src/destroy.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import mongoose from 'mongoose'
55
import type { MongooseAdapter } from './index.js'
66

77
export const destroy: Destroy = async function destroy(this: MongooseAdapter) {
8-
if (this.mongoMemoryServer) {
9-
await this.mongoMemoryServer.stop()
10-
} else {
11-
await mongoose.disconnect()
12-
}
8+
await mongoose.disconnect()
139

1410
Object.keys(mongoose.models).map((model) => mongoose.deleteModel(model))
1511
}

pnpm-lock.yaml

Lines changed: 20 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"eslint": "^9.23.0",
6464
"eslint-config-next": "15.3.0",
6565
"graphql": "^16.8.1",
66-
"mongodb-memory-server": "^10.1.2",
66+
"mongodb-memory-server": "10.1.4",
6767
"next": "15.3.0",
6868
"open": "^10.1.0",
6969
"payload": "3.37.0",

test/admin/seed.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import type { Payload } from 'payload'
22

33
import { devUser } from '../credentials.js'
44
import { executePromises } from '../helpers/executePromises.js'
5-
import { seedDB } from '../helpers/seed.js'
65
import {
7-
collectionSlugs,
86
customViews1CollectionSlug,
97
customViews2CollectionSlug,
108
geoCollectionSlug,
@@ -14,7 +12,7 @@ import {
1412
with300DocumentsSlug,
1513
} from './slugs.js'
1614

17-
export const seed = async (_payload) => {
15+
export const seed = async (_payload: Payload) => {
1816
await executePromises(
1917
[
2018
() =>
@@ -139,12 +137,3 @@ export const seed = async (_payload) => {
139137

140138
await Promise.all([...manyDocumentsPromises])
141139
}
142-
143-
export async function clearAndSeedEverything(_payload: Payload) {
144-
return await seedDB({
145-
_payload,
146-
collectionSlugs,
147-
seedFunction: seed,
148-
snapshotKey: 'adminTests',
149-
})
150-
}

test/buildConfigWithDefaults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { es } from 'payload/i18n/es'
3232
import sharp from 'sharp'
3333

3434
import { databaseAdapter } from './databaseAdapter.js'
35-
import { reInitEndpoint } from './helpers/reInit.js'
35+
import { reInitEndpoint } from './helpers/reInitEndpoint.js'
3636
import { localAPIEndpoint } from './helpers/sdk/endpoint.js'
3737
import { testEmailAdapter } from './testEmailAdapter.js'
3838

test/fields-relationship/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { Restricted } from './collections/Restricted/index.js'
1818
import { RelationshipUpdatedExternally } from './collections/UpdatedExternally/index.js'
1919
import { Versions } from './collections/Versions/index.js'
2020
import { Video } from './collections/Video/index.js'
21-
import { clearAndSeedEverything } from './seed.js'
21+
import { seed } from './seed.js'
2222

2323
export default buildConfigWithDefaults({
2424
admin: {
@@ -49,7 +49,7 @@ export default buildConfigWithDefaults({
4949
},
5050
onInit: async (payload) => {
5151
if (process.env.SEED_IN_CONFIG_ONINIT !== 'false') {
52-
await clearAndSeedEverything(payload)
52+
await seed(payload)
5353
}
5454
},
5555
typescript: {

test/fields-relationship/seed.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import type { Payload } from 'payload'
22

3-
import path from 'path'
4-
import { fileURLToPath } from 'url'
5-
63
import { devUser } from '../credentials.js'
7-
import { seedDB } from '../helpers/seed.js'
84
import {
95
collection1Slug,
106
collection2Slug,
11-
collectionSlugs,
127
podcastCollectionSlug,
138
relationOneSlug,
149
relationRestrictedSlug,
@@ -18,9 +13,6 @@ import {
1813
videoCollectionSlug,
1914
} from './slugs.js'
2015

21-
const filename = fileURLToPath(import.meta.url)
22-
const dirname = path.dirname(filename)
23-
2416
export const seed = async (_payload: Payload) => {
2517
await _payload.create({
2618
collection: 'users',
@@ -179,13 +171,3 @@ export const seed = async (_payload: Payload) => {
179171
})
180172
}
181173
}
182-
183-
export async function clearAndSeedEverything(_payload: Payload) {
184-
return await seedDB({
185-
_payload,
186-
collectionSlugs,
187-
seedFunction: seed,
188-
snapshotKey: 'fieldsTest',
189-
uploadsDir: path.resolve(dirname, './collections/Upload/uploads'),
190-
})
191-
}

0 commit comments

Comments
 (0)