-
-
Notifications
You must be signed in to change notification settings - Fork 754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mongodb config does not seem to wait for the connection to resolve #996
Comments
I think adding something like before(() => app.get('mongoClient')); In the tests should also wait for the client to connect and run your tests reliably. |
From @eddyystop on July 30, 2017 21:20 These are not mocha tests, rather some test apps. I think this should at least be documented as its a time sink to track down. |
I wonder if there is a good way for the generator to wait for the connection before starting the server. |
From @eddyystop on August 1, 2017 17:35 That is the conundrum. One positive point is that MongoDB can only be used on the server. Can anything be done with async/await? |
For everyone coming here from google, I've worked around this by using a custom event which is emitted when the setup is finished. src/mongoose.ts:import mongoose from 'mongoose';
import { Application } from './declarations';
import logger from './logger';
export default function (app: Application) {
mongoose.connect(
app.get('mongodb'),
{ useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true },
).then(() => {
// This emits a event called mognoose-ready on app instance
app.emit('mongoose-ready');
}).catch((err) => {
logger.error(err);
process.exit(1);
});
mongoose.Promise = global.Promise;
app.set('mongooseClient', mongoose);
} src/app.test.ts beforeAll((done) => {
// Waits for mongoose to be initialized and ready then executed tests
app.on('mongoose-ready', () => {
done();
});
});
afterAll(() => {
app.get('mongooseClient').disconnect();
}) |
From @eddyystop on July 30, 2017 20:45
The relevent code in src/services/monododb.js is
My test apparently used a mongodb service before the connection was established. It looks like it threw because
app.service(...)
was undefined. It did not throw once I delayed the test with a setTimeout.I think the main concern is that an app using mongodb may sometimes boot and sometimes not.
Copied from original issue: feathersjs-ecosystem/generator-feathers#253
The text was updated successfully, but these errors were encountered: