mercredi 12 février 2020

Mock InfluxDB for end to end tests

I want to write e2e tests for a REST API I have written in typescript (NestJS). The API uses the influx npm package to get data from an influx instance.
I don't want to start an influx instance for the tests and thought I could mock the influx client, but I have trouble doing so.
My concrete problem here is that I don't know how to create the response that the client instance would return, when InfluxDB.query() is invoked.

So here is my current test setup:

import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from '../src/app.module';
import { InfluxDbClient } from '../src/shared/clients/influx-client';
import { IResults } from 'influx';
import { IMeasurement } from '../src/shared/types/results';

describe('AppController (e2e)', () => {
  let app: { init: () => any; getHttpServer: () => any };
  let dbClient: InfluxDbClient; // extends InfluxDB

  beforeEach(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();
    dbClient = moduleFixture.get<InfluxDbClient>(InfluxDbClient);
    app = moduleFixture.createNestApplication();
    await app.init();
  });

  it('GET last measurement', async () => {
    let dbMock: jest.SpyInstance;
    dbMock = jest.spyOn(dbClient, 'query');
    dbMock.mockImplementation(
      (): Promise<IResults<IMeasurement>> =>
        new Promise(resolve => {
          setTimeout(
            () =>
              resolve(
                null,         // what to do here???
              ),
            1000,
          );
        }),
    );
    await request(app.getHttpServer())
      .get('/measurements/demoDevice/illuminance/last?precision=ms')
      .expect(200)
      .expect('{"timestamp":1581086196501,"illuminance":919.8247849488981}');
  });

The method that actually calls query() is:

getLastMeasurement(
    deviceId: string,
    datapoint: string,
  ): Promise<IResults<IMeasurement>> {
    return this.client.query('SELECT LAST(' + datapoint + ') FROM ' + deviceId);
  }

IMeasurement is my own type, that I use to handle the result later because I didn't find out what else is used when query() returns a IResults<T> type.

export interface IMeasurement {
  time: INanoDate;
  [name: string]: object;
}

When I use the Database and simply print the result I get this:

this.databaseService.getLastMeasurement(deviceId, datapoint).then(values => {
  console.log(values);
});

OUTPUT:
[ { time:
     { 2020-02-07T14:36:36.501Z
       _nanoISO: '2020-02-07T14:36:36.501Z',
       getNanoTime: [Function: getNanoTimeFromISO],
       toNanoISOString: [Function: toNanoISOStringFromISO] },
    last: 919.8247849488981 },
  groupsTagsKeys: [],
  groupRows: [ { name: 'demoDevice', rows: [Array], tags: {} } ],
  group: [Function: groupMethod],
  groups: [Function: groupsMethod] ]

Does someone have a solution for this? If not are there other ways to avoid the influx instance?

Thank you.

Aucun commentaire:

Enregistrer un commentaire