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

Fixed #111 by conditionally requiring the nedb and mongo clients. #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

stevefranchak
Copy link

@stevefranchak stevefranchak commented Mar 20, 2018

I ran into #111 while developing an Electron application that is only going to use nedb. I installed camo with npm install --no-optional camo since my application directly depends on nedb and I did not want mongodb and its dependencies (like kerberos) to be installed. I received a similar error to what is provided in #111.

I fixed this by conditionally requiring the nedb and mongo clients in lib/db.js. With this change, the aforementioned error will only be thrown if the user intentionally tries to use one of the client databases whose corresponding module is not installed.

I did not provide tests as I did not know if it matters that the unit tests fail for a --no-optional installation.

I started playing with the idea of allowing unit tests to pass if either the nedb or mongodb modules were not detected (depending on the test), but then realized this may not be wanted or needed. I tried out the idea of figuring out whether to call describe or describe.skip based on whether a callback function that contains require calls throws an error. Before that approach, I tried calling this.skip() inside of before() hooks, but this did not seem to work (probably because camo's version of mocha is < 3).

Example:

In test/util.js:

// Skips entire test suite if the callback 'tryRequires' throws an error.
exports.maybeDescribe = function(tryRequires = () => {}) {
  let maybeDescribe = this;
  try {
    tryRequires();
  } catch(err) {
    maybeDescribe = this.skip;
  }
  return maybeDescribe;
}

In test/mongoclient.test.js:

const maybeDescribe = require('./util').maybeDescribe;
let ObjectId; // changed from const

// I had to pass the 'describe' in this context since
// it did not equal require('mocha').describe in test/util.js.
// TODO: look into this further 
maybeDescribe.call(describe, () => {
    ObjectID = require('mongodb').ObjectId;
})('MongoClient', function() {
// . . . .
});

Feedback on how to go about providing unit tests if they're needed for this fix would be much appreciated.

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

Successfully merging this pull request may close these issues.

1 participant