e2e在Nest中测试微服务

时间:2019-08-01 18:59:49

标签: typescript microservices e2e-testing nestjs

我正在尝试在Nest中为微服务编写e2e测试。我想我已经正确创建了ProxyClient以便对服务进行请求。

我想在测试中做什么:

  1. 创建代理客户端
  2. 使用该客户端发送消息
  3. 让消息由服务接收
  4. 返回服务完成的消息处理的结果

这是我到目前为止所拥有的...似乎已经接近了,但是我在运行测试时一直看到这一点。

  

预期:是的,       收到:{“ _isScalar”:否,“操作员”:{“并发”:无限,“项目”:[匿名函数]},“源”:{“ _isScalar”:假,“ _ subscribe”:[匿名函数]} }

import { Inject } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { ClientsModule, Transport, ClientProxy } from '@nestjs/microservices';
import * as request from 'supertest';
import { HeartbeatModule } from './../src/heartbeat.module';
import { HeartbeatService } from './../src/heartbeat.service';


describe('AppController (e2e)', () => {
  let app;
  let client: ClientProxy;

  beforeEach(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [HeartbeatModule, ClientsModule.register([{ name: 'HEARTBEAT_SERVICE', transport: Transport.TCP }])],
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();

    client = app.get('HEARTBEAT_SERVICE');
    await client.connect();
  });

  afterAll(async () => {
    await client.close();
  });

  it('sends a level 1 heartbeat message to the HeartbeatService', async () => {
    const response = client.send({"cmd": "heartbeat"}, {"type": 1});

    expect(response).toBe(true);
  });
});

1 个答案:

答案 0 :(得分:1)

好吧,所以我之前缺少了一些东西……这里是工作代码...

import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { ClientsModule, Transport, ClientProxy } from '@nestjs/microservices';
import * as request from 'supertest';
import { HeartbeatModule } from './../src/heartbeat.module';
import { HeartbeatService } from './../src/heartbeat.service';
import { Observable } from 'rxjs';

describe('HeartbeatController (e2e)', () => {
  let app: INestApplication;
  let client: ClientProxy;

  beforeAll(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [
        HeartbeatModule,
        ClientsModule.register([
          { name: 'HEARTBEAT_SERVICE', transport: Transport.TCP },
        ]),
      ],
    }).compile();

    app = moduleFixture.createNestApplication();

    app.connectMicroservice({
      transport: Transport.TCP,
    });

    await app.startAllMicroservicesAsync();
    await app.init();

    client = app.get('HEARTBEAT_SERVICE');
    await client.connect();
  });

  afterAll(async () => {
    await app.close();
    client.close();
  });

  it('sends a level 1 heartbeat message to the HeartbeatService', done => {
    const response: Observable<any> = client.send(
      { cmd: 'heartbeat' },
      { type: 1 },
    );

    response.subscribe(json => {
      expect(Date.parse(json.now)).toBeLessThanOrEqual(new Date().getTime());
      expect(json.originalRequest.type).toBe(1);

      done();
    });
  });

  it('sends a level 2 heartbeat message to the HeartbeatService', done => {
    const requestJson = {
      type: 2,
      startTime: 12345,
      endTime: 67890,
      messagesSent: 22,
    };
    const response: Observable<any> = client.send(
      { cmd: 'heartbeat' },
      requestJson,
    );

    response.subscribe(json => {
      expect(Date.parse(json.now)).toBeLessThanOrEqual(new Date().getTime());
      expect(json.messagesReceived).toBe(22);
      expect(json.originalRequest.type).toBe(2);
      expect(json.originalRequest.startTime).toBe(12345);
      expect(json.originalRequest.endTime).toBe(67890);
      expect(json.originalRequest.messagesSent).toBe(22);

      done();
    });
  });

});

我收到一个开玩笑的错误“测试运行完成后,开玩笑没有退出一秒钟。”。我可能会忽略它,但是对于如何解决它持开放态度:)

编辑:感谢Jay McDoniel,我用完整的工作代码更新了答案。