Getting db.close is not a function in sample JEST - mongodb

I'm trying to setup testing my node with mongo app using Jest. I've set it up and copied their sample verbatim, which works fine, except when I call the db.close(); function. It gives me a TypeError: that db.close is not a function. This is directly out of their example:
import { MongoClient } from 'mongodb';
describe('insert', () => {
let connection;
let db;
beforeAll(async () => {
connection = await MongoClient.connect(global.__MONGO_URI__, {
useNewUrlParser: true,
});
db = await connection.db(global.__MONGO_DB_NAME__);
});
afterAll(async () => {
await connection.close();
await db.close();
});
it('should insert a doc into collection', async () => {
const users = db.collection('users');
const mockUser = {_id: 'some-user-id', name: 'John'};
await users.insertOne(mockUser);
const insertedUser = await users.findOne({_id: 'some-user-id'});
expect(insertedUser).toEqual(mockUser);
});
});

Related

Incorrect State for operation: "new", allowed States: "[running,starting]"

There is just one test case that I'm testing. I verified the MongoDB methods and they seem to be up to
date. No open issues on GitHub as well.
Error: at MongoMemoryServer.getUri (node_modules/mongodb-memory-server-
core/src/MongoMemoryServer.ts:706:15)
at src/test/setup.ts:7:32
at src/test/setup.ts:8:71
at Object.<anonymous>.__awaiter (src/test/setup.ts:4:12)
at Object.<anonymous> (src/test/setup.ts:5:22)
import { MongoMemoryServer } from "mongodb-memory-server";
import mongoose from "mongoose";
let mongo: any;
beforeAll(async () => {
mongo = new MongoMemoryServer();
const mongoUri = await mongo.getUri();
await mongoose.connect(mongoUri);
});
beforeEach(async () => {
const collections = await mongoose.connection.db.collections();
for (let collection of collections) {
await collection.deleteMany({});
}
});
afterAll(async () => {
await mongo.stop();
await mongoose.connection.close();
});
channge the line mongo = new MongoMemoryServer(); to mongo = await MongoMemoryServer.create()
I had your same issue and by looking at the documentation I figure out that you first have to create the object with new and then start the instance with start():
mongod = new MongoMemoryServer()
await mongod.start()
const uri = await mongod.getUri()
const mongooseOptions = {
useNewUrlParser:true,
};
await mongoose.connect(uri,mongooseOptions)
You have 2 options to solve this problem:
1- Change the line:
mongo = new MongoMemoryServer();
to:
mongo = await MongoMemoryServer.create();
2- Change the lines:
mongo = new MongoMemoryServer();
const mongoUri = await mongo.getUri();
await mongoose.connect(mongoUri);
To:
mongod = new MongoMemoryServer()
await mongod.start()
const uri = await mongod.getUri()
const mongooseOptions = {
useNewUrlParser:true,
};
await mongoose.connect(uri, mongooseOptions)

Testing VSCode Extension with mocha and puppeteer behaves strangely

I'm writing test code for VS Code Extension with mocha and puppeteer.
This code behaves oddly.
This test will change the result without editing the code.
Sometimes test is failed because timeout of const e = await global.page.waitForSelector("iframe", { timeout: 10000 });, and sometimes it succeeds.
Changing the value of the slowMo option of puppeteer.launch() after several timeouts may result in a successful test. (not every time)  
How can I make this stable?
describe("test", () => {
before(async () => {
global.browser = await puppeteer.connect({
browserURL: 'http://127.0.0.1:9229',
defaultViewport: null,
slowMo: 10
});
global.page = (await global.browser.pages())[0];
});
after(async () => {
if (global.page) {
await global.page.close();
}
if (global.browser) {
global.browser.disconnect();
}
});
describe("open file", () => {
before(async () => {
// open file with my custom editor
const uri = Uri.file(path.join(__dirname, "test.pdf"));
await commands.executeCommand("vscode.open", uri);
// get editor's dom content
const e = await global.page.waitForSelector("iframe", { timeout: 10000 });
const ec = await editor.contentFrame();
const ee = await editorFrame.waitForSelector("iframe#active-frame", { timeout: 10000 });
const eec = await editorContent.contentFrame();
global.dom = eec;
});
after(async () => {
await commands.executeCommand("workbench.action.closeActiveEditor");
});
it("should have x", async () => {
const x = await global.doc.waitForSelector("#x", { timeout: 5000 });
assert.ok(x);
});
});
});

mongodb client not connecting in jest test

I am trying to setup an integration test for apollo graphql and need to connect to my mongodb database but this doesnt seem to be working. See the below code:
const MongoClient = require('mongodb').MongoClient
const LOADER_URI = 'mongodb://localhost:27017'
describe('ApolloIntegrationTest', () => {
test('Test', () => {
MongoClient.connect(URI, (err, client) => {
if(err) throw err
const pulseCore = client.db('pulse-core')
console.log(pulseCore)
const books = pulseCore.collection('books')
console.log(books)
client.close()
})
})
});
When i run the test, i expected to see the console logs printing but nothing prints when the test finishes. Not sure what i am doing wrong here.
You can use beforeAll and afterAll to simplify this.
const MongoClient = require('mongodb').MongoClient;
const LOADER_URI = 'mongodb://localhost:27017'
describe('ApolloIntegrationTest', () => {
let connection;
beforeAll(async () => {
connection = await MongoClient.connect(LOADER_URI);
});
afterAll(async () => {
await connection.close();
});
test('Test', async () => {
const pulseCore = await connection.db('pulse-core');
console.log(pulseCore);
const books = pulseCore.collection('books');
console.log(books);
});
});

