1

I created acceptance test of controller that using memory datasource. Before the test start, I was trying to have clean database and only add 1 user credential to login by calling endpoint since the rest of test require authenticated access with access token.

I can get the user info in getUser after created the user record from givenUser. However, the login request can't find the credential. It seems that the helpers and client are not sharing the same memory datasource.

I am not sure what configuration setup is wrong in this case.

src/tests/fixtures/datasources/testdb.datasource.ts

import {juggler} from '@loopback/repository';

export const testdb: juggler.DataSource = new juggler.DataSource({
  name: 'db',
  connector: 'memory',
});

src/datasources/mongodb.datasource.ts

import {inject} from '@loopback/core';
import {juggler} from '@loopback/repository';
import * as config from './mongodb.datasource.json';

export class MongodbDataSource extends juggler.DataSource {
  static dataSourceName = 'mongodb';

  constructor(
    @inject('datasources.config.mongodb', {optional: true})
    dsConfig: object = config,
  ) {
    super(dsConfig);
  }
}

user.controller.acceptance.ts

import {Client, expect, supertest} from '@loopback/testlab';
import {ApiApplication} from '../..';
import {setupApplication} from './test-helper';
import {givenEmptyDatabase, givenUser,getUser} from '../helpers/database.helpers';

describe('UserController', () => {
  let app: ApiApplication;
  let client: supertest.SuperTest<supertest.Test>;;
  let jwtToken: string;

  before('setupApplication', async () => {
    ({app, client} = await setupApplication());
  });
  before(givenEmptyDatabase);
  before(givenUser);
  before(getUser);
  before(async () => {
    const response = await client
      .post('/login')
      .send({username: 'user1', password: 'password'});
    jwtToken = response.body.token;
  });

  after(async () => {
    await app.stop();
  });

  it('invokes GET /info without authentication', async () => {
    const expectedError = {
      error: {
        statusCode: 401,
        name: 'UnauthorizedError',
        message: 'Unauthorized'
      }
    };
    const res = await client.get('/user/info').expect(401);
    expect(res.body).to.containEql(expectedError);
  });

test-help.ts

import {ApiApplication} from '../..';
import {
  createRestAppClient,
  givenHttpServerConfig,
  Client,
} from '@loopback/testlab';
import {testdb} from '../fixtures/datasources/testdb.datasource';

export async function setupApplication(): Promise<AppWithClient> {

  const app = new ApiApplication();
  await app.bind('datasources.config.mongodb').to({
    name: 'mongodb',
    connector: 'memory',
    debug: true,
  });

  await app.boot();
  await app.start();

  const client = createRestAppClient(app);

  return {app, client};
}

export interface AppWithClient {
  app: ApiApplication;
  client: Client;
}

database.helper.ts

import { UserRepository } from '../../repositories';
import { User } from '../../models';
import { testdb } from '../fixtures/datasources/testdb.datasource';

export async function givenEmptyDatabase() {
  let userRepo: UserRepository;

  userRepo = new UserRepository(testdb);

  userRepo.deleteAll();
}

export async function givenUser() {
  let userRepo = new UserRepository(testdb);
  const user = {
    username: 'user1',
    password: 'password',
    created_at: new Date('2019-08-08'),
    updated_at: new Date('2019-08-08'),
  }
  await userRepo.create(operator);
}

export async function getUser() {
  let userRepo = new UserRepository(testdb);
  const users = await userRepo.find();
  console.log(users);
}

1 Answer 1

1

It seems that the helpers and client are not sharing the same memory datasource.

Exactly.

It's important to realize that you can have multiple datasources using the same connector, for example you can one logDb using one MongoDB server and userDb connecting to another one. The same applies to memory datasources, you can have multiple datasources using the memory connector and each instance will have its own space for data.

Since you are already changing the configuration of your main mongodb datasource in the tests, my recommendation is to use that modified datasource when setting up the initial test data.

In database.helper.ts:

export async function givenEmptyDatabase(db) {
  let userRepo: UserRepository;

  userRepo = new UserRepository(db);

  userRepo.deleteAll();
}

export async function givenUser(db) {
  // ...
}
// and so on

In your test:

before(async () => {
 const db = await app.get<juggler.DataSource>('datasources.config.mongodb');
 await givenEmptyDatabase(db);
});

Personally, I strongly recommend use the same database in your tests as you will use in production. The memory database behaves differently from MongoDB. Certain operations may pass when storing data in memory but fail when MongoDB is used.

You can learn more about testing in LoopBack's "Best Practice" guides: Testing your application

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.