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:
@@ -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
|
||||
|
||||
@@ -22,9 +22,12 @@ const connectMongoose = async (
|
||||
let mongoMemoryServer;
|
||||
|
||||
if (process.env.NODE_ENV === 'test') {
|
||||
connectionOptions.dbName = 'payloadmemory';
|
||||
const { MongoMemoryServer } = require('mongodb-memory-server');
|
||||
const getPort = require('get-port');
|
||||
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');
|
||||
|
||||
const port = await getPort();
|
||||
mongoMemoryServer = await MongoMemoryServer.create({
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user