Why are my tests occasionally passing and occasionally failing? (Jest, Mongoose, MongoDB)

I have a setup file for my tests that looks like this:
const mongoose = require('mongoose');
mongoose.set('useCreateIndex', true);
mongoose.promise = global.Promise;
async function removeAllCollections() {
const collections = Object.keys(mongoose.connection.collections);
for (const collectionName of collections) {
const collection = mongoose.connection.collections[collectionName];
await collection.deleteMany();
}
}
async function dropAllCollections() {
const collections = Object.keys(mongoose.connection.collections);
for (const collectionName of collections) {
const collection = mongoose.connection.collections[collectionName];
try {
await collection.drop();
} catch (error) {
// Sometimes this error happens, but you can safely ignore it
if (error.message === 'ns not found') return;
// This error occurs when you use it.todo. You can
// safely ignore this error too
if (error.message.includes('a background operation is currently running'))
return;
console.log(error.message);
}
}
}
export default function setupDB(databaseName) {
// Connect to Mongoose
beforeAll(async () => {
const url = `mongodb://127.0.0.1/${databaseName}`;
await mongoose.connect(
url,
{
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
},
err => {
if (err) {
console.error(err);
process.exit(1);
}
}
);
});
// Cleans up database between each test
afterEach(async () => {
await removeAllCollections();
});
// Disconnect Mongoose
afterAll(async () => {
await dropAllCollections();
await mongoose.connection.close();
});
}
I am then writing tests like this:
import User from 'db/models/User';
import setupDB from 'utils/setupDbForTesting';
setupDB('mongoose_bcrypt_test');
it('correctly hashes and salts passwords', async done => {
// create a user a new user
const newUser = new User({
username: 'jmar777',
password: 'Password123'
});
await newUser.save(function (err) {
if (err) {
console.log(err);
}
});
const user = await User.findOne({ username: 'jmar777' });
user.comparePassword('Password123', function (err, isMatch) {
if (err) throw err;
expect(isMatch).toBeTruthy();
});
user.comparePassword('123Password', function (err, isMatch) {
if (err) throw err;
expect(isMatch).toBeFalsy();
});
done();
});
However, every other time I run these tests, they pass (or fail) so for every time T that the tests pass, T + 1 they will fail. My question is - why?
The tests fail because user (in the callback for User.findOne) returns null, even though the user has been saved.
I think the issue lies in the tearing down of the database, but I really can't see any problems. Any help would be appreciated, thanks.

Cannot read property 'find' of inherited method in unit tests

im having an issue trying to run an e2e test for my NestJS application tha uses a mongodb-memory-server to run mongo in memory
my e2e test looks like this
describe('UsersController.e2e', () => {
let app: INestApplication;
let module: TestingModule;
const mongod = new MongoMemoryServer();
beforeAll(async () => {
const port = await mongod.getPort();
const database = await mongod.getDbName();
module = await Test.createTestingModule({
providers: [UserRepository, UserService],
controllers: [UserController],
imports: [TypeOrmModule.forRootAsync({
useFactory: () => {
return {
type: 'mongodb',
host: '127.0.0.1',
port,
database,
entities: [__dirname + '../../../**/*.entity{.ts,.js}'],
} as TypeOrmModuleOptions;
},
}),
TypeOrmModule.forFeature([User])
]
}).compile();
app = module.createNestApplication();
await app.init();
});
afterAll(async () => {
await module.close();
await app.close();
});
describe('GET /users', () => {
it('should return a collection of user resources', async () => {
const { body } = await supertest
.agent(app.getHttpServer())
.get('/users')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200);
expect(body).toEqual(userCollectionMock);
});
});
});
when running the test it throws a 500 error
Cannot read property 'find' of undefined
TypeError: Cannot read property 'find' of undefined
at UserRepository.Object.<anonymous>.MongoRepository.find (src/repository/MongoRepository.ts:77:29)
at UserRepository.index (src/modules/user/repositories/user.repository.ts:12:20)
the repository class looks like
export class UserRepository extends MongoRepository<User> implements IResourceRepository<User> {
index(): Promise<User[]> {
return this.find();
}
}
the extension of MongoRepository provides find() https://github.com/typeorm/typeorm/blob/master/src/repository/MongoRepository.ts#L76
So it perplexes me as to why it is undefined!
I know this is a month old, but I just ran into this issue and resolved it.
I'm using mongoose + typegoose with mongodb-memory-server.
import { MongoMemoryServer } from 'mongodb-memory-server';
describe('Auth', () => {
let app: INestApplication;
let mongoServer: MongoMemoryServer;
beforeAll(async () => {
// Set up database
mongoServer = new MongoMemoryServer();
const mongoUri = await mongoServer.getUri();
const moduleRef = await Test.createTestingModule({
imports: [
...
TypegooseModule.forRoot(mongoUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
}),
...
],
controllers: [...],
providers: [...],
}).compile();
app = moduleRef.createNestApplication();
await app.init();
});
afterAll(async () => {
await mongoServer.stop();
});
});
The main mistake I was making was that I had separately overwritten my User Typegoose injection.
providers: [
// THIS WAS MY MISTAKE!
{
provide: getModelToken('User'),
useValue: mockUserModel,
},
],
I know this is not a complete answer to your question, but I hope this might help you.