Reputation: 577
I've started making a new Express application, and the testing framework of choice is Jest.
I've managed to cover every line of code, except callback of the mongoose.connect
method:
I've tried spying on the connect
method of the mongoose
object, so I can specify the returned object, but to no avail.
Is there a way to test the callback of mongoose.connect
?
Upvotes: 2
Views: 5179
Reputation: 102577
Here is the solution, your code is not easy to test, so I make some refactorings.
index.ts
:
import mongoose, { Mongoose } from 'mongoose';
import { MongoError } from 'mongodb';
function callback(err?: MongoError) {
if (err) {
console.log(err.message);
} else {
console.log('Succesfully Connected!');
}
}
function connectDatabase(): Promise<Mongoose> {
const mongoUrl = 'localhost';
return mongoose.connect(mongoUrl, { useCreateIndex: true, useNewUrlParser: true }, exports.callback);
}
exports.callback = callback;
exports.connectDatabase = connectDatabase;
Unit test:
import mongoose, { Mongoose, ConnectionOptions } from 'mongoose';
import { MongoError } from 'mongodb';
jest.mock('mongoose');
describe('connectDatabase', () => {
const dbModule = require('./');
it('should connect database succesfully', done => {
const consoleLogSpyOn = jest.spyOn(console, 'log');
const mongooseConnectSpyOn = jest
.spyOn<Mongoose, 'connect'>(mongoose, 'connect')
.mockImplementationOnce((uris: string, options?: ConnectionOptions, callback?: (err?: MongoError) => void) => {
if (callback) {
callback();
done();
}
return Promise.resolve(mongoose);
});
dbModule.connectDatabase();
expect(mongooseConnectSpyOn).toBeCalledWith(
'localhost',
{ useCreateIndex: true, useNewUrlParser: true },
dbModule.callback
);
expect(consoleLogSpyOn).toBeCalledWith('Succesfully Connected!');
consoleLogSpyOn.mockRestore();
});
it('connect database error', done => {
const consoleLogSpyOn = jest.spyOn(console, 'log');
const mongooseConnectSpyOn = jest
.spyOn<Mongoose, 'connect'>(mongoose, 'connect')
.mockImplementationOnce((uris: string, options?: ConnectionOptions, callback?: (err?: MongoError) => void) => {
if (callback) {
callback(new Error('connect error'));
done();
}
return Promise.resolve(mongoose);
});
dbModule.connectDatabase();
expect(mongooseConnectSpyOn).toBeCalledWith(
'localhost',
{ useCreateIndex: true, useNewUrlParser: true },
dbModule.callback
);
expect(consoleLogSpyOn).toBeCalledWith('connect error');
consoleLogSpyOn.mockRestore();
});
});
Unit test result and coverage report:
PASS src/stackoverflow/56132437/index.spec.ts
connectDatabase
✓ should connect database succesfully (12ms)
✓ connect database error (1ms)
console.log node_modules/jest-mock/build/index.js:860
Succesfully Connected!
console.log node_modules/jest-mock/build/index.js:860
connect error
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 1.853s, estimated 5s
Here is the completed demo: https://github.com/mrdulin/jest-codelab/blob/master/src/stackoverflow/56132437/index.spec.ts
Upvotes: 6