Test Node.js API with Jest (and mockgoose) - node.js

Two questions here:
1) Is Jest a good options to test Node.js (express) APIs?
2) I'm trying to use Jest with Mockgoose, but I can't figure out how to establish the connection and run tests after. Here is my final attempt before coming on SO:
const Mongoose = require('mongoose').Mongoose
const mongoose = new Mongoose()
mongoose.Promise = require('bluebird')
const mockgoose = require('mockgoose')
const connectDB = (cb) => () => {
return mockgoose(mongoose).then(() => {
return mongoose.connect('mongodb://test/testingDB', err => {
if (err) {
console.log('err is', err)
return process.exit()
}
return cb(() => {
console.log('END') // this is logged
mongoose.connection.close()
})
})
})
}
describe('test api', connectDB((end) => {
test('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3)
})
end()
}))
The error is Your test suite must contain at least one test. This error makes a bit of sense to me but I can't figure out how to solve it. Any suggestions?
Output:
Test suite failed to run
Your test suite must contain at least one test.

Very late answer, but I hope it'll help.
If you pay attention, your describe block has no test function inside it.
The test function is actually inside the callback passed to describe.. kind of, the stack is complicated due to arrow function callbacks.
this example code will generate the same problem..
describe('tests',function(){
function cb() {
setTimeout(function(){
it('this does not work',function(end){
end();
});
},500);
}
cb();
setTimeout(function(){
it('also does not work',function(end){
end();
});
},500);
});
since the connection to mongo is async, when jest first scans the function to find "tests" inside the describe, it fails as there is none.
it may not look like it, but that's exactly what you're doing.
I think in this case your solution was a bit too clever(to the point it doesn't work), and breaking it down to simpler statements could've helped pinpointing this issue

var mongoose = require('mongoose');
// mongoose.connect('mongodb://localhost/animal', { useNewUrlParser: true });
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
var kittySchema = new mongoose.Schema({
name: String
});
var god = mongoose.model('god', kittySchema);
module.exports = god;
code for god.js file
describe("post api", () => {
//life cycle hooks for unit testing
beforeAll(() => {
mongoose.connect(
"mongodb://localhost/animal",
{ useNewUrlParser: true }
);
});
//test the api functions
test("post the data", async () => {
console.log("inside the test data ");
var silence = await new god({ name: "bigboss" }).save();
});
// disconnecting the mongoose connections
afterAll(() => {
// mongoose.connection.db.dropDatabase(function (err) {
// console.log('db dropped');
// // process.exit(0);
// });
mongoose.disconnect(done);
});
});
Jest testing code ..using jest we can we store the name in db

Related

Best way to setup files for jest and mongodb

Hihi.
I'm having a problem where jest it's sometimes failing a couple of tests randomly, most of the time because of this error "mongodb memory server cannot perform operation: a background operation is currently running for collection".
In another post I read something about building differents mongo instance for each block of tests.
What I have so far is a globalsetup file where I start the mongo replica set like this:
// global.ts
import { MongoMemoryReplSet } from "mongodb-memory-server";
const replSet = new MongoMemoryReplSet({
replSet: { storageEngine: "wiredTiger" },
});
module.exports = async () => {
await replSet.waitUntilRunning();
const uri = await replSet.getUri();
process.env.MONGODB_URI = uri;
};
and my db.ts is like this
// db.ts
export const connect = async () => {
mongoose.set("useFindAndModify", false);
const conn = mongoose.connect(
process.env.MONGODB_URI || config.connectionString, connectionSettings
);
When trying to call it from a test file I do something like this
// test.spec.ts
import db from "./db";
beforeAll(async () => {
await db.connect();
});
afterAll(async (done) => {
await db.dropCollections();
await db.disconnect(done);
});
beforeEach(async () => {
await seed();
});
describe('Some test', () => {
it('Should not fail and get the seeders', () => {
// some random tests using the seeds values
})
})
What I read in that post is instead of using globalSetup use a setupFile that will run for every test instead of one globally and then I MIGHT be able to solve the concurrency issue I have with my tests.
So, to conclude, does anyone knows if there is a proper way to configure the mongodb in memory or if I am doing something THAT BAD that is allowing this to happend or if is there any kind of improvement I can do that will prevent "mongodb memory server cannot perform operation: a background operation is currently running for collection" this to happen?

How do you test Postgres with Node.js / Jest without mocking the pg import

I have 2 SQL queries I want to test, an INSERT and a SELECT. I want to run a test that calls the insert, and then calls the select, which should return the data from the insert.
I found a stackoverflow post about mocking postgres, but I want to actually run a test instance of postgres instead of mocking the actual queries.
An integration test with postgres could look like this:
const { Pool } = require('pg');
describe('testing postgres', () => {
let pgPool;
beforeAll(() => {
pgPool = new Pool({
connectionString: process.env.TEST_DATABASE_URL
});
});
afterAll(async () => {
await pgPool.end();
});
it('should test', async () => {
const client = await pgPool.connect();
try {
await client.query('BEGIN');
const { rows } = await client.query('SELECT 1 AS "result"');
expect(rows[0]["result"]).toBe(1);
await client.query('ROLLBACK');
} catch(err) {
throw err;
} finally {
client.release();
}
})
});
You would call this test with:
TEST_DATABASE_URL=postgresql://sth:sth#someserver/testdb jest
Testing an insert and select can be done by using RETURNING *.
const { rows } = await client.query('INSERT INTO ... RETURNING *');
Using BEGIN and ROLLBACK is necessary - so you do not alter the database and have side effects for the next test to come.

mocha async test is timing out

Hi I am new to testing with Mocha let alone async testing. I keep getting the following error when running this test. I have spend a lot of time researching the resolution on the web but no luck.
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
it('Should fail to create new user due to missing email', (done) => {
const user_empty_email = {
name: "First Name",
email: "",
password: "password",
isAdmin: false
}
chai.request(app).post('/v1/users')
.send(user_empty_email)
.then((res) => {
expect(res).to.have.status(400);
done();
}).catch(done)
})
Below is an example response I am getting fir the /v1/users
{
"user": {
"_id": "5de4293d3501dc21d2c5293c",
"name": "Test Person",
"email": "testemail#gmail.com",
"password": "$2a$08$8us1C.thHWsvFw3IRX6o.usskMasZVAyrmccTNBjxpNQ8wrhlBt6q",
"isAdmin": false,
"tokens": [
{
"_id": "5de4293d3501dc21d2c5293d",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZGU0MjkzZDM1MDFkYzIxZDJjNTI5M2MiLCJpYXQiOjE1NzUyMzM4NTN9.mi4YyYcHCvdYrl7OuI5eDwJ8xQyKWDcqgKsXRYtn0kw"
}
],
"__v": 1
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZGU0MjkzZDM1MDFkYzIxZDJjNTI5M2MiLCJpYXQiOjE1NzUyMzM4NTN9.mi4YyYcHCvdYrl7OuI5eDwJ8xQyKWDcqgKsXRYtn0kw"
}
why don't you try increasing the timeout in (ms), it's normal for tests to run slow especially when your testing network requests.
package.json
"test": "mocha --timeout 10000"
Is there a chance that your endpoint actually takes more than 2 seconds to run? If so, you might want to increase the timeout when running Mocha: Change default timeout for mocha.
Also, does your endpoint return a response? If not, increasing the timeout will not help. Could you add the code of the /v1/users endpoint to your question to look into that possibility?
Not sure about this. But as far as i recall mixing Promises and callback-style (done-callback) can cause such problems in mocha.
Try using Promises only:
remove all done from test
actually return the Promise (return chai.request...)
the issue was this test:
mongoDB-connect.js
It was ran before the others. mongoDB connection was being closed which caused the other tests to timeout. When I removed the close command all tests passed as expected.
"use strict";
// NPM install mongoose and chai. Make sure mocha is globally
// installed
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const chai = require('chai');
const expect = chai.expect;
// Create a new schema that accepts a 'name' object.
// 'name' is a required field
const testSchema = new Schema({
name: { type: String, required: true }
});
// mongoose connect options
var options = {
useNewUrlParser: true,
useUnifiedTopology: true
}
//Create a new collection called 'Name'
const Name = mongoose.model('Name', testSchema);
describe('Database Tests', function() {
//Before starting the test, create a sandboxed database connection
//Once a connection is established invoke done()
before(function (done) {
// mongoose.connect('mongodb://localhost:27017',options);
mongoose.connect('mongodb://ip/testDatabase',options);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error'));
db.once('open', function() {
console.log('We are connected to test database!');
done();
});
});
describe('Test Database', function() {
//Save object with 'name' value of 'Mike"
it('New name saved to test database', function(done) {
var testName = Name({
name: 'Mike'
});
testName.save(done);
});
it('Dont save incorrect format to database', function(done) {
//Attempt to save with wrong info. An error should trigger
var wrongSave = Name({
notName: 'Not Mike'
});
wrongSave.save(err => {
if(err) { return done(); }
throw new Error('Should generate error!');
});
});
it('Should retrieve data from test database', function(done) {
//Look up the 'Mike' object previously saved.
Name.find({name: 'Mike'}, (err, name) => {
if(err) {throw err;}
if(name.length === 0) {throw new Error('No data!');}
done();
});
});
});
//After all tests are finished drop database and close connection
after(function(done){
mongoose.connection.db.dropDatabase(function(){
mongoose.connection.close(done);
});
});
});

How to restore mongo in chai mocha

So I want to restore mongo database before tests begin.
I do this way:
const app = require("../app");
const chai = require("chai");
const mongoose = require("mongoose");
const User = require('../models/users');
const Region = require('../models/regions');
const testUsers = require('../testdata/users.json');
const testRegions = require('../testdata/regions.json');
describe('Restoring database', function () {
before(function(done) {
var promises = [
User.deleteMany().exec()
,Region.deleteMany().exec()
];
console.log('Cleaned database');
done();
});
before(function(done) {
testUsers.users.forEach(element => {
var ObjectId = mongoose.Types.ObjectId;
element._id = new ObjectId(element._id);
var newUser = new User(element);
newUser.save(function (err, result) {
if (err) {
console.log("err:",err);
}
});
});
console.log('Users added');
done();
});
before(function(done) {
testRegions.regions.forEach(element => {
var newRegion = new Region(element);
newRegion.save(function (err, result) {
if (err) {
console.log("err:",err);
}
});
});
console.log('Regions added');
done();
});
testdata/users.json and testdata/regions.json are simple json files including key/pair values.
Does this look good?
When I run
npm test
It does not give any error. And in the console I see this:
Restoring database
Cleaned database
Users added
Regions added
But when I look in database I get different results.
Sometimes everything looks good. All the rows are in the collections.
Sometimes a few rows are missing in one of the collections.
Sometimes one of the collections is empty.
This is a very strange behaviour.
I also tried to add in the variable "promises" each of the "newUser" and "newRegion" instead of executing them directly.
But I still get these strange results.
Whats the deal?
The issue is due to done() being called before the async statements/promises have completed.
Either use async/await or use Promises and only call done() when your async statements/promises have completed.
For example:
No done() call as we using await statements which will wait till each statement completes before continuing:
before(async function() {
let userResult = await User.deleteMany();
let regionResult = wait Region.deleteMany();
console.log('Cleaned database');
});
Or use done() with promises:
before(function(done) {
User.deleteMany()
.then(result => {
console.log('Cleaned database');
done();
});
});
The syntax in your before example is not adding Promises at all, it is simply adding those functions to an array:
var promises = [
User.deleteMany().exec()
,Region.deleteMany().exec()
];
Take a look at the following related answer to help.

How to use async/await with mongoose

In node.js I had code like following:
mongoose.connect(dbURI, dbOptions)
.then(() => {
console.log("ok");
},
err => {
console.log('error: '+ err)
}
);
Now i want to do it with async/await syntax. So i could start with var mcResult = await mongoose.connect(dbURI, dbOptions);, afaik it will wait for operation, until it ends with any result (much like calling C function read() or fread() in syncronous mode).
But what should I write then? What does that return to the mcResult variable and how to check for an error or success? Basically I want a similar snippet, but written with proper async/await syntax.
Also I wonder because I have auto reconnect, among dbOptions:
dbOptions: {
autoReconnect: true,
reconnectTries: 999999999,
reconnectInterval: 3000
}
Would it "stuck" on await forever, in case if database connection is unavailble? I hope you can give me a clue on what would happen and how that would work.
Basically I want a similar snippet, but written with proper async/await syntax.
(async () => {
try {
await mongoose.connect(dbURI, dbOptions)
} catch (err) {
console.log('error: ' + err)
}
})()
Please try this, Below code has basics of db connectivity and a query :
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let url = 'mongodb://localhost:27017/test';
const usersSchema = new Schema({
any: {}
}, {
strict: false
});
const Users = mongoose.model('users', usersSchema, 'users');
/** We've created schema as in mongoose you need schemas for your collections to do operations on them */
const dbConnect = async () => {
let db = null;
try {
/** In real-time you'll split DB connection(into another file) away from DB calls */
await mongoose.connect(url, { useNewUrlParser: true }); // await on a step makes process to wait until it's done/ err'd out.
db = mongoose.connection;
let dbResp = await Users.find({}).lean(); /** Gets all documents out of users collection.
Using .lean() to convert MongoDB documents to raw Js objects for accessing further. */
db.close(); // Needs to close connection, In general you don't close & re-create often. But needed for test scripts - You might use connection pooling in real-time.
return dbResp;
} catch (err) {
(db) && db.close(); /** Needs to close connection -
Only if mongoose.connect() is success & fails after it, as db connection is established by then. */
console.log('Error at dbConnect ::', err)
throw err;
}
}
dbConnect().then(res => console.log('Printing at callee ::', res)).catch(err => console.log('Err at Call ::', err));
As we're talking about async/await then few things I wanted to mention - await definitely needs it's function to be declared as async - otherwise it would throw an error. And it's recommended to wrap async/await code inside try/catch block.
const connectDb = async () => {
await mongoose.connect(dbUri, dbOptions).then(
() => {
console.info(`Connected to database`)
},
error => {
console.error(`Connection error: ${error.stack}`)
process.exit(1)
}
)
}
connectDb().catch(error => console.error(error))
Lets assume the use of then() is prohibited, you can result to this...
const connectDb = async () => {
try {
await mongoose.connect(dbConfig.url, dbConfigOptions)
console.info(`Connected to database on Worker process: ${process.pid}`)
} catch (error) {
console.error(`Connection error: ${error.stack} on Worker process: ${process.pid}`)
process.exit(1)
}
}

Resources