lundi 13 janvier 2020

jest test with mongodb errors

I tried to do test in my project, and i got some errors from mongodb. I use test in function that login to the website, the function check if the user is in the data base and after check if the password is right. But when i use the test he dont know the data base, how i can to do that in the test he connected to my real mongodb? This is my code:

var expect = require('expect');
const MongoClient   = require('mongodb').MongoClient;
const app = require("../app"); // Link to your server file
const supertest = require("supertest");
const request = supertest(app);

const manager = require("../app/server/modules/account-manager");

var db, accounts, phrases, categories;
describe("Mongo DataBase Manager", () => {

beforeAll(async () => {                                 // here i tried to connect to my database
    MongoClient.connect(process.env.DB_URL, { useUnifiedTopology: true, useNewUrlParser: true }, function(e, client) {
        if (e) {
            console.log(e);
        } else {
            db = client.db(process.env.DB_NAME);
            accounts = db.collection('accounts');
            phrases = db.collection('pharses');
            categories = db.collection('categories');
        }
    });
});

test("swisa is in the accounts database", () => {
    const e = manager.manualLogin("swisa", "123456");
    expect(e).toBe(null);
});

});

this is the manual login function:

exports.manualLogin = function(user, pass, callback)
{
accounts.findOne({user:user}, function(e, o) {
    if (o == null){
        callback('user-not-found');
    }   else{
        validatePassword(pass, o.pass, function(err, res) {
            if (res){
                callback(null, o);
            }   else{
                callback('invalid-password');
            }
        });
    }
});

}

and the error is:

TypeError: Cannot read property 'findOne' of undefined

  201 | exports.manualLogin = function(user, pass, callback)
  202 | {
> 203 |     accounts.findOne({user:user}, function(e, o) {

thanks for help.

Aucun commentaire:

Enregistrer un commentaire