Skip to content
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

[Bug] Mongoist does not open new connection after close #15

Open
xfg opened this issue Mar 12, 2018 · 0 comments
Open

[Bug] Mongoist does not open new connection after close #15

xfg opened this issue Mar 12, 2018 · 0 comments

Comments

@xfg
Copy link

xfg commented Mar 12, 2018

I have created db.js module with code

//db.js
module.exports = mongoist('test');

Now I use db.js module within my tests in the following way

//foobar.spec.js
const db = require('./db');

describe('foo', () => {
  beforeEach(() => db.connect());
  afterEach(() => db.close());

  it('is some test', async () => {
    expect(await db.mycol.find({_id: 'foo'})).toBeNull();
  });

  it('is some other test', async () => {
    expect(await db.mycol.find({_id: 'bar'})).toBeNull();
  });
});

The second test will fall with an error MongoError: topology was destroyed because the connection will be closed after first test but not be opened anew before second test.

It is happen because Database#connect method always try to return the old connection even if it was be closed.

I think we should set this.connection to equal null within Database#close method about like this

return this.client
  .close(force)
  .then(() => this.connection = null)
  .catch((e) => {
  // Ignore this
    debug(`Could not close the connection due to error "${e.message}" ${e.stack}`);
  });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant