Skip to content

Commit

Permalink
Refactored E2E tests to stabilize them
Browse files Browse the repository at this point in the history
  • Loading branch information
Shamshiel committed Feb 14, 2021
1 parent ca84566 commit be6958a
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 180 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist/
.vscode
77 changes: 46 additions & 31 deletions test/app-async.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,53 @@ import { INestMicroservice } from '@nestjs/common';
import AppModule from './e2e/app/config.app.async';
import { TestConsumer } from './e2e/app/test.controller';
import { messages } from './constants';
import { Utils } from './utils';

describe('AppModule Async (e2e)', () => {
let app: INestMicroservice;
let controller: TestConsumer;

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestMicroservice({});
app.enableShutdownHooks();
await app.listenAsync();

controller = await app.resolve(TestConsumer);
controller.messages = [];
});

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

it("should give kafka some time", done => {
setTimeout(done, 4000);
});

it('We can SEND and ACCEPT AVRO messages', async () => {
return controller.sendMessage({ messages }).then(() => {
expect(controller.messages.length).toBe(messages.length);

expect(controller.messages[0]).toEqual(messages[0].value);
expect(controller.messages[1]).toEqual(messages[1].value);
let app: INestMicroservice;
let controller: TestConsumer;

beforeAll(async () => {
jest.setTimeout(10000);

await Utils.schemaRegistrySetup();

const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule]
}).compile();

app = moduleFixture.createNestMicroservice({});
app.enableShutdownHooks();
await app.listenAsync();

controller = await app.resolve(TestConsumer);
controller.messages = [];
});

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

beforeEach(async () => {
await Utils.sleep(2000);

controller = await app.resolve(TestConsumer);
controller.messages = [];
});

it('We can SEND and ACCEPT AVRO messages', async () => {
await Utils.sleep(2000);

await controller.sendMessage({ messages });

let count = 0;
while (controller.messages.length < messages.length && count < 4) {
await Utils.sleep(1000);
count++;
}

expect(controller.messages.length).toBe(messages.length);
expect(controller.messages.find((x) => x.id == messages[0].value.id)).toBeDefined();
expect(controller.messages.find((x) => x.id == messages[1].value.id)).toBeDefined();
});
});
});
74 changes: 43 additions & 31 deletions test/app-sync.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,50 @@ import { INestMicroservice } from '@nestjs/common';
import AppModule from './e2e/app/config.app.sync';
import { TestConsumer } from './e2e/app/test.controller';
import { messages } from './constants';
import { Utils } from './utils';

describe('AppModule Sync (e2e)', () => {
let app: INestMicroservice;
let controller: TestConsumer;

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestMicroservice({});
app.enableShutdownHooks();
await app.listenAsync();

controller = await app.resolve(TestConsumer);
controller.messages = [];
});

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

it("should give kafka some time", done => {
setTimeout(done, 4000);
});

it('We can SEND and ACCEPT AVRO messages', async () => {
return controller.sendMessage({ messages }).then(() => {
expect(controller.messages.length).toBe(messages.length);

expect(controller.messages[0]).toEqual(messages[0].value);
expect(controller.messages[1]).toEqual(messages[1].value);
let app: INestMicroservice;
let controller: TestConsumer;

beforeAll(async () => {
jest.setTimeout(10000);

await Utils.schemaRegistrySetup();

const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule]
}).compile();

app = moduleFixture.createNestMicroservice({});
app.enableShutdownHooks();
await app.listenAsync();
});

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

beforeEach(async () => {
await Utils.sleep(2000);

controller = await app.resolve(TestConsumer);
controller.messages = [];
});

it('We can SEND and ACCEPT AVRO messages', async () => {
await Utils.sleep(2000);

await controller.sendMessage({ messages });

let count = 0;
while (controller.messages.length < messages.length && count < 4) {
await Utils.sleep(1000);
count++;
}

expect(controller.messages.length).toBe(messages.length);
expect(controller.messages.find((x) => x.id == messages[0].value.id)).toBeDefined();
expect(controller.messages.find((x) => x.id == messages[1].value.id)).toBeDefined();
});
});
});
92 changes: 0 additions & 92 deletions test/app.e2e-spec.ts

This file was deleted.

26 changes: 0 additions & 26 deletions test/e2e-setup.spec.ts

This file was deleted.

27 changes: 27 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { SchemaRegistry, readAVSC } from '@kafkajs/confluent-schema-registry';
import { join } from 'path';

export class Utils {
public static sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

public static async schemaRegistrySetup() {
const registry = new SchemaRegistry({ host: 'http://localhost:8081/' });

// For our other tests we require the schema to already exist
// in schema registry and dont allow uploaded through the nestJS
// application.
const valuePath = join(__dirname, 'e2e', 'app', 'value-schema.avsc');
const keyPath = join(__dirname, 'e2e', 'app', 'key-schema.avsc');
const valueSchema = readAVSC(valuePath);
const keySchema = readAVSC(keyPath);

const valueSchemaResult = await registry.register(valueSchema, {
separator: '-'
});
const keySchemaResult = await registry.register(keySchema, {
separator: '-'
});
}
}

0 comments on commit be6958a

Please sign in to comment.