简体   繁体   中英

How to setup MongoDB from Mocha for some Node.js tests?

I'm manually testing some Node.js endpoints that access a MongoDB. And I'm using run-rs to setup replica sets in development mode ( https://www.npmjs.com/package/run-rs )

My process is the following:

  1. $ run-rs -v 4.0.0 -s
  2. rs:PRIMARY> use my_db
  3. rs:PRIMARY> load("create-db.js")

Then in a separate terminal window, npm start to run my Node.js app and connect to my_db. In create-db.js I have the setup of many collections. Something like:

db.categorie.insert({"categories":["all","cat","dog","cow","mus"]});

var users = [{
    "_id" : ObjectId("5b7b99477943f109fba8b128"),
    "admin" : false,
    "log" : false,
    "city" : "",
    "state" : "",
    "country" : "",
    "active" : true,
    "forgot" : false,
    "email" : "some+user@gmail.com",
    "hash" : "88409d4e8754af084ccb8554934e979bcd7c6a7f2532915b225a47b97de9b80a70c2c025e5610d9cdc15afebdd4efe26f9df586ee3b57763d196c96fb5dff96a1e3cfa80acf0bd232acb5b8eb7a2e9cf06a48dfd9b8cfd16ebd6fc33a1d7c5a6f66fb1c1efa45c570acb16e08c1867c8a58de40d4471433aa025de46cc21d07f52b68e0494f24f41efc2234c97070a943fb149640ae9e434de0df0154e0893718d3177c9bde61169cac2ee3bf265ffa99ca8338602d5be8c3e18aa7df99ba3cb38ca00bc17e85d680f92c6e3f84bb0cd63c062931b5e90793e9774c1f7c58a4312562e24752c366d5459ff9214b57309ac8e16357fee109cc1d397d48d1f1a6438d19226ffb0c42a175a0043a8a6405f85290657b1ce75504585123b0af9c1de01e3edc2d0538ab2246900e3a811c12ece1b2495e2d532a4a01f5e5ebc1ebe25853147071cfc394f9161d643b518327c5be777d5dabb701485a919533d972a046c4b86af6c3edf427ea73ac8d7a5cf19cf236ec9849984f4c1029a12c7ee0bc655125b9558eba223e6768a36c4bbbefdcda6f218561446bbc86efb41c4ca5755e89d3a4a9f3e7f480dea158e15687d6b1838356cc9ce37b86827283bb4a596681a4f691c5ab812769b753e7ece29d4dd81c90e98e3b8cf0261d88ecfc37c8d32db7efea99a84d2689d577cd20130b4c7f93493bd7c88d2a388c1de4d0d4c41d1",
    "salt" : "9b3a7884e00077a125e7cbdc9dddc09236804cee65gb31467910755aeb2759d0",
    "__v" : 0
},
{
...
},
{
...
}];
db.users.insertMany(users);

...and so on, with many other collections.

The database setup is huge and I want to keep things organised. The thing is that I'm running run-rs manually, and I'm also loading the script from mongo shell. I don't know how to to this from Mocha - or even if it's best practice or not.

If you would be doing this every time before testing a suite of test cases then you should use the before hook where you could set the db / all the collections etc. that are needed for the tests. Then you do your tests and in the after hook you cleanup.

So you would run your manual scripts I am assuming after the run-rs all in the before hook ... then do your tests.

Here is a link to MongoDB hooks

If you want to run the commands you can always use exec() and make this as setup process before test. if you want to always run it inside hook, make it as function and run.

exec('dir', (err, stdout, stderr) => {
  if (err) {
    console.error(`exec error: ${err}`);
    return;
  }

  console.log(`Number of files ${stdout}`);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM