A
A
Andrey2017-05-21 22:20:35
MongoDB
Andrey, 2017-05-21 22:20:35

Why is the save to database function not working in a Mocha test?

I'm writing a test to check if a user is saved to a Mongo database (using Mongoose). I copied the
test code from the documentation :

const User = require('./../models/User');

describe('User', function() {
    describe('#save()', function() {
        const newUser = {
            name: 'TestUser',
            password: 'testPassword2',
            email: '[email protected]'
        };
        it('should save without error', function(done) {
            const user = new User(newUser);
            console.log(user); //1
            user.save(function(err) {
                console.log('save');
                if (err) done(err);
                else done();
            });
        });
    });
});

I am getting an error:
Error: Timeout of 2000ms exceeded. for async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

While console.log('save'); does not work out. That is, the user.save() function simply does not work.
At the same time, almost the same code in the application:
let user = new User({name, password, email});
    user.save((err) => {
        if(err) {
            res.send('ERROR! ' + JSON.stringify(err));
        } else {
            res.send('saved!');
        }
    });

outside of the test works with the same transmitted data.
As a test base I use an external service https://mlab.com/

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question