I wish to write unit tests for my mongodb datasource in my Loopback 4 project, however I could not find any examples.
Below is my datasource:
import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core';
import {juggler} from '@loopback/repository';
const arrayStr: string[] = [];
const config = {
name: 'mongodb',
connector: 'mongodb',
// url: '',
host: 'localhost',
port: 27017,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
authSource: 'admin',
database: 'testdb',
ssl: false, // default to false
sslValidate: false, // default to false
useNewUrlParser: true,
allowExtendedOperators: true,
sslCA: arrayStr,
};
// Observe application's life cycle to disconnect the datasource when
// application is stopped. This allows the application to be shut down
// gracefully. The `stop()` method is inherited from `juggler.DataSource`.
// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html
@lifeCycleObserver('datasource')
export class MongodbDataSource extends juggler.DataSource
implements LifeCycleObserver {
static dataSourceName = 'mongodb';
static readonly defaultConfig = config;
constructor(
@inject('datasources.config.mongodb', {optional: true})
dsConfig: object = config,
) {
super(dsConfig);
}
}
I have tried writing a basic constructor test:
import {MongodbDataSource} from '../../../datasources';
import {expect} from '@loopback/testlab';
describe('MongoDB Datasource (unit)', () => {
// we recommend to group tests by method names
describe('consturctor()', () => {
it('basic constructor', () => {
const ds = new MongodbDataSource();
expect(ds.name).to.equal('mongodb'); // default name
});
});
});
However, tests will hang/stop and not completed. Appreciate any advice.