Skip to content

Commit

Permalink
winstonjs#225: Make tests use in-memory MongoDB by default
Browse files Browse the repository at this point in the history
  • Loading branch information
curledUpSheep committed Jul 13, 2023
1 parent 8b27274 commit 004eb44
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions test/winston-mongodb-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,56 @@ require('dotenv').config();
const mongodb = require('mongodb');
const mongoose = require('mongoose');
const testSuite = require('abstract-winston-transport');
const { MongoMemoryServer } = require('mongodb-memory-server');

const MongoDB = require('../lib/winston-mongodb').MongoDB;

const dbUrl = process.env.USER_WINSTON_MONGODB_URL
|| process.env.WINSTON_MONGODB_URL || 'mongodb://localhost:27017/winston';
const dbName = process.env.WINSTON_MONGODB_DBNAME
|| 'otherWinston';

mongoose.connect(dbUrl, { useNewUrlParser: true });
let dbUrl;

async function setUpDb() {
const customDbUrl = process.env.USER_WINSTON_MONGODB_URL
|| process.env.WINSTON_MONGODB_URL;

if (customDbUrl) {
dbUrl = customDbUrl;
} else {
const inMemoryMongo = await MongoMemoryServer.create();
dbUrl = inMemoryMongo.getUri('winston');
}

mongoose.connect(dbUrl, { useNewUrlParser: true });
}

before(setUpDb);
before(setUpDynamicallyGeneratedTests);

describe('winston-mongodb-manual-tests', function () {
describe('winston-mongodb', function () {
const transport = new MongoDB({ db: dbUrl });
it('should be closeable', function () {
const transport = new MongoDB({ db: dbUrl });
transport.close();
});
});
});

testSuite({ name: '{db: url}', Transport: MongoDB, construct: { db: dbUrl }});
testSuite({ name: '{db: url, dbName: string}', Transport: MongoDB,
construct: { db: dbUrl, dbName }});
testSuite({ name: '{db: url} on capped collection', Transport: MongoDB,
construct: { db: dbUrl, capped: true, collection: 'cappedLog' }});
testSuite({ name: '{db: url, dbName: string} on capped collection', Transport: MongoDB,
construct: { db: dbUrl, dbName, capped: true, collection: 'cappedLog' }});
testSuite({ name: '{db: client promise}', Transport: MongoDB,
construct: { db: mongodb.MongoClient.connect(dbUrl, { useNewUrlParser: true }) }});
testSuite({ name: '{db: client promise, dbName: string}', Transport: MongoDB,
construct: {
dbName,
db: mongodb.MongoClient.connect(dbUrl, { useNewUrlParser: true }) }
});
testSuite({ name: '{db: mongoose client}', Transport: MongoDB,
construct: { db: mongoose.connection }});
function setUpDynamicallyGeneratedTests() {
testSuite({ name: '{db: url}', Transport: MongoDB, construct: { db: dbUrl }});
testSuite({ name: '{db: url, dbName: string}', Transport: MongoDB,
construct: { db: dbUrl, dbName }});
testSuite({ name: '{db: url} on capped collection', Transport: MongoDB,
construct: { db: dbUrl, capped: true, collection: 'cappedLog' }});
testSuite({ name: '{db: url, dbName: string} on capped collection', Transport: MongoDB,
construct: { db: dbUrl, dbName, capped: true, collection: 'cappedLog' }});
testSuite({ name: '{db: client promise}', Transport: MongoDB,
construct: { db: mongodb.MongoClient.connect(dbUrl, { useNewUrlParser: true }) }});
testSuite({ name: '{db: client promise, dbName: string}', Transport: MongoDB,
construct: {
dbName,
db: mongodb.MongoClient.connect(dbUrl, { useNewUrlParser: true }) }
});
testSuite({ name: '{db: mongoose client}', Transport: MongoDB,
construct: { db: mongoose.connection }});
}

0 comments on commit 004eb44

Please sign in to comment.