use*_*235 5 typescript microservices e2e-testing nestjs
我正在尝试为 Nest 中的微服务编写 e2e 测试。我想我已经正确创建了 ProxyClient 以便向服务发出请求。
我在测试中试图做什么:
这是我到目前为止所拥有的......看起来很接近,但当我运行测试时我不断看到这一点。
预期:true,收到:{“_isScalar”:false,“operator”:{“concurrent”:Infinity,“project”:[函数匿名]},“source”:{“_isScalar”:false,“_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);
});
});
Run Code Online (Sandbox Code Playgroud)
好吧,所以我错过了 beforeEach 中的一些内容...这是工作代码...
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();
});
});
});
Run Code Online (Sandbox Code Playgroud)
我收到一个笑话错误“测试运行完成后一秒 Jest 没有退出。”。我可能会忽略它,但我愿意接受任何有关如何修复它的想法:)
编辑:谢谢 Jay McDoniel,我用完整的工作代码更新了我的答案。
归档时间: |
|
查看次数: |
7973 次 |
最近记录: |