Skip to content

Commit b40e9f8

Browse files
authored
chore: use transactions in tests running mongoDB memory server (#4750)
* chore: use transactions in tests running mongoDB memory server * chore: relationship test async setup changes * chore: async test fix * chore: flaky e2e localization test
1 parent e5a7907 commit b40e9f8

File tree

7 files changed

+51
-62
lines changed

7 files changed

+51
-62
lines changed

packages/db-mongodb/src/connect.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,18 @@ export const connect: Connect = async function connect(this: MongooseAdapter, pa
2929
urlToConnect = process.env.PAYLOAD_TEST_MONGO_URL
3030
} else {
3131
connectionOptions.dbName = 'payloadmemory'
32-
const { MongoMemoryServer } = require('mongodb-memory-server')
32+
const { MongoMemoryReplSet } = require('mongodb-memory-server')
3333
const getPort = require('get-port')
3434

3535
const port = await getPort()
36-
this.mongoMemoryServer = await MongoMemoryServer.create({
36+
this.mongoMemoryServer = await MongoMemoryReplSet.create({
3737
instance: {
3838
dbName: 'payloadmemory',
3939
port,
4040
},
41+
replSet: {
42+
count: 3,
43+
},
4144
})
4245

4346
urlToConnect = this.mongoMemoryServer.getUri()

test/config/int.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BlockField } from 'payload/types';
1+
import type { BlockField } from 'payload/types'
22

33
import payload from '../../packages/payload/src'
44
import { initPayloadTest } from '../helpers/configHelpers'

test/database/int.spec.ts

+14-22
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,12 @@ describe('database', () => {
1616
const collection = 'posts'
1717
const title = 'title'
1818
let user: TypeWithID & Record<string, unknown>
19-
let useTransactions = true
2019

2120
beforeAll(async () => {
2221
const init = await initPayloadTest({ __dirname, init: { local: false } })
2322
serverURL = init.serverURL
2423
const url = `${serverURL}/api/graphql`
2524
client = new GraphQLClient(url)
26-
if (payload.db.name === 'mongoose') {
27-
useTransactions = false
28-
}
2925

3026
const loginResult = await payload.login({
3127
collection: 'users',
@@ -57,15 +53,13 @@ describe('database', () => {
5753
req,
5854
})
5955

60-
if (useTransactions) {
61-
await expect(() =>
62-
payload.findByID({
63-
id: first.id,
64-
collection,
65-
// omitting req for isolation
66-
}),
67-
).rejects.toThrow('The requested resource was not found.')
68-
}
56+
await expect(() =>
57+
payload.findByID({
58+
id: first.id,
59+
collection,
60+
// omitting req for isolation
61+
}),
62+
).rejects.toThrow('The requested resource was not found.')
6963

7064
const second = await payload.create({
7165
collection,
@@ -180,15 +174,13 @@ describe('database', () => {
180174
// this should not do anything but is needed to be certain about the next assertion
181175
await commitTransaction(req)
182176

183-
if (useTransactions) {
184-
await expect(() =>
185-
payload.findByID({
186-
id: first.id,
187-
collection,
188-
req,
189-
}),
190-
).rejects.toThrow('The requested resource was not found.')
191-
}
177+
await expect(() =>
178+
payload.findByID({
179+
id: first.id,
180+
collection,
181+
req,
182+
}),
183+
).rejects.toThrow('The requested resource was not found.')
192184
})
193185
})
194186
})

test/fields-relationship/e2e.spec.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import type {
1111
} from './payload-types'
1212

1313
import payload from '../../packages/payload/src'
14-
import { mapAsync } from '../../packages/payload/src/utilities/mapAsync'
1514
import wait from '../../packages/payload/src/utilities/wait'
1615
import { initPageConsoleErrorCatch, openDocControls, saveDocAndAssert } from '../helpers'
1716
import { AdminUrlUtil } from '../helpers/adminUrlUtil'
@@ -525,10 +524,10 @@ async function clearAllDocs(): Promise<void> {
525524
}
526525

527526
async function clearCollectionDocs(collectionSlug: string): Promise<void> {
528-
const ids = (await payload.find({ collection: collectionSlug, limit: 100 })).docs.map(
529-
(doc) => doc.id,
530-
)
531-
await mapAsync(ids, async (id) => {
532-
await payload.delete({ id, collection: collectionSlug })
527+
await payload.delete({
528+
collection: collectionSlug,
529+
where: {
530+
id: { exists: true },
531+
},
533532
})
534533
}

test/fields/e2e.spec.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import path from 'path'
66
import type { RelationshipField, TextField } from './payload-types'
77

88
import payload from '../../packages/payload/src'
9-
import { mapAsync } from '../../packages/payload/src/utilities/mapAsync'
109
import wait from '../../packages/payload/src/utilities/wait'
1110
import {
1211
exactText,
@@ -900,6 +899,7 @@ describe('fields', () => {
900899
await page.goto(url.list)
901900
await page.locator('.row-1 .cell-title a').click()
902901
}
902+
903903
describe('cell', () => {
904904
test('ensure cells are smaller than 300px in height', async () => {
905905
const url: AdminUrlUtil = new AdminUrlUtil(serverURL, 'rich-text-fields')
@@ -1387,13 +1387,9 @@ describe('fields', () => {
13871387

13881388
afterEach(async () => {
13891389
// delete all existing relationship documents
1390-
const allRelationshipDocs = await payload.find({
1390+
await payload.delete({
13911391
collection: relationshipFieldsSlug,
1392-
limit: 100,
1393-
})
1394-
const relationshipIDs = allRelationshipDocs.docs.map((doc) => doc.id)
1395-
await mapAsync(relationshipIDs, async (id) => {
1396-
await payload.delete({ id, collection: relationshipFieldsSlug })
1392+
where: { id: { exists: true } },
13971393
})
13981394
})
13991395

test/localization/e2e.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ describe('Localization', () => {
216216
await page.fill('#field-layout__0__text', 'test')
217217
await saveDocAndAssert(page)
218218

219-
const originalDocURL = page.url()
219+
const originalID = await page.locator('.id-label').innerText()
220+
220221
// duplicate
221222
await openDocControls(page)
222223
await page.locator('#action-duplicate').click()
@@ -229,7 +230,7 @@ describe('Localization', () => {
229230
await expect(page.locator('.Toastify')).toContainText('successfully duplicated')
230231

231232
// expect that the document has a new id
232-
expect(page.url()).not.toStrictEqual(originalDocURL)
233+
await expect(page.locator('.id-label')).not.toContainText(originalID)
233234
})
234235
})
235236
})

test/relationships/int.spec.ts

+20-22
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import type {
1111
} from './payload-types'
1212

1313
import payload from '../../packages/payload/src'
14-
import { mapAsync } from '../../packages/payload/src/utilities/mapAsync'
1514
import { devUser } from '../credentials'
1615
import { initPayloadTest } from '../helpers/configHelpers'
1716
import { RESTClient } from '../helpers/rest'
@@ -40,7 +39,7 @@ describe('Relationships', () => {
4039
beforeAll(async () => {
4140
const { serverURL } = await initPayloadTest({ __dirname, init: { local: false } })
4241
apiUrl = `${serverURL}/api`
43-
client = new RESTClient(config, { serverURL, defaultSlug: slug })
42+
client = new RESTClient(config, { defaultSlug: slug, serverURL })
4443
await client.login()
4544

4645
const response = await fetch(`${apiUrl}/users/login`, {
@@ -128,8 +127,8 @@ describe('Relationships', () => {
128127
})
129128

130129
chained3 = await payload.update<ChainedRelation>({
131-
collection: chainedRelSlug,
132130
id: chained3.id,
131+
collection: chainedRelSlug,
133132
data: {
134133
name: 'chain3',
135134
relation: chained.id,
@@ -155,13 +154,13 @@ describe('Relationships', () => {
155154
})
156155

157156
post = await createPost({
158-
relationField: relation.id,
159-
defaultAccessRelation: defaultAccessRelation.id,
160157
chainedRelation: chained.id,
161-
maxDepthRelation: relation.id,
162-
customIdRelation: customIdRelation.id,
163158
customIdNumberRelation: customIdNumberRelation.id,
159+
customIdRelation: customIdRelation.id,
160+
defaultAccessRelation: defaultAccessRelation.id,
164161
filteredRelation: filteredRelation.id,
162+
maxDepthRelation: relation.id,
163+
relationField: relation.id,
165164
})
166165

167166
await createPost() // Extra post to allow asserting totalDoc count
@@ -194,15 +193,15 @@ describe('Relationships', () => {
194193
expect(docAfterUpdatingRel.filteredRelation).toMatchObject({ id: filteredRelation.id })
195194

196195
// Attempt to update post with a now filtered relation
197-
const { status, errors } = await client.update<Post>({
196+
const { errors, status } = await client.update<Post>({
198197
id: post.id,
199198
data: { filteredRelation: filteredRelation.id },
200199
})
201200

202201
expect(errors?.[0]).toMatchObject({
203202
name: 'ValidationError',
204-
message: expect.any(String),
205203
data: expect.anything(),
204+
message: expect.any(String),
206205
})
207206
expect(status).toEqual(400)
208207
})
@@ -307,8 +306,8 @@ describe('Relationships', () => {
307306

308307
it('should allow update removing a relationship', async () => {
309308
const result = await client.update<Post>({
310-
slug,
311309
id: post.id,
310+
slug,
312311
data: {
313312
relationField: null,
314313
},
@@ -446,8 +445,8 @@ describe('Relationships', () => {
446445
await payload.create({
447446
collection: 'screenings',
448447
data: {
449-
movie: movie.id,
450448
name: 'Pulp Fiction Screening',
449+
movie: movie.id,
451450
},
452451
})
453452
})
@@ -486,8 +485,8 @@ describe('Relationships', () => {
486485

487486
beforeAll(async () => {
488487
await Promise.all(
489-
movieList.map((movie) => {
490-
return payload.create({
488+
movieList.map(async (movie) => {
489+
return await payload.create({
491490
collection: 'movies',
492491
data: {
493492
name: movie,
@@ -528,8 +527,8 @@ describe('Relationships', () => {
528527
it('should allow clearing hasMany relationships', async () => {
529528
const fiveMovies = await payload.find({
530529
collection: 'movies',
531-
limit: 5,
532530
depth: 0,
531+
limit: 5,
533532
})
534533

535534
const movieIDs = fiveMovies.docs.map((doc) => doc.id)
@@ -545,8 +544,8 @@ describe('Relationships', () => {
545544
expect(stanley.movies).toHaveLength(5)
546545

547546
const stanleyNeverMadeMovies = await payload.update({
548-
collection: 'directors',
549547
id: stanley.id,
548+
collection: 'directors',
550549
data: {
551550
movies: null,
552551
},
@@ -621,18 +620,18 @@ describe('Relationships', () => {
621620
const req = {} as PayloadRequest
622621
req.transactionID = await payload.db.beginTransaction?.()
623622
const related = await payload.create({
624-
req,
625623
collection: relationSlug,
626624
data: {
627625
name: 'parent',
628626
},
627+
req,
629628
})
630629
const withRelation = await payload.create({
631-
req,
632630
collection: slug,
633631
data: {
634632
filteredRelation: related.id,
635633
},
634+
req,
636635
})
637636

638637
if (req.transactionID) {
@@ -656,8 +655,8 @@ describe('Relationships', () => {
656655
collection: 'polymorphic-relationships',
657656
data: {
658657
polymorphic: {
659-
value: movie.id,
660658
relationTo: 'movies',
659+
value: movie.id,
661660
},
662661
},
663662
})
@@ -709,9 +708,8 @@ async function createPost(overrides?: Partial<Post>) {
709708
}
710709

711710
async function clearDocs(): Promise<void> {
712-
const allDocs = await payload.find({ collection: slug, limit: 100 })
713-
const ids = allDocs.docs.map((doc) => doc.id)
714-
await mapAsync(ids, async (id) => {
715-
await payload.delete({ collection: slug, id })
711+
await payload.delete({
712+
collection: slug,
713+
where: { id: { exists: true } },
716714
})
717715
}

0 commit comments

Comments
 (0)