chore: allow custom mongourl during test (#2743)

Co-authored-by: swenzel <swen.wenzel@thearc.de>
Co-authored-by: PatrikKozak <patrik@trbl.design>
This commit is contained in:
Dan Ribbens
2023-05-31 15:51:49 -04:00
committed by GitHub
parent 8ee9724277
commit 93a85dd937
3 changed files with 18 additions and 7 deletions

View File

@@ -49,6 +49,12 @@ The directory split up in this way specifically to reduce friction when creating
The following command will start Payload with your config: `yarn dev my-test-dir`. This command will start up Payload using your config and refresh a test database on every restart.
If you wish to use to your own Mongo database for the `test` directory instead of using the in memory database, all you need to do is add the following env vars to the `test/dev.ts` file:
- `process.env.NODE_ENV`
- `process.env.PAYLOAD_TEST_MONGO_URL`
- Simply set `process.env.NODE_ENV` to `test` and set `process.env.PAYLOAD_TEST_MONGO_URL` to your mongo url e.g. `mongodb://127.0.0.1/your-test-db`.
NOTE: It is recommended to add the test credentials (located in `test/credentials.ts`) to your autofill for `localhost:3000/admin` as this will be required on every nodemon restart. The default credentials are `dev@payloadcms.com` as E-Mail and `test` as password.
## Pull Requests

View File

@@ -22,6 +22,9 @@ const connectMongoose = async (
let mongoMemoryServer;
if (process.env.NODE_ENV === 'test') {
if (process.env.PAYLOAD_TEST_MONGO_URL) {
urlToConnect = process.env.PAYLOAD_TEST_MONGO_URL;
} else {
connectionOptions.dbName = 'payloadmemory';
const { MongoMemoryServer } = require('mongodb-memory-server');
const getPort = require('get-port');
@@ -46,10 +49,12 @@ const connectMongoose = async (
await mongoose.connection.dropDatabase();
logger.info('---- DROPPED DATABASE ----');
}
logger.info(successfulConnectionMessage);
} catch (err) {
logger.error(`Error: cannot connect to MongoDB. Details: ${err.message}`, err);
logger.error(
`Error: cannot connect to MongoDB. Details: ${err.message}`,
err,
);
process.exit(1);
}

View File

@@ -2,8 +2,8 @@ import fs from 'fs';
import path from 'path';
import express from 'express';
import { v4 as uuid } from 'uuid';
import * as dotenv from 'dotenv';
import payload from '../src';
import * as dotenv from "dotenv";
dotenv.config();
@@ -30,7 +30,7 @@ const expressApp = express();
const startDev = async () => {
await payload.init({
secret: uuid(),
mongoURL: process.env.MONGO_URL || 'mongodb://127.0.0.1/payload',
mongoURL: 'mongodb://127.0.0.1/payload',
express: expressApp,
email: {
logMockCredentials: true,