chore: attach mongoMemoryServer to db and destroy in tests (#5326)

* chore: attach mongoMemoryServer to db and destroy in tests

* bump mongodb-memory-server to 9.x

---------

Co-authored-by: Paul Popus <paul@nouance.io>
This commit is contained in:
Dan Ribbens
2024-03-14 15:41:20 -04:00
committed by GitHub
parent cbfc7c8b43
commit d193c677c7
36 changed files with 254 additions and 133 deletions

View File

@@ -119,7 +119,7 @@
"lexical": "0.13.1",
"lint-staged": "^14.0.1",
"minimist": "1.2.8",
"mongodb-memory-server": "8.13.0",
"mongodb-memory-server": "^9.0",
"next": "14.2.0-canary.21",
"node-mocks-http": "^1.14.1",
"nodemon": "3.0.3",

View File

@@ -1,8 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import type { ConnectOptions } from 'mongoose'
import type { Connect } from 'payload/database'
import mongoose from 'mongoose'
import type { Connect } from 'payload/database'
import type { MongooseAdapter } from './index.js'
@@ -37,12 +36,14 @@ export const connect: Connect = async function connect(
const client = this.connection.getClient()
if (!client.options.replicaSet) {
this.transactionOptions = false
this.beginTransaction = undefined
}
if (!hotReload) {
if (!this.mongoMemoryServer && !hotReload) {
if (process.env.PAYLOAD_DROP_DATABASE === 'true') {
this.payload.logger.info('---- DROPPING DATABASE ----')
await mongoose.connection.dropDatabase()
@@ -50,6 +51,7 @@ export const connect: Connect = async function connect(
}
}
} catch (err) {
console.log(err)
this.payload.logger.error(`Error: cannot connect to MongoDB. Details: ${err.message}`, err)
process.exit(1)
}

View File

@@ -6,11 +6,9 @@ import type { MongooseAdapter } from './index.js'
export const destroy: Destroy = async function destroy(this: MongooseAdapter) {
if (this.mongoMemoryServer) {
await mongoose.connection.dropDatabase()
await mongoose.connection.close()
await this.mongoMemoryServer.stop()
} else {
await mongoose.disconnect()
this.mongoMemoryServer.stop()
}
await mongoose.disconnect()
Object.keys(mongoose.models).map((model) => mongoose.deleteModel(model))
}

View File

@@ -1,12 +1,12 @@
import type { TransactionOptions } from 'mongodb'
import type { ClientSession, ConnectOptions, Connection } from 'mongoose'
import type { ClientSession, Connection, ConnectOptions } from 'mongoose'
import mongoose from 'mongoose'
import type { Payload } from 'payload'
import type { BaseDatabaseAdapter } from 'payload/database'
import type { BaseDatabaseAdapter, DatabaseAdapterObj } from 'payload/database'
import { createDatabaseAdapter } from 'payload/database'
import fs from 'fs'
import mongoose from 'mongoose'
import path from 'path'
import { createDatabaseAdapter } from 'payload/database'
import type { CollectionModel, GlobalModel } from './types.js'
@@ -37,7 +37,6 @@ import { updateOne } from './updateOne.js'
import { updateVersion } from './updateVersion.js'
export type { MigrateDownArgs, MigrateUpArgs } from './types.js'
import type { DatabaseAdapterObj } from 'payload/database'
export interface Args {
/** Set to false to disable auto-pluralization of collection names, Defaults to true */
@@ -47,6 +46,10 @@ export interface Args {
/** Set false to disable $facet aggregation in non-supporting databases, Defaults to true */
useFacet?: boolean
}
/**
* typed as any to avoid dependency
*/
mongoMemoryServer?: any
/** Set to true to disable hinting to MongoDB to use 'id' as index. This is currently done when counting documents for pagination. Disabling this optimization might fix some problems with AWS DocumentDB. Defaults to false */
disableIndexHints?: boolean
migrationDir?: string
@@ -91,6 +94,7 @@ export function mongooseAdapter({
autoPluralization = true,
connectOptions,
disableIndexHints = false,
mongoMemoryServer,
migrationDir: migrationDirArg,
transactionOptions = {},
url,
@@ -109,7 +113,7 @@ export function mongooseAdapter({
connection: undefined,
disableIndexHints,
globals: undefined,
mongoMemoryServer: undefined,
mongoMemoryServer,
sessions: {},
transactionOptions: transactionOptions === false ? undefined : transactionOptions,
url,

100
pnpm-lock.yaml generated
View File

@@ -10,7 +10,7 @@ overrides:
dotenv: 8.6.0
drizzle-orm: 0.29.4
graphql: ^16.8.1
mongodb-memory-server: 8.13.0
mongodb-memory-server: ^9.0
react: ^18.2.0
react-dom: ^18.2.0
typescript: 5.2.2
@@ -188,8 +188,8 @@ importers:
specifier: 1.2.8
version: 1.2.8
mongodb-memory-server:
specifier: 8.13.0
version: 8.13.0
specifier: ^9.0
version: 9.1.7
next:
specifier: 14.2.0-canary.21
version: 14.2.0-canary.21(@babel/core@7.24.0)(react-dom@18.2.0)(react@18.2.0)(sass@1.71.1)
@@ -385,8 +385,8 @@ importers:
specifier: 4.17.1
version: 4.17.1
mongodb-memory-server:
specifier: 8.13.0
version: 8.13.0
specifier: ^9.0
version: 9.1.7
payload:
specifier: workspace:*
version: link:../payload
@@ -7048,8 +7048,8 @@ packages:
tslib: 2.6.2
dev: true
/async-mutex@0.3.2:
resolution: {integrity: sha512-HuTK7E7MT7jZEh1P9GtRW9+aTWiDWWi9InbZ5hjxrnRa39KS4BW04+xLBhYNS2aXhHUIKZSw3gj4Pn1pj+qGAA==}
/async-mutex@0.4.1:
resolution: {integrity: sha512-WfoBo4E/TbCX1G95XTjbWTE3X2XLG0m1Xbv2cwOtuPdyH9CZvnaA5nCt1ucjaKEgW2A5IF71hxrRhr83Je5xjA==}
dependencies:
tslib: 2.6.2
dev: true
@@ -7113,7 +7113,7 @@ packages:
/axios@1.4.0:
resolution: {integrity: sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==}
dependencies:
follow-redirects: 1.15.5
follow-redirects: 1.15.5(debug@4.3.4)
form-data: 4.0.0
proxy-from-env: 1.1.0
transitivePeerDependencies:
@@ -7395,6 +7395,11 @@ packages:
dependencies:
buffer: 5.7.1
/bson@5.5.1:
resolution: {integrity: sha512-ix0EwukN2EpC0SRWIj/7B5+A6uQMQy6KMREI9qQqvgpkV2frH63T0UDVd1SYedL6dNCmDBYB3QtXi4ISk9YT+g==}
engines: {node: '>=14.20.1'}
dev: true
/buffer-crc32@0.2.13:
resolution: {integrity: sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==}
dev: true
@@ -10099,7 +10104,7 @@ packages:
tabbable: 5.3.3
dev: false
/follow-redirects@1.15.5:
/follow-redirects@1.15.5(debug@4.3.4):
resolution: {integrity: sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==}
engines: {node: '>=4.0'}
peerDependencies:
@@ -10107,7 +10112,8 @@ packages:
peerDependenciesMeta:
debug:
optional: true
dev: false
dependencies:
debug: 4.3.4(supports-color@5.5.0)
/for-each@0.3.3:
resolution: {integrity: sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==}
@@ -12276,7 +12282,7 @@ packages:
dependencies:
array-includes: 3.1.7
array.prototype.flat: 1.3.2
object.assign: 4.1.4
object.assign: 4.1.5
object.values: 1.1.7
dev: false
@@ -12647,12 +12653,6 @@ packages:
dependencies:
tmpl: 1.0.5
/md5-file@5.0.0:
resolution: {integrity: sha512-xbEFXCYVWrSx/gEKS1VPlg84h/4L20znVIulKw6kMfmBUAZNAnF00eczz9ICMl+/hjQGo5KSXRxbL/47X3rmMw==}
engines: {node: '>=10.13.0'}
hasBin: true
dev: true
/md5@2.3.0:
resolution: {integrity: sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==}
dependencies:
@@ -12851,38 +12851,44 @@ packages:
'@types/whatwg-url': 8.2.2
whatwg-url: 11.0.0
/mongodb-memory-server-core@8.13.0:
resolution: {integrity: sha512-4NTOzYOlRUilwb8CxOKix/XbZmac4cLpmEU03eaHx90lgEp+ARZM2PQtIOEg3nhHo97r9THIEv6Gs4LECokp0Q==}
engines: {node: '>=12.22.0'}
/mongodb-memory-server-core@9.1.7:
resolution: {integrity: sha512-q8geqCmt5hGuxaDhRo03ZUB0ITr6lnJ3jffdNiC4nDq13WbHUfY2A1RQq3OHDbdrY6aRYvZphx2bcXYBFRis3A==}
engines: {node: '>=14.20.1'}
dependencies:
async-mutex: 0.3.2
async-mutex: 0.4.1
camelcase: 6.3.0
debug: 4.3.4(supports-color@5.5.0)
find-cache-dir: 3.3.2
get-port: 5.1.1
https-proxy-agent: 5.0.1
md5-file: 5.0.0
mongodb: 4.17.1
follow-redirects: 1.15.5(debug@4.3.4)
https-proxy-agent: 7.0.4
mongodb: 5.9.2
new-find-package-json: 2.0.0
semver: 7.6.0
tar-stream: 2.2.0
tar-stream: 3.1.7
tslib: 2.6.2
uuid: 9.0.1
yauzl: 2.10.0
transitivePeerDependencies:
- aws-crt
- '@aws-sdk/credential-providers'
- '@mongodb-js/zstd'
- kerberos
- mongodb-client-encryption
- snappy
- supports-color
dev: true
/mongodb-memory-server@8.13.0:
resolution: {integrity: sha512-CyrKMwEmRePn8iQ3LtWQiOJxlGK0eM+NNTq3Yg8m7gaywepFu24mF7s13q87Kfuq0WgBuCJQ4t6VcUZJ4m+KWQ==}
engines: {node: '>=12.22.0'}
/mongodb-memory-server@9.1.7:
resolution: {integrity: sha512-Yxw1cUMoCKTK6jxk4cKG07P+Z/qOmuCVyt3ScIDaoHeOCbOlg2sEtXYO9vEK/tzpj/1KHdDStU2oYrsJ8Fvm0A==}
engines: {node: '>=14.20.1'}
requiresBuild: true
dependencies:
mongodb-memory-server-core: 8.13.0
mongodb-memory-server-core: 9.1.7
tslib: 2.6.2
transitivePeerDependencies:
- aws-crt
- '@aws-sdk/credential-providers'
- '@mongodb-js/zstd'
- kerberos
- mongodb-client-encryption
- snappy
- supports-color
dev: true
@@ -12899,6 +12905,34 @@ packages:
transitivePeerDependencies:
- aws-crt
/mongodb@5.9.2:
resolution: {integrity: sha512-H60HecKO4Bc+7dhOv4sJlgvenK4fQNqqUIlXxZYQNbfEWSALGAwGoyJd/0Qwk4TttFXUOHJ2ZJQe/52ScaUwtQ==}
engines: {node: '>=14.20.1'}
peerDependencies:
'@aws-sdk/credential-providers': ^3.188.0
'@mongodb-js/zstd': ^1.0.0
kerberos: ^1.0.0 || ^2.0.0
mongodb-client-encryption: '>=2.3.0 <3'
snappy: ^7.2.2
peerDependenciesMeta:
'@aws-sdk/credential-providers':
optional: true
'@mongodb-js/zstd':
optional: true
kerberos:
optional: true
mongodb-client-encryption:
optional: true
snappy:
optional: true
dependencies:
bson: 5.5.1
mongodb-connection-string-url: 2.6.0
socks: 2.8.1
optionalDependencies:
'@mongodb-js/saslprep': 1.1.4
dev: true
/mongoose-aggregate-paginate-v2@1.0.6:
resolution: {integrity: sha512-UuALu+mjhQa1K9lMQvjLL3vm3iALvNw8PQNIh2gp1b+tO5hUa0NC0Wf6/8QrT9PSJVTihXaD8hQVy3J4e0jO0Q==}
engines: {node: '>=4.0.0'}

View File

@@ -1,5 +1,4 @@
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { devUser } from '../credentials.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
@@ -36,7 +35,7 @@ describe('_Community Tests', () => {
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy(payload)
await payload.db.destroy()
}
})

View File

@@ -1,5 +1,4 @@
import type { Payload } from '../../../packages/payload/src/index.js'
import { getPayload } from '../../../packages/payload/src/index.js'
import { NextRESTClient } from '../../helpers/NextRESTClient.js'
import { startMemoryDB } from '../../startMemoryDB.js'
@@ -24,7 +23,7 @@ describe('AuthStrategies', () => {
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy(payload)
await payload.db.destroy()
}
})

View File

@@ -2,7 +2,6 @@ import jwtDecode from 'jwt-decode'
import type { User } from '../../packages/payload/src/auth/index.js'
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { devUser } from '../credentials.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
@@ -24,7 +23,7 @@ describe('Auth', () => {
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy(payload)
await payload.db.destroy()
}
})

View File

@@ -1,11 +1,9 @@
import type { Payload } from '../../../packages/payload/src/index.js'
import { getPayload } from '../../../packages/payload/src/index.js'
import { devUser } from '../../credentials.js'
import { NextRESTClient } from '../../helpers/NextRESTClient.js'
import { startMemoryDB } from '../../startMemoryDB.js'
import { collectionSlug } from './config.js'
import configPromise from './config.js'
import configPromise, { collectionSlug } from './config.js'
let restClient: NextRESTClient
let payload: Payload
@@ -24,7 +22,7 @@ describe('Remove token from auth responses', () => {
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy(payload)
await payload.db.destroy()
}
})

View File

@@ -1,7 +1,6 @@
import type { Payload } from '../../packages/payload/src/index.js'
import type { Post } from './payload-types.js'
import { getPayload } from '../../packages/payload/src/index.js'
import type { Post } from './payload-types.js'
import { mapAsync } from '../../packages/payload/src/utilities/mapAsync.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
import { idToString } from '../helpers/idToString.js'
@@ -33,7 +32,7 @@ describe('collections-graphql', () => {
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy(payload)
await payload.db.destroy()
}
})

View File

@@ -1,13 +1,8 @@
import { randomBytes } from 'crypto'
import type { Payload } from '../../packages/payload/src/index.js'
import type { Relation } from './config.js'
import type { Post } from './payload-types.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { mapAsync } from '../../packages/payload/src/utilities/mapAsync.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
import { startMemoryDB } from '../startMemoryDB.js'
import type { Relation } from './config.js'
import configPromise, {
customIdNumberSlug,
customIdSlug,
@@ -16,6 +11,10 @@ import configPromise, {
relationSlug,
slug,
} from './config.js'
import type { Post } from './payload-types.js'
import { mapAsync } from '../../packages/payload/src/utilities/mapAsync.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
import { startMemoryDB } from '../startMemoryDB.js'
let restClient: NextRESTClient
let payload: Payload
@@ -40,7 +39,7 @@ describe('collections-rest', () => {
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy(payload)
await payload.db.destroy()
}
})

View File

@@ -1,6 +1,5 @@
import type { BlockField } from '../../packages/payload/src/fields/config/types.js'
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise from './config.js'
@@ -13,6 +12,12 @@ describe('Config', () => {
payload = await getPayload({ config })
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('payload config', () => {
it('allows a custom field at the config root', () => {
const { config } = payload

View File

@@ -10,6 +10,7 @@ import { fileURLToPath } from 'url'
import { promisify } from 'util'
import { initNext } from '../../packages/create-payload-app/src/lib/init-next.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)

View File

@@ -12,6 +12,12 @@ describe('Custom GraphQL', () => {
restClient = new NextRESTClient(payload.config)
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('Isolated Transaction ID', () => {
it('should isolate transaction IDs between queries in the same request', async () => {
const query = `query {

View File

@@ -6,15 +6,15 @@ import { fileURLToPath } from 'url'
import type { PostgresAdapter } from '../../packages/db-postgres/src/types.js'
import type { TypeWithID } from '../../packages/payload/src/collections/config/types.js'
import type { Payload } from '../../packages/payload/src/index.js'
import type { PayloadRequest } from '../../packages/payload/src/types/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import type { PayloadRequest } from '../../packages/payload/src/types/index.js'
import { commitTransaction } from '../../packages/payload/src/utilities/commitTransaction.js'
import { initTransaction } from '../../packages/payload/src/utilities/initTransaction.js'
import { devUser } from '../credentials.js'
import removeFiles from '../helpers/removeFiles.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise from './config.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
@@ -40,6 +40,13 @@ describe('database', () => {
user = loginResult.user
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('migrations', () => {
beforeAll(async () => {
if (process.env.PAYLOAD_DROP_DATABASE === 'true' && 'drizzle' in payload.db) {

View File

@@ -1,11 +1,9 @@
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { devUser } from '../credentials.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise from './config.js'
import { postDoc } from './config.js'
import configPromise, { postDoc } from './config.js'
let restClient: NextRESTClient
let payload: Payload
@@ -28,6 +26,12 @@ describe('dataloader', () => {
if (loginResult.token) token = loginResult.token
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('graphql', () => {
it('should allow querying via graphql', async () => {
const query = `query {

View File

@@ -1,4 +1,4 @@
import { getPayload } from '../../packages/payload/src/index.js'
import { getPayload, type Payload } from '../../packages/payload/src/index.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise from './config.js'
@@ -12,15 +12,22 @@ import {
rootEndpoint,
} from './shared.js'
let payload: Payload
let restClient: NextRESTClient
describe('Endpoints', () => {
beforeAll(async () => {
const config = await startMemoryDB(configPromise)
await getPayload({ config })
payload = await getPayload({ config })
restClient = new NextRESTClient(config)
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('Collections', () => {
it('should GET a static endpoint', async () => {
const response = await restClient.GET(`/${collectionSlug}/say-hello/joe-bloggs`)

View File

@@ -3,9 +3,8 @@ import type { IndexDirection, IndexOptions } from 'mongoose'
import type { MongooseAdapter } from '../../packages/db-mongodb/src/index.js'
import type { PaginatedDocs } from '../../packages/payload/src/database/types.js'
import type { Payload } from '../../packages/payload/src/index.js'
import type { GroupField, RichTextField } from './payload-types.js'
import { getPayload } from '../../packages/payload/src/index.js'
import type { GroupField, RichTextField } from './payload-types.js'
import { devUser } from '../credentials.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
import { isMongoose } from '../helpers/isMongoose.js'
@@ -18,11 +17,7 @@ import { groupDoc } from './collections/Group/shared.js'
import { defaultNumber } from './collections/Number/index.js'
import { numberDoc } from './collections/Number/shared.js'
import { pointDoc } from './collections/Point/shared.js'
import {
localizedTextValue,
namedTabDefaultValue,
namedTabText,
} from './collections/Tabs/constants.js'
import { localizedTextValue, namedTabDefaultValue, namedTabText, } from './collections/Tabs/constants.js'
import { tabsDoc } from './collections/Tabs/shared.js'
import { defaultText } from './collections/Text/shared.js'
import configPromise from './config.js'
@@ -59,6 +54,12 @@ describe('Fields', () => {
})
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
beforeEach(async () => {
await clearAndSeedEverything(payload)
await restClient.login({

View File

@@ -1,5 +1,4 @@
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
import { startMemoryDB } from '../startMemoryDB.js'
@@ -21,6 +20,13 @@ describe('globals', () => {
payload = await getPayload({ config })
restClient = new NextRESTClient(config)
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('REST', () => {
it('should create', async () => {
const title = 'update'

View File

@@ -1,8 +1,8 @@
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import type { NestedAfterReadHook } from './payload-types.js'
import { AuthenticationError } from '../../packages/payload/src/errors/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { devUser, regularUser } from '../credentials.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
import { startMemoryDB } from '../startMemoryDB.js'
@@ -11,10 +11,7 @@ import { chainingHooksSlug } from './collections/ChainingHooks/index.js'
import { contextHooksSlug } from './collections/ContextHooks/index.js'
import { dataHooksSlug } from './collections/Data/index.js'
import { hooksSlug } from './collections/Hook/index.js'
import {
generatedAfterReadText,
nestedAfterReadHooksSlug,
} from './collections/NestedAfterReadHooks/index.js'
import { generatedAfterReadText, nestedAfterReadHooksSlug, } from './collections/NestedAfterReadHooks/index.js'
import { relationsSlug } from './collections/Relations/index.js'
import { transformSlug } from './collections/Transform/index.js'
import { hooksUsersSlug } from './collections/Users/index.js'
@@ -33,7 +30,7 @@ describe('Hooks', () => {
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy(payload)
await payload.db.destroy()
}
})

View File

@@ -2,12 +2,12 @@ import path from 'path'
import { fileURLToPath } from 'url'
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import type { Media, Page, Post, Tenant } from './payload-types.js'
import { handleMessage } from '../../packages/live-preview/src/handleMessage.js'
import { mergeData } from '../../packages/live-preview/src/mergeData.js'
import { traverseRichText } from '../../packages/live-preview/src/traverseRichText.js'
import { getPayload } from '../../packages/payload/src/index.js'
import getFileByPath from '../../packages/payload/src/uploads/getFileByPath.js'
import { fieldSchemaToJSON } from '../../packages/payload/src/utilities/fieldSchemaToJSON.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
@@ -16,6 +16,7 @@ import { Pages } from './collections/Pages.js'
import { postsSlug } from './collections/Posts.js'
import configPromise from './config.js'
import { tenantsSlug } from './shared.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
@@ -71,6 +72,12 @@ describe('Collections - Live Preview', () => {
})
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
it('handles `postMessage`', async () => {
const handledMessage = await handleMessage({
depth: 1,

View File

@@ -1,8 +1,7 @@
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import type { Where } from '../../packages/payload/src/types/index.js'
import type { LocalizedPost, WithLocalizedRelationship } from './payload-types.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { englishLocale } from '../globals/config.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
import { idToString } from '../helpers/idToString.js'
@@ -18,9 +17,9 @@ import {
portugueseLocale,
relationEnglishTitle,
relationEnglishTitle2,
relationshipLocalizedSlug,
relationSpanishTitle,
relationSpanishTitle2,
relationshipLocalizedSlug,
spanishLocale,
spanishTitle,
withLocalizedRelSlug,
@@ -68,7 +67,7 @@ describe('Localization', () => {
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy(payload)
await payload.db.destroy()
}
})

View File

@@ -4,11 +4,11 @@ import path from 'path'
import { fileURLToPath } from 'url'
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { describeIfInCIOrHasLocalstack } from '../helpers.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise from './config.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
@@ -19,6 +19,13 @@ describe('@payloadcms/plugin-cloud-storage', () => {
const config = await startMemoryDB(configPromise)
payload = await getPayload({ config })
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
const TEST_BUCKET = 'payload-bucket'
let client: AWS.S3Client

View File

@@ -1,5 +1,4 @@
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise from './config.js'
@@ -12,6 +11,12 @@ describe('@payloadcms/plugin-cloud', () => {
payload = await getPayload({ config })
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('tests', () => {
it.todo('plugin-cloud tests')
})

View File

@@ -1,13 +1,13 @@
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import type { Form } from './payload-types.js'
import { ValidationError } from '../../packages/payload/src/errors/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { serializeLexical } from '../../packages/plugin-form-builder/src/utilities/lexical/serializeLexical.js'
import { serializeSlate } from '../../packages/plugin-form-builder/src/utilities/slate/serializeSlate.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise from './config.js'
import { formSubmissionsSlug, formsSlug } from './shared.js'
import { formsSlug, formSubmissionsSlug } from './shared.js'
let payload: Payload
let form: Form
@@ -39,6 +39,12 @@ describe('@payloadcms/plugin-form-builder', () => {
})) as unknown as Form
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('plugin collections', () => {
it('adds forms collection', async () => {
const { docs: forms } = await payload.find({ collection: formsSlug })

View File

@@ -1,9 +1,5 @@
import type {
ArrayField,
RelationshipField,
} from '../../packages/payload/src/fields/config/types.js'
import type { ArrayField, RelationshipField, } from '../../packages/payload/src/fields/config/types.js'
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise from './config.js'
@@ -16,6 +12,12 @@ describe('@payloadcms/plugin-nested-docs', () => {
payload = await getPayload({ config })
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('seed', () => {
it('should populate two levels of breadcrumbs', async () => {
const query = await payload.find({

View File

@@ -1,7 +1,6 @@
import type { Payload } from '../../packages/payload/src/index.js'
import type { Page } from './payload-types.js'
import { getPayload } from '../../packages/payload/src/index.js'
import type { Page } from './payload-types.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise from './config.js'
import { pagesSlug } from './shared.js'
@@ -22,6 +21,12 @@ describe('@payloadcms/plugin-redirects', () => {
})
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
it('should add a redirects collection', async () => {
const redirect = await payload.find({
collection: 'redirects',

View File

@@ -1,5 +1,4 @@
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import wait from '../../packages/payload/src/utilities/wait.js'
import { startMemoryDB } from '../startMemoryDB.js'
@@ -13,6 +12,12 @@ describe('@payloadcms/plugin-search', () => {
payload = await getPayload({ config })
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
it('should add a search collection', async () => {
const search = await payload.find({
collection: 'search',

View File

@@ -1,5 +1,4 @@
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise from './config.js'
@@ -12,6 +11,12 @@ describe('@payloadcms/plugin-sentry', () => {
payload = await getPayload({ config })
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('tests', () => {
it.todo('plugin-sentry tests')
})

View File

@@ -2,13 +2,13 @@ import path from 'path'
import { fileURLToPath } from 'url'
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import getFileByPath from '../../packages/payload/src/uploads/getFileByPath.js'
import removeFiles from '../helpers/removeFiles.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise from './config.js'
import { mediaSlug } from './shared.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
@@ -48,6 +48,12 @@ describe('@payloadcms/plugin-seo', () => {
})
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
it('should add meta title', async () => {
const pageWithTitle = await payload.update({
collection: 'pages',

View File

@@ -1,5 +1,4 @@
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise from './config.js'
@@ -12,6 +11,12 @@ describe('Stripe Plugin', () => {
payload = await getPayload({ config })
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
it('should create products', async () => {
const product = await payload.create({
collection: 'products',

View File

@@ -1,5 +1,4 @@
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise, { pagesSlug } from './config.js'
@@ -12,6 +11,12 @@ describe('Collections - Plugins', () => {
payload = await getPayload({ config })
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
it('created pages collection', async () => {
const { id } = await payload.create({
collection: pagesSlug,

View File

@@ -1,6 +1,7 @@
import { randomBytes } from 'crypto'
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import type {
ChainedRelation,
CustomIdNumberRelation,
@@ -9,8 +10,6 @@ import type {
Post,
Relation,
} from './payload-types.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise from './config.js'
@@ -41,7 +40,7 @@ describe('Relationships', () => {
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy(payload)
await payload.db.destroy()
}
})

View File

@@ -1,4 +1,3 @@
import getPort from 'get-port'
import { MongoMemoryReplSet } from 'mongodb-memory-server'
import type { SanitizedConfig } from '../packages/payload/src/config/types.js'
@@ -22,20 +21,17 @@ export const startMemoryDB = async (
}
default: {
const port = await getPort()
const db = await MongoMemoryReplSet.create({
instanceOpts: [
{
port,
},
],
replSet: {
count: 3,
dbName: 'payloadmemory',
},
})
config.db = mongooseAdapter({ url: db.getUri() })
config.db = mongooseAdapter({
url: db.getUri(),
mongoMemoryServer: db,
})
return config
}

View File

@@ -6,21 +6,14 @@ import { fileURLToPath } from 'url'
import { promisify } from 'util'
import type { Payload } from '../../packages/payload/src/index.js'
import type { Enlarge, Media } from './payload-types.js'
import { getPayload } from '../../packages/payload/src/index.js'
import type { Enlarge, Media } from './payload-types.js'
import getFileByPath from '../../packages/payload/src/uploads/getFileByPath.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
import { startMemoryDB } from '../startMemoryDB.js'
import configPromise from './config.js'
import {
enlargeSlug,
mediaSlug,
reduceSlug,
relationSlug,
unstoredMediaSlug,
usersSlug,
} from './shared.js'
import { enlargeSlug, mediaSlug, reduceSlug, relationSlug, unstoredMediaSlug, usersSlug, } from './shared.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
@@ -84,6 +77,12 @@ describe('Collections - Uploads', () => {
await restClient.login({ slug: usersSlug })
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('REST', () => {
describe('create', () => {
it('creates from form data given a png', async () => {

View File

@@ -1,5 +1,4 @@
import type { Payload } from '../../packages/payload/src/index.js'
import { getPayload } from '../../packages/payload/src/index.js'
import { devUser } from '../credentials.js'
import { NextRESTClient } from '../helpers/NextRESTClient.js'
@@ -55,6 +54,12 @@ describe('Versions', () => {
token = data.loginUser.token
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
beforeEach(async () => {
await clearAndSeedEverything(payload)